1 function Particle(stage)
4 this.rotater = Stage.randomRotater();
13 var randSize = Math.pow(Math.random(), 4) * 25 + 15;
14 this.size = new Point(randSize, randSize);
15 this.maxPosition = this.stage.size.subtract(this.size);
16 this.position = new Point(this.stage.size.x / 2, this.stage.size.y / 4);
18 var angle = Stage.randomInt(0, this.stage.emitSteps) / this.stage.emitSteps * Math.PI * 2 + Date.now()/1000*this.stage.emissionSpin;
19 this._velocity = new Point(Math.sin(angle), Math.cos(angle))
20 .multiply(Stage.random(.8, 1.2));
23 animate: function(timeDelta)
25 this.rotater.next(timeDelta);
27 this.position = this.position.add(this._velocity.multiply(timeDelta));
28 this._velocity.y += 0.03;
30 // If particle is going to move off right side
31 var maxX = this.stage.size.x - this.size.x;
32 if (this.position.x > this.maxPosition.x) {
33 if (this._velocity.x > 0)
34 this._velocity.x *= -1;
35 this.position.x = this.maxPosition.x;
36 } else if (this.position.x < 0) {
37 // If particle is going to move off left side
38 if (this._velocity.x < 0)
39 this._velocity.x *= -1;
43 // If particle is going to move off bottom side
44 if (this.position.y > this.maxPosition.y) {
45 // Adjust direction but maintain magnitude
46 var magnitude = this._velocity.length();
47 this._velocity.x *= 1.5 + .005 * this.size.x;
48 this._velocity = this._velocity.normalize().multiply(magnitude);
49 if (Math.abs(this._velocity.y) < 0.7)
52 if (this._velocity.y > 0)
53 this._velocity.y *= -0.999;
54 this.position.y = this.maxPosition.y;
56 } else if (this.position.y < 0) {
57 // If particle is going to move off top side
58 var magnitude = this._velocity.length();
59 this._velocity.x *= 1.5 + .005 * this.size.x;
60 this._velocity = this._velocity.normalize().multiply(magnitude);
61 if (this._velocity.y < 0)
62 this._velocity.y *= -0.998;
74 ParticlesStage = Utilities.createSubclass(Stage,
81 initialize: function(benchmark, options)
83 Stage.prototype.initialize.call(this, benchmark, options);
84 this.emissionSpin = Stage.random(0, 3);
85 this.emitSteps = Stage.randomInt(4, 6);
88 animate: function(timeDelta)
90 var offset = (Date.now() / 2000) % 1;
91 this.element.style.background = [
102 this.particles.forEach(function(particle) {
103 particle.animate(timeDelta);
107 tune: function(count)
110 return this.particles.length;
113 for (var i = 0; i < count; ++i)
114 this.particles.push(this.createParticle());
115 return this.particles.length;
118 count = Math.min(-count, this.particles.length);
120 if (typeof(this.willRemoveParticle) == "function") {
121 for (var i = 0; i < count; ++i)
122 this.willRemoveParticle(this.particles[i]);
125 this.particles.splice(0, count);
126 return this.particles.length;
129 complexity: function()
131 // We add one to represent the gradient background.
132 return this.particles.length + 1;