The createPattern() method is used to repeat the specified element in the specified direction. It can be an image, a video or any other canvas element.
Syntax:
context.createPattern(image, "repeat | repeat-x | repeat-y | no-repeat");
Parameters:
- image: It specifies the image, canvas, or video element of the pattern to be used.
- repeat: It repeats the pattern both horizontally and vertically. It is the default.
- repeat-x: It repeats the pattern only horizontally.
- repeat-y: It repeats the pattern only vertically.
- no-repeat: It doesn’t repeat the pattern .
Example:
<!DOCTYPE html> <html> <head> <title> HTML | canvas createPattern() Method </title> </head> <body> <h2 style="color:green;"> GeeksforGeeks </h2> <h2> HTML canvas createPattern() Method </h2> <p style="color:green;">Image to be used:</p> <img src= id="geeks" width="32" height="32"> <p>Canvas:</p> <canvas id="myCanvas" width="500" height="200" style="border:2px solid "> </canvas> <br><br> <!-- Change the values here --> <button onclick="create('repeat')">Repeat</button> <!-- Script to repeat the pattern --> <script> function create(value) { var gfg = document.getElementById("myCanvas"); var context = gfg.getContext("2d"); context.clearRect(0, 0, gfg.width, gfg.height); var img = document.getElementById("geeks") var pattern = context.createPattern(img, value); context.rect(0, 0, 150, 100); context.fillStyle = pattern; context.fill(); } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking the button:

- After clicking on repeat button:

Supported Browsers: The browsers supported by HTML canvas createPattern() Method are listed below:
- Chrome
- Mozilla Firefox
- Internet Explorer 9.0
- Opera
- Safari
Recommended Posts:
- HTML | canvas rect() Method
- HTML | canvas clearRect() Method
- HTML | canvas strokeRect() Method
- HTML | canvas fillRect() Method
- HTML | canvas fill() Method
- HTML | canvas stroke() Method
- HTML | canvas translate() Method
- HTML canvas rotate() Method
- HTML | canvas scale() Method
- HTML | canvas createLinearGradient() Method
- HTML | canvas beginPath() Method
- HTML | canvas quadraticCurveTo() Method
- HTML | canvas moveTo() Method
- HTML | canvas closePath() Method
- HTML | canvas isPointInPath() Method
- HTML | canvas arcTo() Method
- HTML | canvas strokeText() Method
- HTML | canvas arc() Method
- HTML | canvas fillText() Method
- HTML | canvas lineTo() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


