Clean referencing the options object in the graphics benchmark
[WebKit-https.git] / PerformanceTests / Animometer / tests / examples / resources / canvas-electrons.js
1 function CanvasElectron(stage)
2 {
3     this._context = stage.context;
4     this._stageSize = stage.size;
5
6     var minSide = Math.min(this._stageSize.width, this._stageSize.height);
7     var radiusX = stage.random(minSide / 8, 7 * minSide / 16);
8     var radiusY = stage.random(minSide / 8, 3 * radiusX / 4);
9     this._orbitRadiuses = new Point(radiusX, radiusY);
10     this._radius = stage.random(5, 15);
11     this._direction = stage.randomInt(0, 2);
12     this._angle = stage.randomInt(0, 360);
13     this._color = stage.randomColor();
14     this._rotater = stage.randomRotater();
15     this._rotater.next(stage.random(0, this._rotater.interval));
16 }
17
18 CanvasElectron.prototype._draw = function()
19 {
20     // Calculate the position of the object on the ellipse.
21     var angle = this._direction ? this._rotater.degree() : 360 - this._rotater.degree();
22     var position = this._stageSize.center.subtract(Point.pointOnEllipse(angle, this._orbitRadiuses));
23
24     this._context.save();    
25         this._context.translate(this._stageSize.center.x, this._stageSize.center.y);
26         this._context.rotate(this._angle * Math.PI / 180);
27         this._context.translate(-this._stageSize.center.x, -this._stageSize.center.y);
28
29         // Set the stroke and the fill colors
30         this._context.strokeStyle = "rgba(192, 192, 192, 0.9)";
31         this._context.fillStyle = this._color;
32
33         // Draw the orbit of the object.
34         this._context.beginPath();
35         this._context.ellipse(this._stageSize.center.x, this._stageSize.center.y, this._orbitRadiuses.x, this._orbitRadiuses.y, 0, 0, 2 * Math.PI);
36         this._context.stroke();
37     
38         // Draw the object.
39         this._context.beginPath();
40         this._context.arc(position.x, position.y, this._radius, 0, Math.PI * 2, true);
41         this._context.fill();
42     this._context.restore();
43 }
44
45 CanvasElectron.prototype.animate = function(timeDelta)
46 {
47     this._rotater.next(timeDelta / 100);
48     this._draw();
49 }
50
51 function CanvasElectronsStage(element, options)
52 {
53     Stage.call(this, element, options);
54     this.context = this.element.getContext("2d");
55     this._electrons = [];
56 }
57
58 CanvasElectronsStage.prototype = Object.create(Stage.prototype);
59 CanvasElectronsStage.prototype.constructor = CanvasElectronsStage;
60
61 CanvasElectronsStage.prototype.tune = function(count)
62 {
63     if (count == 0)
64         return this._electrons.length;
65
66     if (count > 0) {
67         for (var i = 0; i < count; ++i)
68             this._electrons.push(new CanvasElectron(this));
69         return this._electrons.length;
70     }
71
72     count = Math.min(-count, this._electrons.length);
73     this._electrons.splice(-count, count);
74     
75     return this._electrons.length;
76 }
77
78 CanvasElectronsStage.prototype.animate = function(timeDelta)
79 {
80     this._electrons.forEach(function(electron) {
81         electron.animate(timeDelta);
82     });
83 }
84
85 function CanvasElectronsAnimator(benchmark, options)
86 {
87     Animator.call(this, benchmark, options);
88     this._context = benchmark._stage.context;
89 }
90
91 CanvasElectronsAnimator.prototype = Object.create(StageAnimator.prototype);
92 CanvasElectronsAnimator.prototype.constructor = CanvasElectronsAnimator;
93
94 CanvasElectronsAnimator.prototype.animate = function()
95 {
96     this._context.clearRect(0, 0, this._benchmark._stage.size.x, this._benchmark._stage.size.y);
97
98     // Draw a big star in the middle.
99     this._context.fillStyle = "orange";    
100     this._context.beginPath();
101     this._context.arc(this._benchmark._stage.size.center.x, this._benchmark._stage.size.center.y, 50, 0, Math.PI * 2, true);
102     this._context.fill();
103
104     return StageAnimator.prototype.animate.call(this);
105 }
106
107 function CanvasElectronsBenchmark(suite, test, options, recordTable, progressBar)
108 {
109     StageBenchmark.call(this, suite, test, options, recordTable, progressBar);
110 }
111
112 CanvasElectronsBenchmark.prototype = Object.create(StageBenchmark.prototype);
113 CanvasElectronsBenchmark.prototype.constructor = CanvasElectronsBenchmark;
114
115 CanvasElectronsBenchmark.prototype.createStage = function(element)
116 {
117     // Attach the stage to the benchmark.
118     return new CanvasElectronsStage(element, this._options);
119 }
120
121 CanvasElectronsBenchmark.prototype.createAnimator = function()
122 {
123     // Attach the animator to the benchmark.
124     return new CanvasElectronsAnimator(this, this._options);
125 }
126
127 window.benchmarkClient.create = function(suite, test, options, recordTable, progressBar)
128 {
129     // This function is called from the test harness which starts the
130     // test by creating your benchmark object.
131     return new CanvasElectronsBenchmark(suite, test, options, recordTable, progressBar);
132 }