javascript - How to draw a rectangle on canvas like we do on paint? -
say want draw rectangle on canvas. want able co-ordinates user's mouse. ideal scenario user clicks @ point , drags down end rectangles draw using paint. how can draw rectangle in paint dragging mouse? (how co-ordinates of mouse when clicks mouse , leaves at?)
here's outline of how drag-draw rectangle on canvas:
in mousedown:
- save starting mouse position
- set flag indicating drag has begun
in mousemove:
- clear canvas of previous rectangle
- calculate rectangle width/height based on starting vs current mouse position
- draw rectangle starting xy current mouse position
in mouseup:
- clear dragging flag because drag over
here's example code , demo: http://jsfiddle.net/m1erickson/6e2yd/
<!doctype html> <html> <head> <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css --> <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> <style> body{ background-color: ivory; } #canvas{border:1px solid red;} </style> <script> $(function(){ // references canvas , context var canvas=document.getelementbyid("canvas"); var ctx=canvas.getcontext("2d"); // style context ctx.strokestyle = "blue"; ctx.linewidth=3; // calculate canvas on window // (used calculate mousex/mousey) var $canvas=$("#canvas"); var canvasoffset=$canvas.offset(); var offsetx=canvasoffset.left; var offsety=canvasoffset.top; var scrollx=$canvas.scrollleft(); var scrolly=$canvas.scrolltop(); // flage true when user dragging mouse var isdown=false; // these vars hold starting mouse position var startx; var starty; function handlemousedown(e){ e.preventdefault(); e.stoppropagation(); // save starting x/y of rectangle startx=parseint(e.clientx-offsetx); starty=parseint(e.clienty-offsety); // set flag indicating drag has begun isdown=true; } function handlemouseup(e){ e.preventdefault(); e.stoppropagation(); // drag over, clear dragging flag isdown=false; } function handlemouseout(e){ e.preventdefault(); e.stoppropagation(); // drag over, clear dragging flag isdown=false; } function handlemousemove(e){ e.preventdefault(); e.stoppropagation(); // if we're not dragging, return if(!isdown){return;} // current mouse position mousex=parseint(e.clientx-offsetx); mousey=parseint(e.clienty-offsety); // put mousemove stuff here // clear canvas ctx.clearrect(0,0,canvas.width,canvas.height); // calculate rectangle width/height based // on starting vs current mouse position var width=mousex-startx; var height=mousey-starty; // draw new rect start position // current mouse position ctx.strokerect(startx,starty,width,height); } // listen mouse events $("#canvas").mousedown(function(e){handlemousedown(e);}); $("#canvas").mousemove(function(e){handlemousemove(e);}); $("#canvas").mouseup(function(e){handlemouseup(e);}); $("#canvas").mouseout(function(e){handlemouseout(e);}); }); // end $(function(){}); </script> </head> <body> <h4>drag mouse create rectangle</h4> <canvas id="canvas" width=300 height=300></canvas> </body> </html>
- clear
Comments
Post a Comment