summaryrefslogtreecommitdiffstats
path: root/third_party/webkit/PerformanceTests/MotionMark/tests/bouncing-particles/resources/bouncing-canvas-particles.js
blob: 0a76ba05c3d978c84d077a3abd673b19a74581ae (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
BouncingCanvasParticle = Utilities.createSubclass(BouncingParticle,
    function(stage, shape)
    {
        BouncingParticle.call(this, stage);
        this.context = stage.context;
        this._shape = shape;
        this._clip = stage.clip;
    }, {

    applyRotation: function()
    {
        if (this._shape == "circle")
            return;

        this.context.translate(this.size.x / 2, this.size.y / 2);
        this.context.rotate(this.rotater.degree() * Math.PI / 180);
        this.context.translate(-this.size.x / 2, -this.size.y / 2);
    },

    applyClipping: function()
    {
        var clipPoints = BouncingCanvasParticle.clips[this._clip];
        if (!clipPoints)
            return;

        this.context.beginPath();
        clipPoints.forEach(function(point, index) {
            var point = this.size.multiply(point);
            if (!index)
                this.context.moveTo(point.x, point.y);
            else
                this.context.lineTo(point.x, point.y);
        }, this);

        this.context.closePath();
        this.context.clip();
    },

    _draw: function()
    {
        throw "Not implemented";
    },

    animate: function(timeDelta)
    {
        BouncingParticle.prototype.animate.call(this, timeDelta);
        this.context.save();
            this.context.translate(this.position.x, this.position.y);
            this._draw();
        this.context.restore();
    }
});

BouncingCanvasParticle.clips = {
    star: [
        new Point(0.50, 0.00),
        new Point(0.38, 0.38),
        new Point(0.00, 0.38),
        new Point(0.30, 0.60),
        new Point(0.18, 1.00),
        new Point(0.50, 0.75),
        new Point(0.82, 1.00),
        new Point(0.70, 0.60),
        new Point(1.00, 0.38),
        new Point(0.62, 0.38)
    ]
};

BouncingCanvasParticlesStage = Utilities.createSubclass(BouncingParticlesStage,
    function()
    {
        BouncingParticlesStage.call(this);
    }, {

    initialize: function(benchmark, options)
    {
        BouncingParticlesStage.prototype.initialize.call(this, benchmark, options);
        this.context = this.element.getContext("2d");
    },

    animate: function(timeDelta)
    {
        this.context.clearRect(0, 0, this.size.x, this.size.y);
        this.particles.forEach(function(particle) {
            particle.animate(timeDelta);
        });
    }
});