HTML Canvas save and restore state
<!DOCTYPE html> <html> <head> <title>Pushing canvas further</title> <meta charset="utf-8"> //from w ww . j a va2 s .c o m <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { var canvas = $("#myCanvas"); var context = canvas.get(0).getContext("2d"); context.fillStyle = "rgb(255, 0, 0)"; context.save(); // Save the canvas state context.fillRect(50, 50, 100, 100); // Red square context.fillStyle = "rgb(0, 0, 255)"; context.save(); context.fillRect(200, 50, 100, 100); // Blue square context.restore(); // Restore the canvas state context.fillRect(350, 50, 100, 100); // Blue square context.restore(); // Restore the canvas state context.fillRect(50, 200, 100, 100); // Red square }); </script> </head> <body> <canvas id="myCanvas" width="500" height="500"> <!-- Insert fallback content here --> </canvas> </body> </html>