Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML canvas createPattern() method

HTML canvas createPattern() method

HTML canvas Reference HTML canvas Reference Manual

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


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 .


Definition and Usage

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

The element can be an image, a video, or other .

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

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

Parameter value

Parameters Description
image Specifies the image, canvas, or video element to use for the pattern.
repeat Default. The pattern repeats in both horizontal and vertical directions.
repeat-x The pattern repeats only in the horizontal direction.
repeat-y The pattern repeats only in the vertical direction.
no-repeat The pattern is shown only once (not repeated).

HTML canvas Reference HTML canvas Reference Manual