运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>HTML5 Canvas</h1> <h2>The createPattern() Method</h2> <p>Image to use:</p> <img src="../jsref/img_lamp.jpg" id="lamp" width="32" height="32"> <p> <button onclick="draw('repeat')">Repeat</button> <button onclick="draw('repeat-x')">Repeat-x</button> <button onclick="draw('repeat-y')">Repeat-y</button> <button onclick="draw('no-repeat')">No-repeat</button> </p> <canvas id="myCanvas" width="300" height="150" style="background:black;"> </canvas> <script> function draw(direction) { const c = document.getElementById("myCanvas"); const ctx = c.getContext("2d"); ctx.clearRect(0, 0, c.width, c.height); const img = document.getElementById("lamp") const pat = ctx.createPattern(img, direction); ctx.rect(0, 0, 150, 100); ctx.fillStyle = pat; ctx.fill(); } </script> </body> </html>