Canvas createPattern() Method

Watch a demo of different values of the createPattern() method.

Or click a specific value to see the result:

createPattern(repeat);
createPattern(repeat-x);
createPattern(repeat-y);
createPattern(no-repeat);
your browser does not support the canvas tag
your browser does not support the canvas tag
your browser does not support the canvas tag
your browser does not support the canvas tag
let c = document.getElementById('myCanvas');
let ctx = c.getContext('2d');
let img=document.createElement('img');
img.src='img_lamp.jpg';
let pat=ctx.createPattern(img,'repeat-x');
ctx.rect(0,0,150,100);
ctx.fillStyle=pat;
ctx.fill();

Play more with the code in our Tryit yourself editor: Try it Yourself ❯