HTML canvas createPattern() method

Definition and Usage

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

The elements can be images, videos, or other <canvas> elements.

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

Example

Image used:

lamp

Repeat the image in both horizontal and vertical directions:

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 Specifies the image, canvas, or video element to be used.
repeat Default. This mode repeats in both horizontal and vertical directions.
repeat-x This mode repeats only in the horizontal direction.
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 to fully support 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.