e8500f3a1c0eefbfab79384eb23654d99bb9051d
[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, options)
61 {
62     StageAnimator.call(this, benchmark, options);
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     if (count == 0)
99         return this._particles.length;
100
101     if (count > 0) {
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;
106     }
107
108     count = Math.min(-count, this._particles.length);
109         
110     if (typeof(this.particleWillBeRemoved) == "function") {
111         for (var i = 0; i < count; ++i)
112             this.particleWillBeRemoved(this._particles[this._particles.length - 1 - i]);
113     }
114
115     this._particles.splice(-count, count);
116     return this._particles.length;
117 }
118
119 function BouncingParticlesBenchmark(suite, test, options, recordTable, progressBar)
120 {
121     StageBenchmark.call(this, suite, test, options, recordTable, progressBar);
122 }
123
124 BouncingParticlesBenchmark.prototype = Object.create(StageBenchmark.prototype);
125 BouncingParticlesBenchmark.prototype.constructor = BouncingParticlesBenchmark;
126
127 BouncingParticlesBenchmark.prototype.createAnimator = function()
128 {
129     return new BouncingParticlesAnimator(this, this._options);
130 }