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:

Repeat the image horizontally and vertically:
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();
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.