copy-clip.html (459B)
1 <!DOCTYPE html> 2 <html> 3 <body> 4 <script> 5 function test() { 6 let canvas = document.createElement('canvas'); 7 canvas.width = canvas.height = 200; 8 let ctx = canvas.getContext('2d'); 9 ctx.globalCompositeOperation = 'copy'; 10 ctx.translate(0, 0); 11 ctx.rect(0, 0, 100, 100); 12 ctx.clip(); 13 ctx.fillStyle = '#f00'; 14 ctx.resetTransform(); 15 ctx.fillRect(0, 0, canvas.width, canvas.height); 16 return canvas; 17 } 18 19 document.body.appendChild(test(0)); 20 </script> 21 </body> 22 </html>