5e12e9f7dbf0c9a500b4ddb54ee47d01ec1252c7
[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     count = count > 0 ? Math.floor(count) : Math.ceil(count);
64
65     if (count == 0)
66         return this._electrons.length;
67
68     if (count > 0) {
69         for (var i = 0; i < count; ++i)
70             this._electrons.push(new CanvasElectron(this));
71         return this._electrons.length;
72     }
73
74     count = Math.min(-count, this._electrons.length);
75     this._electrons.splice(-count, count);
76     
77     return this._electrons.length;
78 }
79
80 CanvasElectronsStage.prototype.animate = function(timeDelta)
81 {
82     this._electrons.forEach(function(electron) {
83         electron.animate(timeDelta);
84     });
85 }
86
87 function CanvasElectronsAnimator(benchmark)
88 {
89     Animator.call(this, benchmark);
90     this._context = benchmark._stage.context;
91 }
92
93 CanvasElectronsAnimator.prototype = Object.create(StageAnimator.prototype);
94 CanvasElectronsAnimator.prototype.constructor = CanvasElectronsAnimator;
95
96 CanvasElectronsAnimator.prototype.animate = function()
97 {
98     this._context.clearRect(0, 0, this._benchmark._stage.size.x, this._benchmark._stage.size.y);
99
100     // Draw a big star in the middle.
101     this._context.fillStyle = "orange";    
102     this._context.beginPath();
103     this._context.arc(this._benchmark._stage.size.center.x, this._benchmark._stage.size.center.y, 50, 0, Math.PI * 2, true);
104     this._context.fill();
105
106     return StageAnimator.prototype.animate.call(this);
107 }
108
109 function CanvasElectronsBenchmark(suite, test, options, recordTable, progressBar)
110 {
111     StageBenchmark.call(this, suite, test, options, recordTable, progressBar);
112 }
113
114 CanvasElectronsBenchmark.prototype = Object.create(StageBenchmark.prototype);
115 CanvasElectronsBenchmark.prototype.constructor = CanvasElectronsBenchmark;
116
117 CanvasElectronsBenchmark.prototype.createStage = function(element)
118 {
119     // Attach the stage to the benchmark.
120     return new CanvasElectronsStage(element, this._options);
121 }
122
123 CanvasElectronsBenchmark.prototype.createAnimator = function()
124 {
125     // Attach the animator to the benchmark.
126     return new CanvasElectronsAnimator(this);
127 }
128
129 window.benchmarkClient.create = function(suite, test, options, recordTable, progressBar)
130 {
131     // This function is called from the test harness which starts the
132     // test by creating your benchmark object.
133     return new CanvasElectronsBenchmark(suite, test, options, recordTable, progressBar);
134 }