I would like to clip some round or rectangular holes of a given path. CanvasRenderingContext2D.clearRect() doesn't work here because I need to reveal the background. I referenced the answer here: https://stackoverflow.com/a/18993901/3066086
but it doesn't work when the shapes are touched. Here is the code to demonstrate my application and picture of the result/desired result:
<canvas id="canvas" width = "500" height="500" ></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.rect(0, 0,500, 500);
ctx.fillStyle = 'rgba(100,100,100,1)';
ctx.fill();
ctx.beginPath();
ctx.rect(0,0,500,500);
ctx.arc(100, 250, 50, 0, 2 * Math.PI);
ctx.closePath();
ctx.arc(300, 250, 50, 0, 2 * Math.PI);
ctx.closePath();
ctx.rect(95,245,200,10);
ctx.clip('evenodd');
ctx.beginPath();
ctx.rect(5, 5, 400, 400);
ctx.fillStyle = 'rgba(255,0,0,1)';
ctx.fill();
</script>
result:
desired result: