49556f382eb0d39360809fc761a429467a5ef836
[WebKit-https.git] / PerformanceTests / Animometer / tests / master / resources / particles.js
1 function Particle(stage)
2 {
3     this.stage = stage;
4     this.rotater = Stage.randomRotater();
5     this.reset();
6     this.move();
7 }
8
9 Particle.prototype =
10 {
11     sizeMinimum: 40,
12     sizeRange: 10,
13
14     reset: function()
15     {
16         var randSize = Math.round(Math.pow(Pseudo.random(), 4) * this.sizeRange + this.sizeMinimum);
17         this.size = new Point(randSize, randSize);
18         this.maxPosition = this.stage.size.subtract(this.size);
19     },
20
21     animate: function(timeDelta)
22     {
23         this.rotater.next(timeDelta);
24
25         this.position = this.position.add(this.velocity.multiply(timeDelta));
26         this.velocity.y += 0.03;
27
28         // If particle is going to move off right side
29         if (this.position.x > this.maxPosition.x) {
30             if (this.velocity.x > 0)
31                 this.velocity.x *= -1;
32             this.position.x = this.maxPosition.x;
33         } else if (this.position.x < 0) {
34             // If particle is going to move off left side
35             if (this.velocity.x < 0)
36                 this.velocity.x *= -1;
37             this.position.x = 0;
38         }
39
40         // If particle is going to move off bottom side
41         if (this.position.y > this.maxPosition.y) {
42             // Adjust direction but maintain magnitude
43             var magnitude = this.velocity.length();
44             this.velocity.x *= 1.5 + .005 * this.size.x;
45             this.velocity = this.velocity.normalize().multiply(magnitude);
46             if (Math.abs(this.velocity.y) < 0.7)
47                 this.reset();
48             else {
49                 if (this.velocity.y > 0)
50                     this.velocity.y *= -0.999;
51                 this.position.y = this.maxPosition.y;
52             }
53         } else if (this.position.y < 0) {
54             // If particle is going to move off top side
55             var magnitude = this.velocity.length();
56             this.velocity.x *= 1.5 + .005 * this.size.x;
57             this.velocity = this.velocity.normalize().multiply(magnitude);
58             if (this.velocity.y < 0)
59                 this.velocity.y *= -0.998;
60             this.position.y = 0;
61         }
62
63         this.move();
64     },
65
66     move: function()
67     {
68     }
69 }
70
71 ParticlesStage = Utilities.createSubclass(Stage,
72     function()
73     {
74         Stage.call(this);
75         this.particles = [];
76     }, {
77
78     initialize: function(benchmark)
79     {
80         Stage.prototype.initialize.call(this, benchmark);
81         this.emissionSpin = Stage.random(0, 3);
82         this.emitSteps = Stage.randomInt(4, 6);
83         this.emitLocation = [
84             new Point(this.size.x * .25, this.size.y * .333),
85             new Point(this.size.x * .5, this.size.y * .25),
86             new Point(this.size.x * .75, this.size.y * .333)
87         ];
88     },
89
90     animate: function(timeDelta)
91     {
92         timeDelta /= 4;
93         this.particles.forEach(function(particle) {
94             particle.animate(timeDelta);
95         });
96     },
97
98     tune: function(count)
99     {
100         if (count == 0)
101             return;
102
103         if (count > 0) {
104             for (var i = 0; i < count; ++i)
105                 this.particles.push(this.createParticle());
106             return;
107         }
108
109         count = Math.min(-count, this.particles.length);
110
111         if (typeof(this.willRemoveParticle) == "function") {
112             for (var i = 0; i < count; ++i)
113                 this.willRemoveParticle(this.particles[i]);
114         }
115
116         this.particles.splice(0, count);
117     },
118
119     complexity: function()
120     {
121         // We add one to represent the gradient background.
122         return this.particles.length + 1;
123     }
124 });