d37c2a2cba500bc45749d4765566070656711fa9
[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     if (count == 0)
65         return this._objects.length;
66
67     if (count > 0) {
68         for (var i = 0; i < count; ++i)
69             this._objects.push(new CanvasStar(this));
70         return this._objects.length;
71     }
72
73     count = Math.min(-count, this._objects.length);
74     this._objects.splice(-count, count);
75
76     return this._objects.length;
77 }
78
79 CanvasStarsStage.prototype.animate = function(timeDelta)
80 {
81     this._objects.forEach(function(object) {
82         object.animate(timeDelta);
83     });
84 }
85
86 function CanvasStarsAnimator(benchmark, options)
87 {
88    Animator.call(this, benchmark, options);
89    this._context = benchmark._stage.context;
90 }
91
92 CanvasStarsAnimator.prototype = Object.create(StageAnimator.prototype);
93 CanvasStarsAnimator.prototype.constructor = CanvasStarsAnimator;
94
95 CanvasStarsAnimator.prototype.animate = function()
96 {
97     this._context.beginPath();
98     this._context.fillStyle = 'black';
99     this._context.rect(0, 0, this._benchmark._stage.size.width, this._benchmark._stage.size.height);
100     this._context.fill();
101    return StageAnimator.prototype.animate.call(this);
102 }
103
104 function CanvasStarsBenchmark(suite, test, options, progressBar)
105 {
106    StageBenchmark.call(this, suite, test, options, progressBar);
107 }
108
109 CanvasStarsBenchmark.prototype = Object.create(StageBenchmark.prototype);
110 CanvasStarsBenchmark.prototype.constructor = CanvasStarsBenchmark;
111
112 CanvasStarsBenchmark.prototype.createStage = function(element)
113 {
114    // Attach the stage to the benchmark.
115    return new CanvasStarsStage(element, this._options);
116 }
117
118 CanvasStarsBenchmark.prototype.createAnimator = function()
119 {
120    // Attach the animator to the benchmark.
121    return new CanvasStarsAnimator(this, this._options);
122 }
123
124 window.benchmarkClient.create = function(suite, test, options, progressBar)
125 {
126    // This function is called from the test harness which starts the
127    // test by creating your benchmark object.
128    return new CanvasStarsBenchmark(suite, test, options, progressBar);
129 }