I was trying to understand the working of the save
and restore
methods of canvas
and looked at the MDN .
Even though I still did not understand the difference between using them or not.
According to the documentation, save
"saves a default state" and restore
"restores that state later."
But I still can not understand what the use of them did in the example given there in the documentation:
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.save(); // save the default state
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 100, 100);
ctx.restore(); // restore to the default state
ctx.fillRect(150, 75, 100, 100);
<canvas id="canvas"></canvas>
What is the purpose of the restore
and save
method?
How does this state work?