ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • CANVAS 연습16(save() && restore())
    게임/HTML 연습 2018. 7. 3. 23:37
    반응형
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <canvas id="myCanvas" width=800 height = 600 style = "border: 1px dotted blue"></canvas>
    <script>
        var canvas = document.getElementById("myCanvas");
        var ctx = canvas.getContext("2d");


     

        ctx.fillStyle = "red";
        ctx.save(); //속성 저장
        ctx.fillRect(50, 100, 100, 100);
        ctx.strokeRect(50, 100, 100, 100);
        ctx.fillStyle = "black";
        ctx.font = "28px black";
        ctx.fillText("1. red", 60, 160);


     

        ctx.fillStyle = "blue";
        ctx.fillRect(300, 100, 100, 100);
        ctx.strokeRect(300, 100, 100, 100);
        ctx.fillStyle = "black";
        ctx.font = "28px black";
        ctx.fillText("2. blue", 305, 160);


     

        ctx.restore(); //저장한 속성 불러옴
        ctx.fillRect(50, 300, 100, 100); //파란색이 아니라 빨간색이다.
        ctx.strokeRect(50, 300, 100, 100);
        ctx.fillStyle = "black";
        ctx.font = "28px black";
        ctx.fillText("3. blue", 60, 360);
     
    </script>
    </body>
    </html>

     

     

    결과 :

     

    반응형
Designed by Tistory.