Update master benchmark with SVG test
[WebKit-https.git] / PerformanceTests / Animometer / tests / master / resources / svg-particles.js
1 (function() {
2
3 SVGParticle = Utilities.createSubclass(Particle,
4     function(stage)
5     {
6         var shapeId = "#shape-" + Stage.randomInt(1, stage.particleTypeCount);
7         this.isClipPath = Stage.randomBool();
8         if (this.isClipPath) {
9             this.element = Utilities.createSVGElement("rect", {
10                 x: 0,
11                 y: 0,
12                 "clip-path": "url(" + shapeId + ")"
13             }, {}, stage.element);
14         } else {
15             var shapePath = document.querySelector(shapeId + " path");
16             this.element = shapePath.cloneNode();
17             stage.element.appendChild(this.element);
18         }
19
20         this.gradient = document.getElementById("default-gradient").cloneNode(true);
21         this.gradient.id = "gradient-" + stage.gradientsCounter++;
22         stage.gradientsDefs.appendChild(this.gradient);
23         this.element.setAttribute("fill", "url(#" + this.gradient.id + ")");
24
25         Particle.call(this, stage);
26     }, {
27
28     sizeMinimum: 30,
29     sizeRange: 40,
30
31     reset: function()
32     {
33         Particle.prototype.reset.call(this);
34
35         this.position = Stage.randomElementInArray(this.stage.emitLocation);
36
37         var velocityMagnitude = Stage.random(.5, 2.5);
38         var angle = Stage.randomInt(0, this.stage.emitSteps) / this.stage.emitSteps * Math.PI * 2 + Stage.dateCounterValue(1000) * this.stage.emissionSpin + velocityMagnitude;
39         this.velocity = new Point(Math.sin(angle), Math.cos(angle))
40             .multiply(velocityMagnitude);
41
42         if (this.isClipPath) {
43             this.element.setAttribute("width", this.size.x);
44             this.element.setAttribute("height", this.size.y);
45             this.transformSuffix = "translate(-" + (this.size.x >> 1) + "px,-" + (this.size.y >> 1) + "px)";
46         } else
47             this.transformSuffix = "scale(" + this.size.x + ")translate(-.5px,-.5px)";
48
49         this.stage.colorOffset = (this.stage.colorOffset + .5) % 360;
50
51         var transform = this.stage.element.createSVGTransform();
52         transform.setRotate(Stage.randomInt(0, 359), 0, 0);
53         this.gradient.gradientTransform.baseVal.initialize(transform);
54
55         var stops = this.gradient.querySelectorAll("stop");
56         stops[0].setAttribute("stop-color", "hsl(" + this.stage.colorOffset + ", 70%, 45%)");
57         stops[1].setAttribute("stop-color", "hsl(" + ((this.stage.colorOffset + Stage.randomInt(50,100)) % 360) + ", 70%, 65%)");
58     },
59
60     move: function()
61     {
62         this.element.style.transform = "translate(" + this.position.x + "px," + this.position.y + "px)" + this.rotater.rotateZ() + this.transformSuffix;
63     }
64 });
65
66 SVGParticleStage = Utilities.createSubclass(ParticlesStage,
67     function()
68     {
69         ParticlesStage.call(this);
70     }, {
71
72     initialize: function(benchmark)
73     {
74         ParticlesStage.prototype.initialize.call(this, benchmark);
75         this.emissionSpin = Stage.random(0, 3);
76         this.emitSteps = Stage.randomInt(4, 6);
77         this.emitLocation = [
78             new Point(this.size.x * .25, this.size.y * .333),
79             new Point(this.size.x * .5, this.size.y * .25),
80             new Point(this.size.x * .75, this.size.y * .333)
81         ];
82         this.colorOffset = Stage.randomInt(0, 359);
83
84         this.particleTypeCount = document.querySelectorAll(".shape").length;
85         this.gradientsDefs = document.getElementById("gradients");
86         this.gradientsCounter = 0;
87     },
88
89     createParticle: function()
90     {
91         return new SVGParticle(this);
92     },
93
94     willRemoveParticle: function(particle)
95     {
96         particle.element.remove();
97         if (particle.gradient)
98             particle.gradient.remove();
99     }
100 });
101
102 SVGParticleBenchmark = Utilities.createSubclass(Benchmark,
103     function(options)
104     {
105         Benchmark.call(this, new SVGParticleStage(), options);
106     }
107 );
108
109 window.benchmarkClass = SVGParticleBenchmark;
110
111 })();