Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Canvas Cookbook

You're reading from   Canvas Cookbook Over 80 simple but creative and structured recipes to explore the capabilities of HTML5 Canvas

Arrow left icon
Product type Paperback
Published in May 2019
Publisher
ISBN-13 9781785284892
Length 254 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. Paths and Text FREE CHAPTER 2. Shapes and Composites 3. Animation 4. Images and Videos 5. Interactivity through Events 6. Creating Graphs and Charts 7. 3D Modeling 8. Game Development 9. Interoperability and Deployment Index

Drawing a quadratic curve

In this recipe, you will learn how to draw a quadratic curve. The quadratic curve provides much more flexibility. These curves can be used to create custom shapes in numerous drawings. You will find one implementation in the next recipe:

Drawing a quadratic curve

How to do it...

Here is a simple code to draw a quadratic curve:

<html>
  <head>
    <title>Arcs</title>
    <script>
    function init()
    {
      can  = document.getElementById("MyCanvasArea");
      ctx = can.getContext("2d");

      //call to the function to draw curve
      drawQuadraticCurve(50,100,150,30,250,100,'#df34ef',7);
              
      //function to draw quadratic curve
      function drawQuadraticCurve(xStart,yStart,xControl, yControl, xEnd, yEnd,color,width)
      {
        ctx.beginPath();
        ctx.strokeStyle=color;
        ctx.lineJoin="round";
        ctx.lineWidth=width;
        ctx.moveTo(xStart,yStart);  
        ctx.quadraticCurveTo(xControl, yControl, xEnd, yEnd);  
        ctx.stroke();
        ctx.closePath();
      }
    } 
    </script>
    </head>
    <body onload="init()">
    <canvas ID="MyCanvasArea" width="300" height="200" style="border:2px solid black;">
    your browser doesn't support canvas
    </canvas>
    </body>
</html>

How it works...

The API function is quadraticCurveTo(cpX,cpY,epX,epY).

In this function, cpX and cpY are coordinates of the control point, and epX and epY are coordinates of the end point, the drawing has to start from some point. However, it is not part of this function. You have to move to a point that you want to draw from. This is done by using the moveTo() function.

Refer to the diagram:

How it works...

Observe the points in the diagram. The parameters passed to the quadraticCurveTo() function are coordinates of the control point and end point. Before this function is called you need to call a function moveTo() from where you specify the start/context point.

There's more...

Try the following:

  • Change the control point to (150,150)
  • Change the other coordinates and observe the output
You have been reading a chapter from
Canvas Cookbook
Published in: May 2019
Publisher:
ISBN-13: 9781785284892
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image