html - HTML5 - Canvas, draw rectangle through div -


i've made example show problem is: http://jsfiddle.net/gsrgf/

html

<canvas id="canvas" width="500" height="500"></canvas> <div id="div"></div> 

js

$(function() { var ctx=$('#canvas')[0].getcontext('2d');  rect = {}; drag = false;  $(document).on('mousedown','#canvas',function(e){     rect.startx = e.pagex - $(this).offset().left;     rect.starty = e.pagey - $(this).offset().top;     rect.w=0;     rect.h=0;     drag = true; });  $(document).on('mouseup','#canvas',function(){     drag = false; });  $(document).on('mousemove','#canvas',function(e){     if (drag) {         rect.w = (e.pagex - $(this).offset().left)- rect.startx;         rect.h = (e.pagey - $(this).offset().top)- rect.starty;         ctx.clearrect(0, 0, 500, 500);         ctx.fillstyle = 'rgba(0,0,0,0.5)';         ctx.fillrect(rect.startx, rect.starty, rect.w, rect.h);     } });     }); 

how possible keep drawing rectangle when mouse on div?

thanks!

remove "#canvas" selector mousemove , mouseup events. edit mousemove event use $("#canvas") instead of $(this).

http://jsfiddle.net/gsrgf/3/

$(function() {     var ctx=$('#canvas')[0].getcontext('2d');      rect = {};     drag = false;      $(document).on('mousedown','#canvas',function(e){         rect.startx = e.pagex - $(this).offset().left;         rect.starty = e.pagey - $(this).offset().top;         rect.w=0;         rect.h=0;         drag = true;     });      $(document).on('mouseup',function(){         drag = false;     });      $(document).on('mousemove',function(e){         if (drag) {             rect.w = (e.pagex - $("#canvas").offset().left)- rect.startx;             rect.h = (e.pagey - $("#canvas").offset().top)- rect.starty;             ctx.clearrect(0, 0, 500, 500);             ctx.fillstyle = 'rgba(0,0,0,0.5)';             ctx.fillrect(rect.startx, rect.starty, rect.w, rect.h);         }     });     }); 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -