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

Time for action – saving the circle position

  1. Open the untangle.data.js file in the text editor.
  2. Add the following circle object definition code in the JavaScript file:
    untangleGame.Circle = function(x,y,radius){
      this.x = x;
      this.y = y;
      this.radius = radius;
    }
  3. Now we need an array to store the circles' positions. Add a new array to the untangleGame object:
    untangleGame.circles = [];
  4. While drawing every circle in the Canvas, we save the position of the circle in the circles array. Add the following line before calling the drawCircle function, inside the createRandomCircles function:
    untangleGame.circles.push(new untangleGame.Circle(x,y,circleRadius));
  5. After the steps, we should have the following code in the untangle.data.js file:
    if (untangleGame === undefined) {
      var untangleGame = {};
    }
    
    untangleGame.circles = [];
    
    untangleGame.Circle = function(x,y,radius){
      this.x = x;
      this.y = y;
      this.radius = radius;
    };
    
    untangleGame.createRandomCircles = function(width, height) {
      // randomly draw 5 circles
      var circlesCount = 5;
      var circleRadius = 10;
      for (var i=0;i<circlesCount;i++) {
        var x = Math.random()*width;
        var y = Math.random()*height;
        untangleGame.circles.push(new untangleGame.Circle(x,y,circleRadius));
        untangleGame.drawCircle(x, y, circleRadius);
      }
    };
  6. Now we can test the code in the web browser. There is no visual difference between this code and the last example when drawing random circles in the Canvas. This is because we are saving the circles but have not changed any code that affects the appearance. We just make sure it looks the same and there are no new errors.

What just happened?

We saved the position and radius of each circle. This is because Canvas drawing is an immediate mode. We cannot directly access the object drawn in the Canvas because there is no such information. All lines and shapes are drawn on the Canvas as pixels and we cannot access the lines or shapes as inpidual objects. Imagine that we are drawing on a real canvas. We cannot just move a house in an oil painting, and in the same way we cannot directly manipulate any drawn items in the canvas element.

Defining a basic class definition in JavaScript

We can use object-oriented programming in JavaScript. We can define some object structures for our use. The Circle object provides a data structure for us to easily store a collection of x and y positions and the radii.

After defining the Circle object, we can create a new Circle instance with an x, y, and radius value using the following code:

var circle1 = new Circle(100, 200, 10);
Note

For more detailed usage on object-oriented programming in JavaScript, please check out the Mozilla Developer Center at the following link:

https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript

Have a go hero

We have drawn several circles randomly on the Canvas. They are in the same style and of the same size. How about we randomly draw the size of the circles? And fill the circles with different colors? Try modifying the code and then play with the drawing API.

主站蜘蛛池模板: 雅江县| 藁城市| 清流县| 屏东市| 铜陵市| 荔波县| 贵阳市| 叙永县| 江油市| 清远市| 永善县| 喀什市| 深泽县| 桦南县| 磐石市| 大城县| 天峻县| 江孜县| 柳州市| 泰顺县| 东港市| 广德县| 丰台区| 咸阳市| 凉山| 蒙自县| 休宁县| 迁安市| 微山县| 科技| 湖北省| 红桥区| 珲春市| 波密县| 普兰店市| 兴海县| 南通市| 莱西市| 太白县| 老河口市| 容城县|