官术网_书友最值得收藏!

  • HTML5 Canvas Cookbook
  • Eric Rowell
  • 247字
  • 2021-08-27 12:08:04

Drawing a Quadratic curve

In this recipe, we'll learn how to draw a Quadratic curve. Quadratic curves provide much more flexibility and natural curvatures compared to its cousin, the arc, and are an excellent tool for creating custom shapes.

Drawing a Quadratic curve

How to do it...

Follow these steps to draw a Quadratic curve:

  1. Define a 2D canvas context and set the curve style:
    window.onload = function(){
        var canvas = document.getElementById("myCanvas");
        var context = canvas.getContext("2d");
        
        context.lineWidth = 10;
        context.strokeStyle = "black"; // line color
  2. Position the canvas context and draw the Quadratic curve:
    context.moveTo(100, canvas.height - 50);
        context.quadraticCurveTo(canvas.width / 2, -50, canvas.width - 100, canvas.height - 50);
        context.stroke();
    };
  3. Embed the canvas tag inside the body of the HTML document:
    <canvas id="myCanvas" width="600" height="250" style="border:1px solid black;">
    </canvas>

How it works...

HTML5 Quadratic curves are defined by the context point, a control point, and an ending point:

  context.quadraticCurveTo(controlX, controlY, endingPointX,       endingPointY);

Take a look at the following diagram:

How it works...

The curvature of a Quadratic curve is defined by three characteristic tangents. The first part of the curve is tangential to an imaginary line that starts with the context point and ends with the control point. The peak of the curve is tangential to an imaginary line that starts with midpoint 1 and ends with midpoint 2. Finally, the last part of the curve is tangential to an imaginary line that starts with the control point and ends with the ending point.

See also...

  • Putting it all together: Drawing a jet, in Chapter 2
  • Unlocking the power of fractals: Drawing a haunted tree
主站蜘蛛池模板: 民县| 延津县| 浙江省| 隆尧县| 赤壁市| 根河市| 上高县| 普宁市| 迁安市| 桦川县| 抚远县| 通州区| 玉屏| 清水河县| 涟水县| 潮安县| 瑞昌市| 涿州市| 花垣县| 冷水江市| 雷波县| 白玉县| 东安县| 乌拉特中旗| 上杭县| 富民县| 隆子县| 本溪市| 商水县| 叙永县| 富宁县| 金川县| 宣恩县| 苍山县| 南昌市| 凉城县| 舟山市| 达日县| 肥东县| 河池市| 神池县|