Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var $;
- Array.prototype.remove = function (index) {
- this.splice(index, 1);
- };
- $(function(){
- function Point(x, y) {
- this.x = x;
- this.y = y;
- return this;
- }
- function Circle(x, y, radius, color) {
- that = new Point(x, y);
- that.max_radius = 48;
- that.radius = radius;
- that.alpha = 1;
- that.color = color;
- that.draw = function (canvas) {
- if (this.alpha <= 0) {
- return;
- }
- canvas.drawEllipse({
- fillStyle: "rgba(" + this.color.join(",") + ", " + this.alpha + ")",
- x: this.x,
- y: this.y,
- width: this.radius,
- height: this.radius,
- fromCenter: true
- });
- }
- that.update = function (game) {
- if (this.radius < this.max_radius) {
- this.radius += 4;
- } else if (this.alpha > 0) {
- this.radius += 4;
- this.alpha -= 0.05;
- }
- console.log(this.alpha);
- }
- return that;
- }
- var game = {
- circles: [],
- init_radius: 8,
- canvas: null,
- init: function () {
- if (game.canvas == null) {
- var w = $(window).width()/2;
- var h = $(window).height()/2;
- game.canvas = $("#game-canvas");
- game.canvas.css({background: "#000"});
- game.canvas[0].width = w;
- game.canvas[0].height = h;
- game.canvas.click(function (e) {
- var x = e.pageX - this.offsetLeft;
- var y = e.pageY - this.offsetTop;
- game.add_circle(x, y);
- });
- }
- },
- add_circle: function (x, y) {
- game.circles.push(new Circle(
- Math.floor(x - game.init_radius / 2),
- Math.floor(y - game.init_radius / 2),
- game.init_radius,
- game.random_color()));
- },
- start: function () {
- setInterval(game.step, 1000 / 30); // 30 fps
- //game.step();
- },
- step: function () {
- var i;
- for(i = 0; i < game.circles.length; i += 1) {
- game.circles[i].update();
- }
- game.draw();
- },
- draw: function () {
- var i;
- game.canvas.clearCanvas();
- for(i = 0; i < game.circles.length; i += 1) {
- game.circles[i].draw(game.canvas);
- if (game.circles[i].alpha <= 0) {
- game.circles.remove(i);
- }
- }
- },
- random_color: function () {
- var r = Math.floor(127+Math.random()*127);
- var g = Math.floor(127+Math.random()*127);
- var b = Math.floor(127+Math.random()*127);
- return [r, g, b];
- }
- };
- game.init();
- game.start();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement