#### Overview of this book

With the growing popularity of HTML5 Canvas, this book offers tailored recipes to help you develop portable applications, presentations, and games. The recipes are simple yet creative and build on each other. At every step, the book inspires the reader to develop his/her own recipe. From basic to advanced, every aspect of Canvas API has been covered to guide readers to develop their own application, presentation, or game.
Canvas Cookbook
Credits
www.PacktPub.com
Preface
Free Chapter
Paths and Text
Shapes and Composites
Animation
Images and Videos
Interactivity through Events
Creating Graphs and Charts
3D Modeling
Game Development
Interoperability and Deployment
Index

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:

### How to do it...

Here is a simple code to draw a quadratic curve:

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

//call to the function to draw curve

{
ctx.beginPath();
ctx.strokeStyle=color;
ctx.lineJoin="round";
ctx.lineWidth=width;
ctx.moveTo(xStart,yStart);
ctx.stroke();
ctx.closePath();
}
}
</script>
<canvas ID="MyCanvasArea" width="300" height="200" style="border:2px solid black;">
</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:

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