1 function CanvasElectron(stage)
3 this._context = stage.context;
4 this._stageSize = stage.size;
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));
18 CanvasElectron.prototype._draw = function()
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));
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);
29 // Set the stroke and the fill colors
30 this._context.strokeStyle = "rgba(192, 192, 192, 0.9)";
31 this._context.fillStyle = this._color;
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();
39 this._context.beginPath();
40 this._context.arc(position.x, position.y, this._radius, 0, Math.PI * 2, true);
42 this._context.restore();
45 CanvasElectron.prototype.animate = function(timeDelta)
47 this._rotater.next(timeDelta / 100);
51 function CanvasElectronsStage(element, options)
53 Stage.call(this, element, options);
54 this.context = this.element.getContext("2d");
58 CanvasElectronsStage.prototype = Object.create(Stage.prototype);
59 CanvasElectronsStage.prototype.constructor = CanvasElectronsStage;
61 CanvasElectronsStage.prototype.tune = function(count)
64 return this._electrons.length;
67 for (var i = 0; i < count; ++i)
68 this._electrons.push(new CanvasElectron(this));
69 return this._electrons.length;
72 count = Math.min(-count, this._electrons.length);
73 this._electrons.splice(-count, count);
75 return this._electrons.length;
78 CanvasElectronsStage.prototype.animate = function(timeDelta)
80 this._electrons.forEach(function(electron) {
81 electron.animate(timeDelta);
85 function CanvasElectronsAnimator(benchmark)
87 Animator.call(this, benchmark);
88 this._context = benchmark._stage.context;
91 CanvasElectronsAnimator.prototype = Object.create(StageAnimator.prototype);
92 CanvasElectronsAnimator.prototype.constructor = CanvasElectronsAnimator;
94 CanvasElectronsAnimator.prototype.animate = function()
96 this._context.clearRect(0, 0, this._benchmark._stage.size.x, this._benchmark._stage.size.y);
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();
104 return StageAnimator.prototype.animate.call(this);
107 function CanvasElectronsBenchmark(suite, test, options, recordTable, progressBar)
109 StageBenchmark.call(this, suite, test, options, recordTable, progressBar);
112 CanvasElectronsBenchmark.prototype = Object.create(StageBenchmark.prototype);
113 CanvasElectronsBenchmark.prototype.constructor = CanvasElectronsBenchmark;
115 CanvasElectronsBenchmark.prototype.createStage = function(element)
117 // Attach the stage to the benchmark.
118 return new CanvasElectronsStage(element, this._options);
121 CanvasElectronsBenchmark.prototype.createAnimator = function()
123 // Attach the animator to the benchmark.
124 return new CanvasElectronsAnimator(this);
127 window.benchmarkClient.create = function(suite, test, options, recordTable, progressBar)
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);