BootWiki教程网
源代码:
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>BootWiki 教程网</title> </head> <body> <p>图片应用:</p> <img id="lamp" src="img_lamp.jpg"> <p>画布:</p> <button onclick="draw('repeat')">重复</button> <button onclick="draw('repeat-x')">重复-x</button> <button onclick="draw('repeat-y')">重复-y</button> <button onclick="draw('no-repeat')">不重复</button> <canvas height="150" id="myCanvas" style="border:1px solid #d3d3d3;" width="300"> 您的浏览器不支持 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,220,128); ctx.fillStyle=pat; ctx.fill(); } </script> </body> </html>
运行结果