Exécuter
❯
Exécuter le code
Ctrl+Alt+R
Sauvegarder le code
Ctrl+Alt+A
Changer d'orientation
Ctrl+Alt+O
Changer de thème
Ctrl+Alt+D
<!DOCTYPE html> <html> <body> <h1>HTML5 Canvas</h1> <h2>La méthode createPattern()</h2> <p>Image à utiliser :</p> <img src="../jsref/img_lamp.jpg" id="lamp" width="32" height="32"> <p> <button onclick="draw('repeat')">Répéter</button> <button onclick="draw('repeat-x')">Répéter-x</button> <button onclick="draw('repeat-y')">Repeat-y</button> <button onclick="draw('no-repeat')">Aucun répétition</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>