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

Drawing arcs

There are three types of curves we can create in canvas—using the arc, quadratic curves, and Bezier curves. Let's get started.

Getting ready

If you recall in Chapter 1, Drawing Shapes in Canvas, in our first recipe we used the arc method to create perfect circles. The arc method is much more than just that. We can actually create any partial curve in a circular shape. If you don't recall drawing circles, I strongly encourage you to scan through Chapter 1, Drawing Shapes in Canvas again, and while you are there, you will find the template for creating the HTML documents as well. We are exclusively going to focus on the JavaScript code in this recipe.

How to do it...

Let's jump into it and create our first noncircle that has curves:

  1. Access the pacman canvas element and fetch its width and height by using the following code snippet:
    var canvas = document.getElementById("pacman");
    var wid = canvas.width;
    var hei = canvas.height;
  2. Create a radian variable (one degree in radians):
    var radian = Math.PI/180;
  3. Get the canvas context and fill its background in black by using the following code snippet:
    var context = canvas.getContext("2d");
      context.fillStyle = "#000000";
      context.fillRect(0,0,wid,hei);
  4. Begin a new path before starting to draw:
      context.beginPath();
  5. Change fill style color:
      context.fillStyle = "#F3F100";
  6. Move the pointer to the center of the screen:
      context.moveTo(wid/2,hei/2);
  7. Draw a curve that starts at 40 degrees and ends at 320 degrees (with a radius of 40) in the center of the screen:
      context.arc(wid / 2, hei / 2, 40, 40*radian, 320*radian, false);
  8. Close the shape by drawing a line back to the starting point of our shape:
      context.lineTo(wid/2,hei/2);
  9. Close the path and fill the shape:
      context.closePath();
      context.fill();

You have just created a PacMan.

How to do it...

For the first time, we take advantage and create a pie-type shape, known as PacMan (you can see how this can be very useful when we get into creating the pie graph). Very simple—again we connect to that idea of radians:

context.arc(wid / 2, hei / 2, 40, 40*radian, 320*radian, false);

Notice how our 4th and 5th parameters—instead of being a complete circle by starting from 0 and ending at 2*Math.PI—are setting the angle to start the arc at radian 40 and end at radian 320 (leaving 80 degrees open to create the mouth of a PacMan ). All that is left is to start drawing from the center of the circle:

context.moveTo(wid/2,hei/2);
context.arc(wid / 2, hei / 2, 40, 40*radian, 320*radian, false);
context.lineTo(wid/2,hei/2);

We start by moving our pointer to the center of our circle. We then create the arc. As our arc isn't a complete shape it's continuing where we left off—drawing a line from the center of the arc to the starting point (40 degrees). We complete the action by drawing a line back to the center of the arc to complete the shape. Now we are ready to fill it and complete our work.

How to do it...

Now that we have got arcs out of the way, you can see how useful this will be for creating pie charts.

主站蜘蛛池模板: 霍邱县| 平昌县| 将乐县| 谷城县| 平昌县| 武定县| 杂多县| 新化县| 同江市| 永吉县| 渭南市| 宁乡县| 崇义县| 莆田市| 玉树县| 武安市| 陵川县| 花垣县| 黄陵县| 洮南市| 营山县| 喀什市| 成安县| 柯坪县| 股票| 余江县| 屏南县| 濮阳县| 清丰县| 绥中县| 陈巴尔虎旗| 翼城县| 农安县| 民勤县| 龙州县| 临邑县| 富锦市| 唐海县| 台东县| 蒲江县| 镇平县|