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

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
主站蜘蛛池模板: 华安县| 永靖县| 台前县| 新竹市| 油尖旺区| 景谷| 虎林市| 苗栗县| 柳林县| 陇川县| 扶沟县| 秦安县| 五指山市| 安远县| 任丘市| 全椒县| 新兴县| 谢通门县| 三河市| 长寿区| 莱州市| 油尖旺区| 乐山市| 徐闻县| 临清市| 曲阜市| 定襄县| 红原县| 忻城县| 镇远县| 威海市| 杭州市| 怀远县| 密山市| 杭锦后旗| 明光市| 武邑县| 岱山县| 陆川县| 万宁市| 双辽市|