Introduction

2Main.png
In my previous article, I explained canvas elements, compatibility of browsers and drawing lines. Now in this article we learn about the use of the arc method, quadratic and bezier curves and creating various shapes and so on.

Arc

In HTML5 Canvas, we can draw an arc using the "arc()" method. The arc() method requires the following six parameters:
Now, we know what we need for the arc() method. It now looks like "arc(x, y, radius, startAngle, endAngle, anticlockwise)". We can use the "lineWidth", "lineCap" and "strokeStyle" properties to style an arc.
arc2.png
Example
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <!-- Draw a Canvas -->
  5. <canvas id="drawCanvas" width="600" height="500"></canvas>
  6. <script>
  7. var drawCanvas = document.getElementById('drawCanvas');
  8. var ctx = drawCanvas.getContext('2d');
  9. ctx.beginPath();
  10. ctx.arc(300, 200, 100, -1.2, Math.PI * 0.4 , true);
  11. ctx.lineWidth = 15;
  12. ctx.strokeStyle = 'blue';
  13. ctx.stroke();
  14. </script>
  15. </div>
  16. </form>
  17. </body>
Output:
Capture7.PNG

Bezier Path

Creating arc's is really fun but they have their limitations. And, if you want to create anything more complex then we need to use two other curves:
To create a bezier path we need to use control points that define where and how to draw the curves. The Quadratic Curve method has one control point whereas the bezier curve method has two control points. Now let's check out these two methods.

Quadratic Curve

In HTML5 Canvas, we can create a quadratic curve using the "quadraticCurveTo()" method. To draw a quadratic curve, we need four main parameters:
Now it looks like "quadraticCurveTo(cpx, cpy, x, y)" and to style a quadratic curve we can use "lineWidth", "lineCap" and "strokeStyle" properties.
quadratic2.png
Example
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <!-- Draw a Canvas -->
  5. <canvas id="drawCanvas" width="600" height="500"></canvas>
  6. <script>
  7. var drawCanvas = document.getElementById('drawCanvas');
  8. var ctx = drawCanvas.getContext('2d');
  9. ctx.beginPath();
  10. ctx.moveTo(150,160);
  11. // ctx.quadraticCurveTo(cpx, cpy, x, y);
  12. ctx.quadraticCurveTo(280, 0, 400, 160);
  13. ctx.lineWidth = 15;
  14. ctx.strokeStyle = '#00f';
  15. ctx.stroke();
  16. </script>
  17. </div>
  18. </form>
  19. </body>
Output
quadraticExample.PNG

Bezier Curve

In HTML5 Canvas, we can create a bezier curve using the "bezierCurveTo()" method. It allows the creation of more complex curves than a quadratic curve. Bezier curves have a context point, two control points, and an ending point. Now to draw a bezier curve, we need six main parameters:
Now we have our bezier curve method that looks like quadraticCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) and to style a bezier curve we can use lineWidth, lineCap and strokeStyle properties.
beziercurve.png
Example
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <!-- Draw a Canvas -->
  5. <canvas id="drawCanvas" width="600" height="500"></canvas>
  6. <script>
  7. var drawCanvas = document.getElementById('drawCanvas');
  8. var ctx = drawCanvas.getContext('2d');
  9. ctx.beginPath();
  10. ctx.moveTo(75,150);
  11. // ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
  12. ctx.bezierCurveTo(200, 400, 200, 20, 400, 200);
  13. ctx.lineWidth = 15;
  14. ctx.strokeStyle = '#00f';
  15. ctx.stroke();
  16. </script>
  17. </div>
  18. </form>
  19. </body>
  20. </html>
Output
bezierCurveExample.PNG

SHAPES

In HTML5 we can create various shapes, like rectangle, circle, semicircle or even custom shapes. Now, we learn how to create various types of shapes.

Rectangle

