尝试向canvas添加一个形状,然后在不使用clearRect的情况下清除它,这是一个演示
const canvas = document.createElement('canvas')
canvas.height = 300
canvas.width = 300
const ctx = canvas.getContext('2d')
document.body.appendChild(canvas)
const make = () => {
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.quadraticCurveTo(100, 75, 100, 100);
ctx.arc(75, 100, 10, 0, 2 * Math.PI)
ctx.lineWidth = 1
ctx.stroke()
}
const draw = () => {
make()
}
const clear = () => {
ctx.save()
ctx.globalCompositeOperation = "destination-out";
make()
ctx.restore()
}
draw()
clear()