运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,暗色/亮色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>HTML5 Canvas clip()</h1> <canvas id="myCanvas" width="300" height="150" style="border:1px solid grey"> Sorry, your browser does not support canvas. </canvas> <script> const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); // Create a triangle-shaped clipping region ctx.beginPath(); ctx.moveTo(100,20); ctx.lineTo(180,100); ctx.lineTo(20,100); ctx.lineTo(100,20); ctx.clip(); // Draw two rectangles ctx.fillStyle = "blue"; ctx.fillRect(0, 0, 300, 150); ctx.fillStyle = "red"; ctx.fillRect(0, 0, 90, 90); </script> </body> </html>