1 function BouncingParticle(stage)
3 this._stageSize = stage.size;
4 this.size = stage.particleSize;
6 this.position = Stage.randomPosition(stage.size.subtract(stage.particleSize));
7 this._angle = Stage.randomAngle();
8 this._velocity = Stage.randomVelocity(stage.maxVelocity);
9 this.rotater = Stage.randomRotater();
12 BouncingParticle.prototype =
16 return this.position.add(this.size.center);
19 animate: function(timeDelta)
21 this.position = this.position.move(this._angle, this._velocity, timeDelta);
22 this.rotater.next(timeDelta);
24 // If particle is going to move off right side
25 if (this.position.x + this.size.x > this._stageSize.x) {
26 // If direction is East-South, go West-South.
27 if (this._angle >= 0 && this._angle < Math.PI / 2)
28 this._angle = Math.PI - this._angle;
29 // If angle is East-North, go West-North.
30 else if (this._angle > Math.PI / 2 * 3)
31 this._angle = this._angle - (this._angle - Math.PI / 2 * 3) * 2;
32 // Make sure the particle does not go outside the stage boundaries.
33 this.position.x = this._stageSize.x - this.size.x;
36 // If particle is going to move off left side
37 if (this.position.x < 0) {
38 // If angle is West-South, go East-South.
39 if (this._angle > Math.PI / 2 && this._angle < Math.PI)
40 this._angle = Math.PI - this._angle;
41 // If angle is West-North, go East-North.
42 else if (this._angle > Math.PI && this._angle < Math.PI / 2 * 3)
43 this._angle = this._angle + (Math.PI / 2 * 3 - this._angle) * 2;
44 // Make sure the particle does not go outside the stage boundaries.
48 // If particle is going to move off bottom side
49 if (this.position.y + this.size.y > this._stageSize.y) {
50 // If direction is South, go North.
51 if (this._angle > 0 && this._angle < Math.PI)
52 this._angle = Math.PI * 2 - this._angle;
53 // Make sure the particle does not go outside the stage boundaries.
54 this.position.y = this._stageSize.y - this.size.y;
57 // If particle is going to move off top side
58 if (this.position.y < 0) {
59 // If direction is North, go South.
60 if (this._angle > Math.PI && this._angle < Math.PI * 2)
61 this._angle = this._angle - (this._angle - Math.PI) * 2;
62 // Make sure the particle does not go outside the stage boundaries.
68 BouncingParticlesStage = Utilities.createSubclass(Stage,
75 initialize: function(benchmark, options)
77 Stage.prototype.initialize.call(this, benchmark, options);
78 this.particleSize = new Point(parseInt(options["particleWidth"]) || 10, parseInt(options["particleHeight"]) || 10);
79 this.maxVelocity = Math.max(parseInt(options["maxVelocity"]) || 500, 100);
82 parseShapeParameters: function(options)
84 this.shape = options["shape"] || "circle";
85 this.fill = options["fill"] || "solid";
86 this.clip = options["clip"] || "";
89 animate: function(timeDelta)
91 this.particles.forEach(function(particle) {
92 particle.animate(timeDelta);
102 for (var i = 0; i < count; ++i)
103 this.particles.push(this.createParticle());
107 count = Math.min(-count, this.particles.length);
109 if (typeof(this.particleWillBeRemoved) == "function") {
110 for (var i = 0; i < count; ++i)
111 this.particleWillBeRemoved(this.particles[this.particles.length - 1 - i]);
114 this.particles.splice(-count, count);
117 complexity: function()
119 return this.particles.length;