HTML Canvas State save and restore

Description

HTML Canvas State save and restore

View in separate window

<html>
    <head>
        <script> 
            window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
                /*w w  w. ja v a  2  s . com*/
                var rectWidth = 150;
                var rectHeight = 75;
                
                context.save(); // save state 1
                context.translate(canvas.width / 2, canvas.height / 2);
                
                context.save(); // save state 2
                context.rotate(Math.PI / 4);
                
                context.save(); // save state 3
                context.scale(2, 2);
                
                context.fillStyle = "blue";
                context.fillRect(-rectWidth / 2, -rectHeight / 2, rectWidth, rectHeight);
                
                context.restore(); // restore state 3
                context.fillStyle = "red";
                context.fillRect(-rectWidth / 2, -rectHeight / 2, rectWidth, rectHeight);
                
                context.restore(); // restore state 2
                context.fillStyle = "yellow";
                context.fillRect(-rectWidth / 2, -rectHeight / 2, rectWidth, rectHeight);
                
                context.restore(); // restore state 1
                context.fillStyle = "green";
                context.fillRect(-rectWidth / 2, -rectHeight / 2, rectWidth, rectHeight);
            };
        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="600" height="250" style="border:1px solid black;">
        </canvas>
    </body>
</html>



PreviousNext

Related