图片自找
<!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</title> <style type="text/css"> #canvas{border:1px solid #eee ; display:block; background-color: #B36666; margin: 20px auto; } </style></head><body><div><canvas id = "canvas" width = "800px" height = "800px"></canvas></div> <script type = "text/javascript" >
window.onload=function(){
var context = document.getElementById('canvas').getContext('2d')
/*
context.createPattern(img'repeat-style');
repeat-style参数可以是:
'no-repeat' 不重复
'repeat' x,y轴重复
'repeat-x' x轴重复
'repeat-y' y轴重复
扩展:context.createPattern(img'repeat-style')方法参数:
可以是image图片作为参数,
可以是canvas对象作为参数
可以是video视频对象作为参数
总结:context.fillStyle 可以等于以下参数
color
gradient
image
canvas
video;
*/
var image1 = new Image();
image1.src = '1.png';
image1.onload = function(){
var pattern = context.createPattern(image1,'repeat');
context.fillStyle=pattern;
context.fillRect(0,0,800,800);
}
}
</script> </body> </html>