Html5-canvas-drawing-quadratic

提供:Dev Guides
移動先:案内検索

HTML5 Canvas-二次曲線の描画

キャンバスに2次曲線を描くには、次の方法が必要です-

S.No. Method and Description
1

beginPath()

このメソッドは、現在のパスをリセットします。

2

moveTo(x, y)

このメソッドは、指定されたポイントを持つ新しいサブパスを作成します。

3

closePath()

このメソッドは、現在のサブパスをクローズとしてマークし、新しくクローズされたサブパスの開始および終了と同じポイントで新しいサブパスを開始します。

4

fill()

このメソッドは、サブパスを現在の塗りつぶしスタイルで塗りつぶします。

5

stroke()

このメソッドは、サブパスを現在のストロークスタイルでストロークします。

6

quadraticCurveTo(cpx, cpy, x, y)

このメソッドは、指定されたポイントを現在のパスに追加し、指定されたコントロールポイントを持つ2次ベジェ曲線によって前のパスに接続します。

quadraticCurveTo()メソッドのxおよびyパラメーターは、終点の座標です。 cpxとcpyは、制御点の座標です。

以下は、上記の方法を使用して2次曲線を描く簡単な例です。

<!DOCTYPE HTML>

<html>
   <head>

      <style>
         #test {
            width: 100px;
            height:100px;
            margin: 0px auto;
         }
      </style>

      <script type = "text/javascript">
         function drawShape() {

           //get the canvas element using the DOM
            var canvas = document.getElementById('mycanvas');

           //Make sure we don't execute when canvas isn't supported
            if (canvas.getContext) {

              //use getContext to use the canvas for drawing
               var ctx = canvas.getContext('2d');

              //Draw shapes
               ctx.beginPath();

               ctx.moveTo(75,25);
               ctx.quadraticCurveTo(25,25,25,62.5);

               ctx.quadraticCurveTo(25,100,50,100);
               ctx.quadraticCurveTo(50,120,30,125);

               ctx.quadraticCurveTo(60,120,65,100);
               ctx.quadraticCurveTo(125,100,125,62.5);

               ctx.quadraticCurveTo(125,25,75,25);
               ctx.stroke();
            } else {
               alert('You need Safari or Firefox 1.5+ to see this demo.');
            }
         }
      </script>
   </head>

   <body id = "test" onload = "drawShape();">
      <canvas id = "mycanvas"></canvas>
   </body>

</html>

上記の例では、次の形状を描画します-