Latest web development tutorials

HTML canvas createPattern () method

HTML canvas Reference Manual HTML canvas Reference Manual

Images used:

Lamp

Examples

Repeat the image horizontally and vertically:

YourbrowserdoesnotsupporttheHTML5canvastag.

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();

try it"

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support createPattern () method.

Note: 8 and earlier versions of Internet Explorer do not support the <canvas> element.


Definition and Usage

createPattern () method repeats a specified element in the specified direction.

Elements can be images, videos, or other <canvas> element.

Be repeated element can be used to draw / fill rectangle, circle, line, or the like.

JavaScript syntax: context .createPattern (image, "repeat |repeat-x | repeat-y | no-repeat");

Parameter Value

参数 描述
image 规定要使用的模式的图片、画布或视频元素。
repeat 默认。该模式在水平和垂直方向重复。
repeat-x 该模式只在水平方向重复。
repeat-y 该模式只在垂直方向重复。
no-repeat 该模式只显示一次(不重复)。


HTML canvas Reference Manual HTML canvas Reference Manual