Get rid of options member variable in Benchmark.
[WebKit-https.git] / PerformanceTests / Animometer / tests / misc / resources / canvas-stars.js
1 (function() {
2
3 function CanvasStar(stage)
4 {
5     this._context = stage.context;
6
7     this._size = Stage.randomSquareSize(5, 20);
8     this._center = Stage.randomPosition(stage.size.subtract(this._size)).add(this._size.center);
9     this._rotateX = 0;
10     this._rotateDeltaX = Stage.random(0.3, 0.7);
11 }
12
13 CanvasStar.prototype = {
14     _draw: function()
15     {
16         this._context.save();
17         this._context.translate(this._center.x, this._center.y);
18
19         this._context.fillStyle = 'yellow';
20         this._context.strokeStyle = 'white';
21
22         this._context.lineWidth = 1;
23
24         this._context.beginPath();
25         this._context.moveTo(                                     0, -this._size.y /  2);
26         this._context.lineTo(+this._size.x / 20 - this._rotateX / 5, -this._size.y / 10);
27         this._context.lineTo(+this._size.x / 4  - this._rotateX,                      0);
28         this._context.lineTo(+this._size.x / 20 - this._rotateX / 5, +this._size.y / 10);
29         this._context.lineTo(                                     0, +this._size.y /  2);
30         this._context.lineTo(-this._size.x / 20 + this._rotateX / 5, +this._size.y / 10);
31         this._context.lineTo(-this._size.x /  4 + this._rotateX,                      0);
32         this._context.lineTo(-this._size.x / 20 + this._rotateX / 5, -this._size.y / 10);
33         this._context.lineTo(                                     0, -this._size.y /  2);
34
35         this._context.fill();
36         this._context.stroke();
37         this._context.closePath();
38         this._context.restore();
39     },
40
41     animate: function(timeDelta)
42     {
43         this._rotateX += this._rotateDeltaX;
44
45         if (this._rotateX > this._size.x / 4 || this._rotateX < -this._size.x / 4) {
46             this._rotateDeltaX = -this._rotateDeltaX;
47             this._rotateX += this._rotateDeltaX;
48         }
49
50         this._draw();
51     }
52 };
53
54 CanvasStarsStage = Utilities.createSubclass(Stage,
55     function()
56     {
57         Stage.call(this);
58         this._objects = [];
59     }, {
60
61     initialize: function(benchmark, options)
62     {
63         Stage.prototype.initialize.call(this, benchmark, options);
64         this.context = this.element.getContext("2d");
65     },
66
67     tune: function(count)
68     {
69         if (count == 0)
70             return this._objects.length;
71
72         if (count > 0) {
73             for (var i = 0; i < count; ++i)
74                 this._objects.push(new CanvasStar(this));
75             return this._objects.length;
76         }
77
78         count = Math.min(-count, this._objects.length);
79         this._objects.splice(-count, count);
80
81         return this._objects.length;
82     },
83
84     animate: function(timeDelta)
85     {
86         this.context.beginPath();
87         this.context.fillStyle = 'black';
88         this.context.rect(0, 0, this.size.width, this.size.height);
89         this.context.fill();
90
91         this._objects.forEach(function(object) {
92             object.animate(timeDelta);
93         });
94     },
95
96     complexity: function()
97     {
98         return this._objects.length;
99     }
100 });
101
102 CanvasStarsBenchmark = Utilities.createSubclass(Benchmark,
103     function(options)
104     {
105         Benchmark.call(this, new CanvasStarsStage(), options);
106     }
107 );
108
109 window.benchmarkClass = CanvasStarsBenchmark;
110
111 })();