Search code examples
htmlmousehtml5-canvasclip

Multiple clicking regions on HTML5 canvas?


I'm messing around with HTML5 canvas and clipping. I am wondering how I can get the user to click on the canvas, have it clip a circle, and then repeat. So essentially, the user can click multiple times and there will be multiple clips.

I tried a couple solutions that were slightly similar to what I want but it isn't working.

Here is the clipping code:

context.save();

context.beginPath();
context.arc(mouseX,mouseY,50,50,Math.PI*2,true);
context.globalCompositeOperation = 'destination-out';
context.clip();
context.closePath(); 
var img = new Image();
img.src = canvasSnowflake.toDataURL();

context.drawImage(canvasSnowflake, 0, 0);

context.restore();

you can view the entire thing in action here: http://jsfiddle.net/cnbishop/8FzuB/. right now you can click one time and the clip works, but you can get a new clip if you click on the canvas again. Is this even possible?


Solution

  • Everytime the user clips you'll need to save this action somehow in JS. Next time the user clicks, you retrieve the past clipping, apply it and then apply the new clip action.

    Basically, you need to reapply all the clipping actions in the history in the same order as they were executed as Canvas is unable to "remember" its previous rendering.

    Did I interpret your question correctly?