搜索文档
首页
HTML/CSS
JavaScript
服务端开发
Java教程
移动端
数据库
当前位置:
首页
HTML/CSS
HTML参考手册
HTML 画布
源代码
清空
点击运行
<!DOCTYPE html> <html> <head> <title>HTML canvas createPattern()方法的使用(大卫编程网 div.cn)</title> </head> <body> <p>图片应用:</p> <img src="haha.gif" id="lamp"> <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 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,220,128); ctx.fillStyle=pat; ctx.fill(); } </script> </body> </html>
运行结果