Canvas States

Last Updated:

Canvas State

Your browser does not support the canvas element.

In this example, the save() method to save the default state and restore() to restore it later, so that you are able to draw a rect with the default state later.


HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <canvas id="point" width="120" height="250"> Your browser does not support the canvas element. </canvas> <script> const canvas = document.getElementById('point'); const ctx = canvas.getContext('2d'); // Save the default state; ctx.fillStyle = '#339933'; ctx.fillRect(10, 10, 100, 100); // Restore the default state ctx.restore(); ctx.fillRect(10, 140, 100, 100); </script> </body> </html>


save() Saves the entire state of the canvas by pushing the current state onto a stack.
restore() Restores the most recently saved canvas state by popping the top entry in the drawing state stack.

Share this Page

Meet the Author