Canvas States

Canvas State

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.

Example

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.save(); ctx.fillStyle = '#339933'; ctx.fillRect(10, 10, 100, 100); // Restore the default state ctx.restore(); ctx.fillRect(10, 140, 100, 100); </script> </body> </html>

Methods

ValueExplanation
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.

Reminder

Hi Developers, we almost covered 93% of Canvas Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in Canvas.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Meet the Author