Canvas Save and Restore Method in HTML5

HTML5 Save() and Restore() method

 
The HTML5 canvas provides two important methods to save and restore the canvas states.
 
Let us now define the save() and restore() methods.
 
Each context maintains a stack of drawing states. Drawing states consist of:
  • Current transformation matrix.
  • Current matrix region.
  • The current values of the following attributes: strokeStyle, fillStyle, globalAlpha, lineWidth, lineCap, lineJoin, miterLimit, shadowOffsetX, shadowOffsetY, shadowBlur, shadowColor, globalCompositeOperation, font, textAlign and textBaseline.
When you use the save() method, all the attributes that you define before save() are pushed into a stack. You can push any number of attribute sets onto the stack. You can then use the restore() method to pop each set of attributes out of the stack in the reverse order in which they were pushed. 
 
One of the most common uses of the "save()" and "restore()" methods is for transformations.
 
In the following example I am saving three sets of attributes and then restoring them one by one for every "fillText()" method. When you execute the code, notice that the attributes of the text are applied in reverse order. 
 
Method Description
save() This method pushes the current state onto the stack.
restore() This method pops the top state on the stack, restoring the context to that state.
 
Step 1:
 
First define the first attribute set 1 by setting the fillStyle and shadow properties. Then we call "ctx.save()" that adds our settings to the stack.
  1. //This is attribute set 1  
  2. ctx.font = "25px verdana";  
  3. ctx.fillStyle = "yellow";  
  4. ctx.shadowOffsetX = 3;  
  5. ctx.shadowOffsetY = 3;  
  6. ctx.shadowBlur = 2;  
  7. ctx.shadowColor = "grey";  
  8.    
  9. //Let's push the attributes into the stack. Since this set of attributes is the first one into the stack, it will be the last one out  
  10. ctx.save(); 
Step 2
 
Next,define the second attribute set 2 by setting the fillStyle and shadow properties. Then we call "ctx.save()" that adds our settings to the stack. 
  1. //This is attribute set 2  
  2. var fillColor = ctx.createLinearGradient(100, 10, 200, 10);  
  3. fillColor.addColorStop(0.4, "red");  
  4. fillColor.addColorStop(0.6, "green");  
  5. ctx.fillStyle = fillColor;  
  6. ctx.shadowOffsetX = 3;  
  7. ctx.shadowOffsetY = 3;  
  8. ctx.shadowBlur = 2;  
  9. ctx.shadowColor = "pink";  
  10.    
  11. // Push attribute set 2 into the stack. This will be the second set to be restored.  
  12. ctx.save(); 
Step 3:
 
Next, define the third attribute set 3 by setting the fillStyle and shadow properties. Then we call "ctx.save()" that adds our settings to the stack.
  1. //This is attribute set 3  
  2. ctx.fillStyle = "rgba(0,0,255,0.5)";  
  3. ctx.shadowOffsetX = 3;  
  4. ctx.shadowOffsetY = 3;  
  5. ctx.shadowBlur = 2;  
  6. ctx.shadowColor = "#c8ffc8";  
  7.    
  8. //Push attribute set 3 into the stack. This is the last set we are pushing inside. So it will be the first one to be restored.  
  9. ctx.save(); 
Step 4:
 
The "Restore()" method pops the top state on the stack, restoring the context to that state.
 
Start popping the stack. Restore attribute set 3:
  1. ctx.restore();  
  2. ctx.fillText("Noida", 5, 20);  
  3. Step 5:  
  4. Restore attribute set 2:  
  5.   
  6. ctx.restore();  
  7. ctx.fillText("Noida", 100, 20); 
Step 6:
 
Restore attribute set 1:
  1. ctx.restore();  
  2. ctx.fillText("Noida", 200, 20); 
Example
  1. <!DOCTYPE html>  
  2.    
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <script type="application/javascript">  
  7.         function init() {  
  8.             var canvas = document.getElementById("canvas");  
  9.             if (canvas.getContext) {  
  10.                 var ctx = canvas.getContext("2d");  
  11.    
  12.                 //This is attribute set 1  
  13.                 ctx.font = "25px verdana";  
  14.                 ctx.fillStyle = "yellow";  
  15.                 ctx.shadowOffsetX = 3;  
  16.                 ctx.shadowOffsetY = 3;  
  17.                 ctx.shadowBlur = 2;  
  18.                 ctx.shadowColor = "grey";  
  19.    
  20.                 //Let's push the attributes into the stack. Since this set of attributes is the first one into the stack, it will be the last one out  
  21.                 ctx.save();  
  22.    
  23.                 //This is attribute set 2  
  24.                 var fillColor = ctx.createLinearGradient(100, 10, 200, 10);  
  25.                 fillColor.addColorStop(0.4, "red");  
  26.                 fillColor.addColorStop(0.6, "green");  
  27.                 ctx.fillStyle = fillColor;  
  28.                 ctx.shadowOffsetX = 3;  
  29.                 ctx.shadowOffsetY = 3;  
  30.                 ctx.shadowBlur = 2;  
  31.                 ctx.shadowColor = "pink";  
  32.    
  33.                 // Push attribute set 2 into the stack. This will be the second set to be restored.  
  34.                 ctx.save();  
  35.    
  36.    
  37.                 //This is attribute set 3  
  38.                 ctx.fillStyle = "rgba(0,0,255,0.5)";  
  39.                 ctx.shadowOffsetX = 3;  
  40.                 ctx.shadowOffsetY = 3;  
  41.                 ctx.shadowBlur = 2;  
  42.                 ctx.shadowColor = "#c8ffc8";  
  43.    
  44.                 //Push attribute set 3 into the stack. This is the last set we are pushing inside. So it will be the first one to be restored.  
  45.                 ctx.save();  
  46.    
  47.                 // Start popping the stack. Restore attribute set 3  
  48.                 ctx.restore();  
  49.                 ctx.fillText("Noida", 5, 20);  
  50.    
  51.                 // Restore attribute set 2  
  52.                 ctx.restore();  
  53.                 ctx.fillText("Noida", 100, 20);  
  54.    
  55.                 //Restore attribute set 1  
  56.                 ctx.restore();  
  57.                 ctx.fillText("Noida", 200, 20);  
  58.    
  59.             }  
  60.         }  
  61.     </script>  
  62.     <title>Save and restore Method</title>  
  63. </head>  
  64. <body onload="init();">  
  65.     <canvas id="canvas" width="600" height="300"></canvas>  
  66.     <br>  
  67. </body>  
  68. </html> 
Output 
 
save.jpg

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles