Transparency groups in CanvasRenderingContext2D

本秂侑毒 提交于 2019-12-02 00:19:49

No, that's currently not possible without using an intermediate canvas.

Unless all of your draw operations in question can be combined into a single path, thereby using a single call to stroke() and fill(). I assume that's not the case here, but people searching later may be looking for that information.

// two rectangles composed into a single draw operation:
ctx.rect(0, 0, 10, 10);
ctx.rect(5, 5, 10, 10);
ctx.fill();

// With transparency, the above produces a different result from:
ctx.rect(0, 0, 10, 10);
ctx.fill();
ctx.rect(5, 5, 10, 10);
ctx.fill();
Kaiido

Maybe because I'm used to the idea of layers, I do believe the off-sreen canvas is still the most elegant way to do it.

But note that in the exceptional* case of an empty canvas, you can also achieve it on a single canvas element, thanks to the globalCompositeOperation property.

The idea is to first draw your group of overlapping paths opaque, then use the gCO "destination-in" and draw a rectangle covering the whole area of the group with the wanted color(see comments and down of post edit for a better way of doing it, still with gCO) :

var ctx = c.getContext('2d');

// first draw your overlapping paths opaque
ctx.lineWidth = 10;
ctx.beginPath();
ctx.arc(40, 40, 35, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
ctx.arc(60, 60, 35, 0, Math.PI * 2);
ctx.stroke();
// then set the fill color to the one you want
ctx.fillStyle = 'rgba(0,255,0,.5)';
// set the gCo so we only draw on the existing pixels
ctx.globalCompositeOperation = 'source-in';
// draw a large rect
ctx.fillRect(0, 0, c.width, c.height);
p{position: absolute;z-index:-1}
body{background-color: ivory;}
<p>Is this transparent ?</p>
<canvas id="c"></canvas>

* Actually, the only one case I can think of where it could be the case, would be for an off-screen canvas ;-)

Edit

As noted by @K3N, it can even be simplified and improved by using the "copy" composite operation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!