javascript - jQuery drag and draw -



javascript - jQuery drag and draw -

i'm trying build jquery plugin allows drag , draw rectangle (or div border) i'm not sure how it. don't know of have ability don't know illustration of how this.

how can implement drag , draw in jquery?

the basics quite simple, when think it:

listen mousedown events on container (possible entire document); place absolutely positioned element @ position of mouse, using mouse coordinates event object (e.pagex , e.pagey); start listening mousemove events alter width , height object (based on mouse coordinates); listen mouseup event detach mousemove event listener.

the aforementioned absolute placed element is, e.g., <div> border , background: transparent.

update: here example:

$(function() { var $container = $('#container'); var $selection = $('<div>').addclass('selection-box'); $container.on('mousedown', function(e) { var click_y = e.pagey; var click_x = e.pagex; $selection.css({ 'top': click_y, 'left': click_x, 'width': 0, 'height': 0 }); $selection.appendto($container); $container.on('mousemove', function(e) { var move_x = e.pagex, move_y = e.pagey, width = math.abs(move_x - click_x), height = math.abs(move_y - click_y), new_x, new_y; new_x = (move_x < click_x) ? (click_x - width) : click_x; new_y = (move_y < click_y) ? (click_y - height) : click_y; $selection.css({ 'width': width, 'height': height, 'top': new_y, 'left': new_x }); }).on('mouseup', function(e) { $container.off('mousemove'); $selection.remove(); }); }); });

demo: http://jsbin.com/ireqix/226/

javascript jquery drag

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -