Canvas createPattern() 方法
定义和用法
createPattern()
方法在指定的方向内重复指定的元素。
元素可以是图片、视频,或其他<canvas>元素。
重复的元素可用于绘制/填充矩形、圆形或线条等等。
实例
使用的图像:

在水平和垂直方向重复图像:
JavaScript:
var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); var img=document.getElementById("lamp"); var pat=ctx.createPattern(img,"repeat"); ctx.rect(0,0,150,100); ctx.fillStyle=pat; ctx.fill();
语法
context.createPattern(image,"repeat|repeat-x|repeat-y|no-repeat");
参数值
参数 | 描述 |
---|---|
image | 规定要使用的图片、画布或视频元素。 |
repeat | 默认。模式在水平和垂直方向重复。 |
repeat-x | 模式仅在水平方向重复。 |
repeat-y | This mode repeats only in the vertical direction. |
no-repeat | This mode is displayed only once (not repeated). |
Browser Support
The numbers in the table indicate the first browser version that fully supports this property.
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.