I have script that writes two triangle, blue and red.
I want to make transparent this area instead of blue and red like Paper-cutting work.
(I want to put the image background, so it must be transparent not white.)
I am struggling with the method clip()
but it doesn’t work what I want.
How can I make it?
function draw() {
var canvas = document.getElementById('test');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'green';
ctx.fillRect(0, 0, 400, 400);
ctx.fillStyle = 'blue';
ctx.beginPath();
ctx.moveTo(50, 70);
ctx.lineTo(100, 100);
ctx.lineTo(150, 200);
ctx.closePath();
ctx.fill();
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.moveTo(100, 170);
ctx.lineTo(300, 200);
ctx.lineTo(350, 300);
ctx.closePath();
ctx.fill();
}
}
<body onload="draw();">
<canvas id="test" width="400px" height="300px"></canvas>
</body>
2
Answers
Something like this? Draw the overlay, cut it out, then draw the background.
You can use
ctx.globalCompositeOperation = 'xor'
if the image is not part of the canvas and you need actual transparency on the canvas. See mdn:Else,
clip()
works fine. Yours is not working might because you drawed two shape and clip() actually takes the intersection, not union of the two, and the two shape has nearly zero intersected region.