3 function CanvasElectron(stage)
5 this._context = stage.context;
6 this._stageSize = stage.size;
8 var minSide = Math.min(this._stageSize.width, this._stageSize.height);
9 var radiusX = Stage.random(minSide / 8, 7 * minSide / 16);
10 var radiusY = Stage.random(minSide / 8, 3 * radiusX / 4);
11 this._orbitRadiuses = new Point(radiusX, radiusY);
12 this._radius = Stage.random(5, 15);
13 this._direction = Stage.randomInt(0, 2);
14 this._angle = Stage.randomInt(0, 360);
15 this._color = Stage.randomColor();
16 this._rotater = Stage.randomRotater();
17 this._rotater.next(Stage.random(0, this._rotater.interval));
20 CanvasElectron.prototype = {
23 // Calculate the position of the object on the ellipse.
24 var angle = this._direction ? this._rotater.degree() : 360 - this._rotater.degree();
25 var position = this._stageSize.center.subtract(Point.pointOnEllipse(angle, this._orbitRadiuses));
28 this._context.translate(this._stageSize.center.x, this._stageSize.center.y);
29 this._context.rotate(this._angle * Math.PI / 180);
30 this._context.translate(-this._stageSize.center.x, -this._stageSize.center.y);
32 // Set the stroke and the fill colors
33 this._context.strokeStyle = "rgba(192, 192, 192, 0.9)";
34 this._context.fillStyle = this._color;
36 // Draw the orbit of the object.
37 if (this._context.ellipse) {
38 this._context.beginPath();
39 this._context.ellipse(this._stageSize.center.x, this._stageSize.center.y, this._orbitRadiuses.x, this._orbitRadiuses.y, 0, 0, 2 * Math.PI);
40 this._context.stroke();
44 this._context.beginPath();
45 this._context.arc(position.x, position.y, this._radius, 0, Math.PI * 2, true);
47 this._context.restore();
50 animate: function(timeDelta)
52 this._rotater.next(timeDelta / 100);
57 CanvasElectronsStage = Utilities.createSubclass(Stage,
64 initialize: function(benchmark, options)
66 Stage.prototype.initialize.call(this, benchmark, options);
67 this.context = this.element.getContext("2d");
76 for (var i = 0; i < count; ++i)
77 this._electrons.push(new CanvasElectron(this));
81 count = Math.min(-count, this._electrons.length);
82 this._electrons.splice(-count, count);
85 animate: function(timeDelta)
87 this.context.clearRect(0, 0, this.size.x, this.size.y);
89 // Draw a big star in the middle.
90 this.context.fillStyle = "orange";
91 this.context.beginPath();
92 this.context.arc(this.size.center.x, this.size.center.y, 50, 0, Math.PI * 2, true);
95 this._electrons.forEach(function(electron) {
96 electron.animate(timeDelta);
100 complexity: function()
102 return this._electrons.length;
106 CanvasElectronsBenchmark = Utilities.createSubclass(Benchmark,
109 Benchmark.call(this, new CanvasElectronsStage(), options);
113 window.benchmarkClass = CanvasElectronsBenchmark;