HTML canvas createPattern()Method

HTML canvas 参考手册HTML canvas Reference Manual

Image used:

Lamp

Example

Repeat the image in both horizontal and vertical directions:

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 yourself »

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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

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


Definition and Usage

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

The element can be an image, video, or another <canvas> element.

The repeated element can be used to draw/fill rectangles, circles, lines, and so on.

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

Parameter Values

Parameter Description
image Specifies the image, canvas, or video element to use as the pattern.  
repeat Default. The pattern is repeated in both horizontal and vertical directions.
repeat-x The pattern is repeated only in the horizontal direction.
repeat-y The pattern is repeated only in the vertical direction.
no-repeat The pattern is displayed only once (no repetition).


HTML canvas 参考手册HTML canvas Reference Manual Other Extensions