343fa34f8225363d2ae51e66613e240b44e91143
[WebKit-https.git] / PerformanceTests / Animometer / tests / examples / resources / canvas-stars.js
1 function CanvasStar(stage)
2 {
3     this._context = stage.context;
4     this._stageSize = stage.size;
5
6     this._size = stage.randomSquareSize(5, 20);
7     this._center = stage.randomPosition(stage.size.subtract(this._size)).add(this._size.center);
8     this._rotateX = 0;
9     this._rotateDeltaX = stage.random(0.3, 0.7);
10 }
11
12 CanvasStar.prototype._draw = function()
13 {
14     this._context.save();
15     this._context.translate(this._center.x, this._center.y);
16     
17     this._context.fillStyle = 'yellow';
18     this._context.strokeStyle = 'white';
19
20     this._context.lineWidth = 1;
21     
22     this._context.beginPath();
23     this._context.moveTo(                                     0, -this._size.y /  2);
24     this._context.lineTo(+this._size.x / 20 - this._rotateX / 5, -this._size.y / 10);
25     this._context.lineTo(+this._size.x / 4  - this._rotateX,                      0);
26     this._context.lineTo(+this._size.x / 20 - this._rotateX / 5, +this._size.y / 10);
27     this._context.lineTo(                                     0, +this._size.y /  2);
28     this._context.lineTo(-this._size.x / 20 + this._rotateX / 5, +this._size.y / 10);
29     this._context.lineTo(-this._size.x /  4 + this._rotateX,                      0);
30     this._context.lineTo(-this._size.x / 20 + this._rotateX / 5, -this._size.y / 10);
31     this._context.lineTo(                                     0, -this._size.y /  2);
32     
33     this._context.fill();
34     this._context.stroke();
35     this._context.closePath();
36     this._context.restore();
37 }
38
39 CanvasStar.prototype.animate = function(timeDelta)
40 {
41     this._rotateX += this._rotateDeltaX;
42     
43     if (this._rotateX > this._size.x / 4 || this._rotateX < -this._size.x / 4) {
44         this._rotateDeltaX = -this._rotateDeltaX;
45         this._rotateX += this._rotateDeltaX;
46     }
47
48    this._draw();
49 }
50
51 function CanvasStarsStage(element, options)
52 {
53    Stage.call(this, element, options);
54    this.context = this.element.getContext("2d");
55
56     this._objects = [];
57 }
58
59 CanvasStarsStage.prototype = Object.create(Stage.prototype);
60 CanvasStarsStage.prototype.constructor = CanvasStarsStage;
61
62 CanvasStarsStage.prototype.tune = function(count)
63 {
64     count = count > 0 ? Math.floor(count) : Math.ceil(count);
65
66     if (count == 0)
67         return this._objects.length;
68
69     if (count > 0) {
70         for (var i = 0; i < count; ++i)
71             this._objects.push(new CanvasStar(this));
72         return this._objects.length;
73     }
74
75     count = Math.min(-count, this._objects.length);
76     this._objects.splice(-count, count);
77
78     return this._objects.length;
79 }
80
81 CanvasStarsStage.prototype.animate = function(timeDelta)
82 {
83     this._objects.forEach(function(object) {
84         object.animate(timeDelta);
85     });
86 }
87
88 function CanvasStarsAnimator(benchmark)
89 {
90    Animator.call(this, benchmark);
91    this._context = benchmark._stage.context;
92 }
93
94 CanvasStarsAnimator.prototype = Object.create(StageAnimator.prototype);
95 CanvasStarsAnimator.prototype.constructor = CanvasStarsAnimator;
96
97 CanvasStarsAnimator.prototype.animate = function()
98 {
99     this._context.beginPath();
100     this._context.fillStyle = 'black';
101     this._context.rect(0, 0, this._benchmark._stage.size.width, this._benchmark._stage.size.height);
102     this._context.fill();
103    return StageAnimator.prototype.animate.call(this);
104 }
105
106 function CanvasStarsBenchmark(suite, test, options, recordTable, progressBar)
107 {
108    StageBenchmark.call(this, suite, test, options, recordTable, progressBar);
109 }
110
111 CanvasStarsBenchmark.prototype = Object.create(StageBenchmark.prototype);
112 CanvasStarsBenchmark.prototype.constructor = CanvasStarsBenchmark;
113
114 CanvasStarsBenchmark.prototype.createStage = function(element)
115 {
116    // Attach the stage to the benchmark.
117    return new CanvasStarsStage(element, this._options);
118 }
119
120 CanvasStarsBenchmark.prototype.createAnimator = function()
121 {
122    // Attach the animator to the benchmark.
123    return new CanvasStarsAnimator(this);
124 }
125
126 window.benchmarkClient.create = function(suite, test, options, recordTable, progressBar)
127 {
128    // This function is called from the test harness which starts the
129    // test by creating your benchmark object.
130    return new CanvasStarsBenchmark(suite, test, options, recordTable, progressBar);
131 }