Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h1>HTML5 Canvas</h1> <h2>The createPattern() Method</h2> <p>Image to use:</p> <img src="img_lamp.jpg" id="lamp" width="32" height="32"> <p> <button onclick="draw('repeat')">Repeat</button> <button onclick="draw('repeat-x')">Repeat-x</button> <button onclick="draw('repeat-y')">Repeat-y</button> <button onclick="draw('no-repeat')">No-repeat</button> </p> <canvas id="myCanvas" width="300" height="150" style="background:black;"> </canvas> <script> function draw(direction) { const c = document.getElementById("myCanvas"); const ctx = c.getContext("2d"); ctx.clearRect(0, 0, c.width, c.height); const img = document.getElementById("lamp") const pat = ctx.createPattern(img, direction); ctx.rect(0, 0, 150, 100); ctx.fillStyle = pat; ctx.fill(); } </script> </body> </html>