summaryrefslogtreecommitdiffstats
path: root/third_party/webkit/PerformanceTests/MotionMark/tests/master/resources/particles.js
blob: cf474e41423888faccde6d304fd024f1be91abdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function Particle(stage)
{
    this.stage = stage;
    this.rotater = Stage.randomRotater();
    this.reset();
    this.move();
}

Particle.prototype =
{
    sizeMinimum: 40,
    sizeRange: 10,

    reset: function()
    {
        var randSize = Math.round(Math.pow(Pseudo.random(), 4) * this.sizeRange + this.sizeMinimum);
        this.size = new Point(randSize, randSize);
        this.minPosition = this.size.center;
        this.maxPosition = this.stage.size.subtract(this.minPosition);
    },

    animate: function(timeDelta)
    {
        this.rotater.next(timeDelta);

        this.position = this.position.add(this.velocity.multiply(timeDelta));
        this.velocity.y += 0.03;

        // If particle is going to move off right side
        if (this.position.x > this.maxPosition.x) {
            if (this.velocity.x > 0)
                this.velocity.x *= -1;
            this.position.x = this.maxPosition.x;
        } else if (this.position.x < this.minPosition.x) {
            // If particle is going to move off left side
            if (this.velocity.x < 0)
                this.velocity.x *= -1;
            this.position.x = this.minPosition.x;
        }

        // If particle is going to move off bottom side
        if (this.position.y > this.maxPosition.y) {
            // Adjust direction but maintain magnitude
            var magnitude = this.velocity.length();
            this.velocity.x *= 1.5 + .005 * this.size.x;
            this.velocity = this.velocity.normalize().multiply(magnitude);
            if (Math.abs(this.velocity.y) < 0.7)
                this.reset();
            else {
                if (this.velocity.y > 0)
                    this.velocity.y *= -0.999;
                this.position.y = this.maxPosition.y;
            }
        } else if (this.position.y < this.minPosition.y) {
            // If particle is going to move off top side
            var magnitude = this.velocity.length();
            this.velocity.x *= 1.5 + .005 * this.size.x;
            this.velocity = this.velocity.normalize().multiply(magnitude);
            if (this.velocity.y < 0)
                this.velocity.y *= -0.998;
            this.position.y = this.minPosition.y;
        }

        this.move();
    },

    move: function()
    {
    }
}

ParticlesStage = Utilities.createSubclass(Stage,
    function()
    {
        Stage.call(this);
        this.particles = [];
    }, {

    animate: function(timeDelta)
    {
        timeDelta /= 4;
        this.particles.forEach(function(particle) {
            particle.animate(timeDelta);
        });
    },

    tune: function(count)
    {
        if (count == 0)
            return;

        if (count > 0) {
            for (var i = 0; i < count; ++i)
                this.particles.push(this.createParticle());
            return;
        }

        count = Math.min(-count, this.particles.length);

        if (typeof(this.willRemoveParticle) == "function") {
            for (var i = 0; i < count; ++i)
                this.willRemoveParticle(this.particles[i]);
        }

        this.particles.splice(0, count);
    },

    complexity: function()
    {
        return this.particles.length;
    }
});