68c2f38c20fef6c527f50bdeddcc8181479a0fba
[WebKit-https.git] / PerformanceTests / Animometer / tests / bouncing-particles / resources / bouncing-particles.js
1 function BouncingParticle(stage)
2 {
3     this._stageSize = stage.size;
4     this._size = stage.particleSize;
5     
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();
10 }
11
12 BouncingParticle.prototype =
13 {
14     get center()
15     {
16         return this._position.add(this._size.center);
17     },
18     
19     animate: function(timeDelta)
20     {
21         this._position = this._position.move(this._angle, this._velocity, timeDelta);
22         this._rotater.next(timeDelta);
23
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;
32         }
33         
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;
42         }
43
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;
49         }
50
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;
56         }
57     }
58 }
59
60 function BouncingParticlesAnimator(benchmark)
61 {
62     StageAnimator.call(this, benchmark);
63 };
64
65 BouncingParticlesAnimator.prototype = Object.create(StageAnimator.prototype);
66 BouncingParticlesAnimator.prototype.constructor = BouncingParticlesAnimator;
67
68 function BouncingParticlesStage(element, options)
69 {
70     Stage.call(this, element);
71     
72     this.particleSize = new Point(parseInt(options["particleWidth"]) || 10, parseInt(options["particleHeight"]) || 10);
73     this._particles = [];
74
75     this.maxVelocity = parseInt(options["maxVelocity"]) || 500;
76     this.maxVelocity = Math.max(this.maxVelocity, 100);
77 }
78
79 BouncingParticlesStage.prototype = Object.create(Stage.prototype);
80 BouncingParticlesStage.prototype.constructor = BouncingParticlesStage;
81
82 BouncingParticlesStage.prototype.parseShapeParamters = function(options)
83 {
84     this.shape = options["shape"] || "circle";
85     this.fill = options["fill"] || "solid";
86     this.clip = options["clip"] || "";
87 }
88
89 BouncingParticlesStage.prototype.animate = function(timeDelta)
90 {
91     this._particles.forEach(function(particle) {
92         particle.animate(timeDelta);
93     });
94 }
95     
96 BouncingParticlesStage.prototype.tune = function(count)
97 {
98     count = count > 0 ? Math.floor(count) : Math.ceil(count);
99
100     if (count == 0)
101         return this._particles.length;
102
103     if (count > 0) {
104         console.assert(typeof(this.createParticle) == "function");
105         for (var i = 0; i < count; ++i)
106             this._particles.push(this.createParticle());
107         return this._particles.length;
108     }
109
110     count = Math.min(-count, this._particles.length);
111         
112     if (typeof(this.particleWillBeRemoved) == "function") {
113         for (var i = 0; i < count; ++i)
114             this.particleWillBeRemoved(this._particles[this._particles.length - 1 - i]);
115     }
116
117     this._particles.splice(-count, count);
118     return this._particles.length;
119 }
120
121 function BouncingParticlesBenchmark(suite, test, options, recordTable, progressBar)
122 {
123     StageBenchmark.call(this, suite, test, options, recordTable, progressBar);
124 }
125
126 BouncingParticlesBenchmark.prototype = Object.create(StageBenchmark.prototype);
127 BouncingParticlesBenchmark.prototype.constructor = BouncingParticlesBenchmark;
128
129 BouncingParticlesBenchmark.prototype.createAnimator = function()
130 {
131     return new BouncingParticlesAnimator(this);
132 }