html5 canvas - Draw multiple circles filled with image content -


i trying draw circles on canvas filled portions image. imagine clicking on white canvas , user clicked reveal portion of photo.

i have found ways draw 1 circle, can not succeed using draw multiples. if repeat action other coordinates drawing not happening.

function start_drawing(){     ctx.beginpath();     ctx.fillstyle = "rgb(255,255,255)";     ctx.fillrect(0,0,canvas.width,canvas.height);//fill background. color default black     ctx.arc(mouse.x,mouse.y,45,0,6.28,false);//draw circle     ctx.arc(mouse.x+100,mouse.y+100,45,0,6.28,false);     ctx.clip();//call clip method next render clipped in last path     ctx.drawimage(img,0,0);      ctx.closepath(); } 

any idea on how can achieved ? thank you.


later edit (the entire exact code used)

<!doctype html> <html> <head> <script>  window.onload=function(){    var canvas = document.getelementbyid('mycanvas');   var ctx=canvas.getcontext('2d');    var mouse={x:0,y:0} //make object hold mouse position  canvas.onmousemove=function(e){mouse={x:e.pagex-this.offsetleft,y:e.pagey-this.offsettop};} //update mouse when canvas moved on  var img=new image();  img.src="bmw_gina.jpg";  setinterval( start_drawing ,100);// set animation motion   ctx.beginpath();  ctx.fillstyle = "rgb(255,255,255)";  ctx.fillrect(0,0,canvas.width,canvas.height);//fill background. color default black   ctx.closepath();  function start_drawing(){  //ctx.save();  ctx.beginpath();                       ctx.arc(mouse.x,mouse.y,45,0,6.28,false);//draw circle                      ctx.clip();//call clip method next render clipped in last path                      ctx.drawimage(img,0,0);    ctx.closepath();  //ctx.restore(); }  }  </script> </head> <body>     <canvas id="mycanvas" width="1003" height="914"></canvas>  </body> </html> 

there 2 issues can see yor code:

the first start_drawing clearing canvas every execution. each mouse click (i assume start_drawing called on mouse click) circle drawn canvas cleared before that.

the other need call beginpath , closepath each clipping region want create. code should that:

function start_drawing(){       ctx.fillstyle = "rgb(255,255,255)";      ctx.fillrect(0,0,canvas.width,canvas.height);//fill background. color default black     ctx.beginpath();      ctx.arc(mouse.x,mouse.y,45,0,6.28,false);//draw circle     ctx.clip();//call clip method next render clipped in last path      ctx.closepath();      ctx.drawimage(img,0,0);      ctx.beginpath();      ctx.arc(mouse.x+100,mouse.y+100,45,0,6.28,false);     ctx.clip();//call clip method next render clipped in last path      ctx.closepath();       ctx.drawimage(img2,0,0);   } 

update

well apparently, trick reset clipping region reset canvas. can achieved re setting it's width.

there go: http://jsfiddle.net/qcg9n/5/


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -