编辑下面的代码:【加编码】
<html> <body> <p>Image to use:</p> <img src="/demo/img_lamp.jpg" id="lamp"> <p>Canvas:</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> <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;"> 您的浏览器不支持 HTML5 canvas 标签。</canvas> <script> function draw(direction) { var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.clearRect(0,0,c.width,c.height); var img=document.getElementById("lamp") var pat=ctx.createPattern(img,direction); ctx.rect(0,0,150,100); ctx.fillStyle=pat; ctx.fill(); } </script> </body> </html>
结果: 【此窗口】 帮助?
Try it Yourself - © 自强学堂