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
27 if (this._angle >= 0 && this._angle < Math.PI / 2)
28 this._angle = Math.PI - this._angle;
29 // If angle is East-North
30 else if (this._angle > Math.PI / 2 * 3)
31 this._angle = this._angle - (this._angle - Math.PI / 2 * 3) * 2;
34 // If particle is going to move off left side
35 if (this._position.x < 0) {
36 // If angle is West-South
37 if (this._angle > Math.PI / 2 && this._angle < Math.PI)
38 this._angle = Math.PI - this._angle;
39 // If angle is West-North
40 else if (this._angle > Math.PI && this._angle < Math.PI / 2 * 3)
41 this._angle = this._angle + (Math.PI / 2 * 3 - this._angle) * 2;
44 // If particle is going to move off bottom side
45 if (this._position.y + this._size.y > this._stageSize.y) {
46 // If direction is South
47 if (this._angle > 0 && this._angle < Math.PI)
48 this._angle = Math.PI * 2 - this._angle;
51 // If particle is going to move off top side
52 if (this._position.y < 0) {
53 // If direction is North
54 if (this._angle > Math.PI && this._angle < Math.PI * 2)
55 this._angle = this._angle - (this._angle - Math.PI) * 2;
60 function BouncingParticlesAnimator(benchmark)
62 StageAnimator.call(this, benchmark);
65 BouncingParticlesAnimator.prototype = Object.create(StageAnimator.prototype);
66 BouncingParticlesAnimator.prototype.constructor = BouncingParticlesAnimator;
68 function BouncingParticlesStage(element, options)
70 Stage.call(this, element);
72 this.particleSize = new Point(parseInt(options["particleWidth"]) || 10, parseInt(options["particleHeight"]) || 10);
75 this.maxVelocity = parseInt(options["maxVelocity"]) || 500;
76 this.maxVelocity = Math.max(this.maxVelocity, 100);
79 BouncingParticlesStage.prototype = Object.create(Stage.prototype);
80 BouncingParticlesStage.prototype.constructor = BouncingParticlesStage;
82 BouncingParticlesStage.prototype.parseShapeParamters = function(options)
84 this.shape = options["shape"] || "circle";
85 this.fill = options["fill"] || "solid";
86 this.clip = options["clip"] || "";
89 BouncingParticlesStage.prototype.animate = function(timeDelta)
91 this._particles.forEach(function(particle) {
92 particle.animate(timeDelta);
96 BouncingParticlesStage.prototype.tune = function(count)
99 return this._particles.length;
102 console.assert(typeof(this.createParticle) == "function");
103 for (var i = 0; i < count; ++i)
104 this._particles.push(this.createParticle());
105 return this._particles.length;
108 count = Math.min(-count, this._particles.length);
110 if (typeof(this.particleWillBeRemoved) == "function") {
111 for (var i = 0; i < count; ++i)
112 this.particleWillBeRemoved(this._particles[this._particles.length - 1 - i]);
115 this._particles.splice(-count, count);
116 return this._particles.length;
119 function BouncingParticlesBenchmark(suite, test, options, recordTable, progressBar)
121 StageBenchmark.call(this, suite, test, options, recordTable, progressBar);
124 BouncingParticlesBenchmark.prototype = Object.create(StageBenchmark.prototype);
125 BouncingParticlesBenchmark.prototype.constructor = BouncingParticlesBenchmark;
127 BouncingParticlesBenchmark.prototype.createAnimator = function()
129 return new BouncingParticlesAnimator(this);