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 save()</h2> <canvas id="myCanvas" width="300" height="150" style="border:1px solid grey"></canvas> <script> const c = document.getElementById("myCanvas"); const ctx = c.getContext("2d"); // Dessiner ctx.fillStyle = "vert"; ctx.fillRect(10, 10, 50, 50); // Enregistrer l'état ctx.save(); // Dessiner nouveau ctx.fillStyle = "rouge"; ctx.fillRect(100, 10, 50, 50); // Restaurer l'état enregistré ctx.restore(); // Dessiner nouveau ctx.fillRect(200, 10, 50, 50); </script> </body> </html>