Canvas createPattern() Method

Definition and Usage

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

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

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

Example

Image used:

lamp

Repeat the image horizontally and vertically:

Your browser does not support the HTML5 canvas tag.

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

Syntax

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

Parameter value

Parameter Description
image Specify the image, canvas, or video element to be used.
repeat Default. This pattern repeats both horizontally and vertically.
repeat-x This pattern repeats only horizontally.
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.