Now, to draw a rectangle we can use a "rect()" or "fillRect()" method that can create the required shape. If you use the "rect()" method then it will create a rectangle without color but if we use "fillRect()" then it will create a rectangle with a color. We need four parameters to call a "rect()" or "fillRect()" method:
Example
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <!-- Draw a Canvas -->
  5. <canvas id="drawCanvas" width="600" height="500"></canvas>
  6. <script>
  7. var drawCanvas = document.getElementById('drawCanvas');
  8. var ctx = drawCanvas.getContext('2d');
  9. ctx.beginPath();
  10. ctx.rect(100, 100, 100, 100);
  11. ctx.lineWidth = 8;
  12. ctx.strokeStyle = "red";
  13. ctx.closePath();
  14. ctx.fillRect(300, 100, 100, 100); //By default Black
  15. ctx.closePath();
  16. ctx.stroke();
  17. </script>
  18. </div>
  19. </form>
  20. </body>
Output
Capture3.PNG

Circle

Now, to draw a full arc or a circle we can use the "arc()" method by defining a starting angle as 0 and ending angle as PI * 2.
Image
Example
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <!-- Draw a Canvas -->
  5. <canvas id="drawCanvas" width="600" height="500"></canvas>
  6. <script>
  7. var drawCanvas = document.getElementById('drawCanvas');
  8. var ctx = drawCanvas.getContext('2d');
  9. ctx.beginPath();
  10. // ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, true);
  11. ctx.arc(300, 200, 100, 0, Math.PI * 2, false);
  12. ctx.fillStyle = 'red';
  13. ctx.fill();
  14. ctx.lineWidth = 6;
  15. ctx.strokeStyle = '#00f';
  16. ctx.stroke();
  17. </script>
  18. </div>
  19. </form>
  20. </body>
Output
Capture4.PNG

Semi-Circle

Creating a semicircle in HTML5 canvas is the same as creating a circle. We can use the "arc()" method but here we need to define the ending angle has "PI+startAngle".
Example
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <!-- Draw a Canvas -->
  5. <canvas id="drawCanvas" width="600" height="500"></canvas>
  6. <script>
  7. var drawCanvas = document.getElementById('drawCanvas');
  8. var ctx = drawCanvas.getContext('2d');
  9. ctx.beginPath();
  10. ctx.arc(300, 200, 100, 0, Math.PI, true);
  11. ctx.fillStyle = 'red';
  12. ctx.fill();
  13. ctx.lineWidth = 6;
  14. ctx.strokeStyle = '#00f';
  15. ctx.stroke();
  16. </script>
  17. </div>
  18. </form>
  19. </body>
Output
Capture5.PNG

Custom Shapes

This is one of my favorite parts, we can create whatever we want using custom shapes in HTML5 Canvas. We can use methods like "arcTo()", "lineTo()", "bezierCurveTo()" or "quadraticCurveTo()" to build each subpath that creates our shapes. Let us see an example:
Image
Example
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <!-- Draw a Canvas -->
  5. <canvas id="drawCanvas" width="600" height="500"></canvas>
  6. <script>
  7. var drawCanvas = document.getElementById('drawCanvas');
  8. var ctx = drawCanvas.getContext('2d');
  9. ctx.beginPath();
  10. ctx.moveTo(100, 110);
  11. ctx.bezierCurveTo(100, 50, 130, 130, 250, 220);
  12. ctx.bezierCurveTo(150, 140, 320, 230, 310, 100);
  13. ctx.bezierCurveTo(200, 100, 420, 110, 400, 150);
  14. ctx.bezierCurveTo(250, 110, 370, 40, 350, 70);
  15. ctx.bezierCurveTo(300, 5, 150, 30, 260, 40);
  16. ctx.bezierCurveTo(350, 5, 50, 30, 180, 80);
  17. ctx.closePath();
  18. ctx.lineWidth = 10;
  19. ctx.strokeStyle = 'green';
  20. ctx.stroke();
  21. </script>
  22. </div>
  23. </form> </body>
Output
custom Image.PNG