I am wondering if the following code matches the correct behavior. I feel that the top left square should still be green. that is, if I fixed one area, ten restore, and then paint in the second area, canvas paint in areas of BOTH. Why?
https://jsfiddle.net/s6t8k3w3/
var my_canvas = document.getElementById('canvas');
var ctx = my_canvas.getContext("2d");
ctx.save();
ctx.rect(20, 20, 100, 100);
ctx.clip();
ctx.fillStyle = 'rgba(0,255,0,1)';
ctx.fillRect(0, 0, my_canvas.width, my_canvas.height);
ctx.restore();
ctx.save();
ctx.rect(50, 50, 100, 100);
ctx.clip();
ctx.fillStyle = 'rgba(0,0,255,1)';
ctx.fillRect(0, 0, my_canvas.width, my_canvas.height);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(500, 500);
ctx.stroke();
ctx.restore();
source
share