【发布时间】:2022-01-26 20:49:17
【问题描述】:
我正在尝试在 HTML 画布上绘制一个透明的圆圈。代码如下:
const canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
canvas.width = 700;
canvas.height = 700;
ctx.beginPath();
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalCompositeOperation = 'destination-out';
ctx.clearRect(50, 50, 200, 200);
ctx.beginPath();
ctx.fillRect(50, 50, 200, 200);
ctx.moveTo(350, 150);
ctx.beginPath();
ctx.arc(350, 150, 80, 0, 2 * Math.PI, false);
ctx.closePath();
ctx.fill();
我可以通过将globalCompositeOperation 设置为destination-out 使矩形透明,但无法使圆完全透明。
这是MDN 所说的关于destination-out 操作的内容,
The existing content is kept where it doesn't overlap the new shape.
圆圈仍然应用了一些不透明的颜色。如何使其完全透明? 这是live 演示。
无法理解代码有什么问题。任何帮助将不胜感激。
【问题讨论】: