Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.83 KB | None | 0 0
  1.  
  2. <canvas width="300" height="300"></canvas>
  3. <script>
  4.  
  5.     var canvas = document.querySelector('canvas'),
  6.         ctx = canvas.getContext('2d');
  7.    
  8.     var Bullet = function (x, y, vx, vy) {
  9.         this.x = x;
  10.         this.y = y;
  11.         this.vx = vx;
  12.         this.vy = vy;      
  13.     };
  14.    
  15.     Bullet.prototype.tick = function (dt) {
  16.         this.x += dt * this.vx;
  17.         this.y += dt * this.vy;
  18.     }
  19.    
  20.     Bullet.prototype.draw = function (ctx) {
  21.         ctx.beginPath();
  22.         ctx.arc(this.x, this.y, 10, 0, 2*Math.PI);
  23.         ctx.stroke();
  24.     }
  25.    
  26.     var bullets = [];
  27.     bullets.push(new Bullet(0, 150, 200, 40));
  28.     bullets.push(new Bullet(0, 150, 200, 0));
  29.     bullets.push(new Bullet(0, 150, 200, -40));
  30.    
  31.     window.setInterval(function () {
  32.         ctx.clearRect(0, 0, canvas.width, canvas.height);
  33.         bullets.forEach(function (b) {
  34.             b.tick(1 / 60);
  35.             b.draw(ctx);
  36.         });
  37.     }, 1000 / 60);
  38.  
  39. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement