HTML5 > Canvasのスケールを変更する

今日の課題:Canvasのスケールを変更する




<!DOCTYPE html>
<html>
    <head>
        <title>HTML5の練習@ヒッキープログラミングスレ</title>
        <style type="text/css">
            body { background-color: silver; }
            canvas { background-color: white; }
        </style>
    </head>
    <body>
        <h4>スケールを変更する</h4>
        <canvas id="mycanvas" width="640" height="480"></canvas>
        <script language="javascript" type="text/javascript">
 
            function mydraw() {
                var canvas = document.getElementById('mycanvas');
                var ctx = canvas.getContext('2d');
                var rad = function (d) { return 2.0 * Math.PI * (d / 360.0) ; };
 
                ctx.beginPath();
                ctx.scale(1.0, 1.0);
                ctx.strokeStyle = 'blue';
                ctx.arc(100, 100, 50, rad(0), rad(360), false);
                ctx.stroke();
 
                ctx.beginPath();
                ctx.scale(2.0, 1.0);
                ctx.strokeStyle = 'red';
                ctx.arc(50, 100, 50, rad(0), rad(360), false);
                ctx.stroke();
                ctx.scale(0.5, 1.0); // スケールを戻しておく(スケールは累積する)
 
                ctx.beginPath();
                ctx.scale(1.0, 2.0);
                ctx.strokeStyle = 'green';
                ctx.arc(100, 50, 50, rad(0), rad(360), false);
                ctx.stroke();
 
            }
 
            mydraw();
 
        </script>
    </body>
</html>
 

  • scale(sx,sy) 現在のX方向スケールをsx倍に、Y方向スケールをsy倍にする
  • 座標や幅や高さなどがスケール倍されて描写される

タグ:

+ タグ編集
  • タグ:
最終更新:2016年05月20日 00:58