Advertisement
KaeruCT

nothing

May 25th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var $;
  2. Array.prototype.remove = function (index) {
  3.     this.splice(index, 1);
  4. };
  5.  
  6.  
  7. $(function(){
  8.  
  9.     function Point(x, y) {
  10.         this.x = x;
  11.         this.y = y;
  12.  
  13.         return this;
  14.     }
  15.  
  16.     function Circle(x, y, radius, color) {
  17.         that = new Point(x, y);
  18.         that.max_radius = 48;
  19.         that.radius = radius;
  20.         that.alpha = 1;
  21.         that.color = color;
  22.  
  23.         that.draw = function (canvas) {
  24.  
  25.             if (this.alpha <= 0) {
  26.                 return;
  27.             }
  28.  
  29.             canvas.drawEllipse({
  30.                 fillStyle: "rgba(" + this.color.join(",") + ", " + this.alpha + ")",
  31.                 x: this.x,
  32.                 y: this.y,
  33.                 width: this.radius,
  34.                 height: this.radius,
  35.                 fromCenter: true
  36.             });
  37.  
  38.         }
  39.  
  40.         that.update = function (game) {
  41.  
  42.             if (this.radius < this.max_radius) {
  43.  
  44.                 this.radius += 4;
  45.  
  46.             } else if (this.alpha > 0) {
  47.  
  48.                 this.radius += 4;
  49.                 this.alpha -= 0.05;
  50.             }
  51.  
  52.             console.log(this.alpha);
  53.         }
  54.  
  55.         return that;
  56.     }
  57.  
  58.     var game = {
  59.         circles: [],
  60.         init_radius: 8,
  61.         canvas: null,
  62.  
  63.         init: function () {
  64.             if (game.canvas == null) {
  65.                 var w = $(window).width()/2;
  66.                 var h = $(window).height()/2;
  67.                 game.canvas = $("#game-canvas");
  68.                 game.canvas.css({background: "#000"});
  69.                 game.canvas[0].width = w;
  70.                 game.canvas[0].height = h;
  71.  
  72.                 game.canvas.click(function (e) {
  73.  
  74.                     var x = e.pageX - this.offsetLeft;
  75.                     var y = e.pageY - this.offsetTop;
  76.  
  77.                     game.add_circle(x, y);
  78.                 });
  79.             }
  80.         },
  81.  
  82.         add_circle: function (x, y) {
  83.  
  84.             game.circles.push(new Circle(
  85.                 Math.floor(x - game.init_radius / 2),
  86.                 Math.floor(y - game.init_radius / 2),
  87.                 game.init_radius,
  88.                 game.random_color()));
  89.         },
  90.  
  91.         start: function () {
  92.             setInterval(game.step, 1000 / 30); // 30 fps
  93.             //game.step();
  94.         },
  95.  
  96.         step: function () {
  97.  
  98.             var i;
  99.             for(i = 0; i < game.circles.length; i += 1) {
  100.  
  101.                 game.circles[i].update();
  102.             }
  103.  
  104.             game.draw();
  105.  
  106.  
  107.         },
  108.  
  109.         draw: function () {
  110.             var i;
  111.  
  112.             game.canvas.clearCanvas();
  113.  
  114.             for(i = 0; i < game.circles.length; i += 1) {
  115.  
  116.                 game.circles[i].draw(game.canvas);
  117.  
  118.                 if (game.circles[i].alpha <= 0) {
  119.                     game.circles.remove(i);
  120.                 }
  121.             }
  122.         },
  123.  
  124.         random_color: function () {
  125.  
  126.             var r = Math.floor(127+Math.random()*127);
  127.             var g = Math.floor(127+Math.random()*127);
  128.             var b = Math.floor(127+Math.random()*127);
  129.  
  130.             return [r, g, b];
  131.         }
  132.     };
  133.  
  134.     game.init();
  135.     game.start();
  136.  
  137. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement