HTML5 > 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');
                ctx.beginPath();
                ctx.strokeStyle = 'red';
                ctx.strokeRect(100, 100, 100, 50);
                ctx.fillStyle = 'blue';
                ctx.fillRect(250, 150, 200, 200);
                ctx.clearRect(300, 200, 50, 50);
            }
 
            mydraw();
 
        </script>
    </body>
</html>
 
 
 

  • strokeRect(x,y,w,h) 線の長方形を描く。x,yが左上隅の座標、w,hが幅と高さ
  • fillStyle 塗りつぶしの色を指定する。色はStyleSheet(CSS)と同じ形式を文字列で指定する。
  • fillRect(x,y,w,h) 塗りつぶしの長方形を描く
  • clearRect(x,y,w,h) 長方形に色を消す

タグ:

+ タグ編集
  • タグ:
最終更新:2016年05月08日 06:38