Get your own website Result Size: 625 x 565
x
 
<!DOCTYPE html>
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>The clip() Method</h2>
<p>Without clip():</p>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid grey"></canvas>
<p>With clip():</p>
<canvas id="myCanvas2" width="300" height="150" style="border:1px solid grey"></canvas>
<script>
const c1 = document.getElementById("myCanvas");
const ctx1 = c1.getContext("2d");
// Draw a rectangle
ctx1.rect(50, 20, 200, 120);
ctx1.stroke();
// Draw red rectangle
ctx1.fillStyle = "red";
ctx1.fillRect(0, 0, 150, 100);
const c2 = document.getElementById("myCanvas2");
const ctx2 = c2.getContext("2d");
// Clip a rectangular area
ctx2.rect(50, 20, 200, 120);
ctx2.stroke();
ctx2.clip();
// Draw red rectangle after clip()
ctx2.fillStyle = "red";
ctx2.fillRect(0, 0, 150, 100);
</script> 
</body>
</html>