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

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
主站蜘蛛池模板: 乌苏市| 工布江达县| 聂荣县| 内黄县| 夏津县| 昌乐县| 会昌县| 吉安市| 彰化县| 大荔县| 英德市| 山东省| 松桃| 南召县| 西林县| 天门市| 开远市| 日土县| 汽车| 湖口县| 蕉岭县| 襄城县| 祁东县| 姚安县| 永州市| 铁力市| 林西县| 台北县| 山丹县| 扶绥县| 德江县| 鄱阳县| 伊春市| 海安县| 射洪县| 长沙市| 滕州市| 家居| 偃师市| 土默特左旗| 望江县|