Advertisement
JesterMgee

JavaScript Canvas Test

Mar 19th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. JavaScript Animation Canvas Test
  3. Jason Absolom
  4. 19 March 2018
  5.  
  6. A test demonstrating the use of canvas animations in JavaScript.
  7.  
  8. Reference videos:
  9. https://www.youtube.com/watch?v=EO6OkltgudE
  10. https://www.youtube.com/watch?v=83L6B13ixQ0
  11. https://www.youtube.com/watch?v=yq2au9EfeRQ
  12. */
  13.  
  14. //set the canvas size to whole screen. Uses CSS to format the style.
  15. var canvas = document.querySelector('canvas');
  16. canvas.width = window.innerWidth;
  17. canvas.height = window.innerHeight;
  18.  
  19.  
  20. //sets the "getContext" to a simple variable "C" for simplicity
  21. var c = canvas.getContext('2d');
  22.  
  23. //function that draws and animates the circle
  24. function Circle(x, y, dx, dy, radius, colour) {
  25.     //creates local variables for the object.
  26.     //These are passed when calling any function within and used only within this function
  27.     this.x = x;
  28.     this.y = y;
  29.     this.dx = dx;
  30.     this.dy = dy;
  31.     this.radius = radius;
  32.     this.colour = colour;
  33.  
  34.     //draws circle using passed variables
  35.     this.draw = function() {
  36.         //console.log('test');
  37.         //draw circle code used to create each circle
  38.         c.beginPath();                                              //sets a new path for the drawing
  39.         c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);  //creates a circle
  40.         c.strokeStyle = this.colour;                                //set line (stroke) style/colour
  41.         c.stroke();                                                 //draw the outline using previous style
  42.         c.fillStyle = this.colour;                                  //set circle fill style/colour
  43.         c.fill();                                                   //fill circle using previous style
  44.     }
  45.  
  46.     //Updates new position for circle (animates). Called from the animation loop (animate).
  47.     this.update = function() {
  48.        
  49.         //sets the X limits for the animation movement taking into account the radius of the circle
  50.         //If the edge is not reached then move circle based on DX/DY
  51.         //If edge has been reached then invert DX/DY to move the opposite way
  52.         if (this.x + this.radius > innerWidth || this.x - this.radius < 0) {
  53.             this.dx = -this.dx;
  54.         }
  55.         if (this.y + this.radius > innerHeight || this.y - this.radius < 0) {
  56.             this.dy = -this.dy;
  57.         }
  58.  
  59.         //increases the X/Y position of the circle based on the dx/dy amount which emulates velocity
  60.         this.x += this.dx;
  61.         this.y += this.dy;
  62.  
  63.         //redraws the circle in the new position calling the "Draw" function
  64.         this.draw();
  65.     }
  66.  
  67.  
  68. }
  69.  
  70. //create arrays & variables
  71. var circleArray = [];
  72. var j = 0;
  73.  
  74. var colourArray = ["blue", "red", "yellow", "purple", "black", "pink"]; //set colours to be created
  75. var radius = 50; //set size of balls
  76. var speed = 1; //set movement speed
  77. var balls = 30; //set number of balls
  78.  
  79. //generates variables for circles based on above variables
  80. for (var i = 0; i < balls; i++) {
  81.  
  82.     var x = Math.random() * (innerWidth - radius * 2) + radius;     //selects random X/Y position in canvas taking circle radius  
  83.     var y = Math.random() * (innerHeight - radius * 2) + radius;    //into account so circles to not draw half outside canvas
  84.     var dx = (Math.random() - 0.5) * speed;                         //sets a random velocity for X/Y
  85.     var dy = (Math.random() - 0.5) * speed;
  86.    
  87.     //cycles through the "colourArray" and picks sequentially a colour for each circles fill
  88.     colour = colourArray[j];
  89.     j++
  90.     if (j == colourArray.length) {
  91.         j = 0;
  92.     }
  93.  
  94.     //stores all variables in an array
  95.     circleArray.push(new Circle(x, y, dx, dy, radius, colour));
  96. }
  97. //test point to view the circle data for each entry
  98. console.log(circleArray);
  99.  
  100.  
  101. //animation function
  102. function animate() {
  103.     requestAnimationFrame(animate);                 //creates a loop by requesting the same function with every animation frame
  104.     c.clearRect(0, 0, innerWidth, innerHeight);     //clears the canvas on every frame
  105.     for (var i = 0; i < circleArray.length; i++) {  //redraws all circles on canvas from array
  106.         circleArray[i].update();                    //calls "update" function to move and redraw circles
  107.     }
  108.  
  109.  
  110. }
  111.  
  112. //calls animation function above
  113. animate();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement