Refactor tune() to not return the complexity of the scene.
[WebKit-https.git] / PerformanceTests / Animometer / tests / misc / resources / canvas-electrons.js
1 (function() {
2
3 function CanvasElectron(stage)
4 {
5     this._context = stage.context;
6     this._stageSize = stage.size;
7
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));
18 }
19
20 CanvasElectron.prototype = {
21     _draw: function()
22     {
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));
26
27         this._context.save();
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);
31
32             // Set the stroke and the fill colors
33             this._context.strokeStyle = "rgba(192, 192, 192, 0.9)";
34             this._context.fillStyle = this._color;
35
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();
41             }
42
43             // Draw the object.
44             this._context.beginPath();
45             this._context.arc(position.x, position.y, this._radius, 0, Math.PI * 2, true);
46             this._context.fill();
47         this._context.restore();
48     },
49
50     animate: function(timeDelta)
51     {
52         this._rotater.next(timeDelta / 100);
53         this._draw();
54     }
55 };
56
57 CanvasElectronsStage = Utilities.createSubclass(Stage,
58     function()
59     {
60         Stage.call(this);
61         this._electrons = [];
62     }, {
63
64     initialize: function(benchmark, options)
65     {
66         Stage.prototype.initialize.call(this, benchmark, options);
67         this.context = this.element.getContext("2d");
68     },
69
70     tune: function(count)
71     {
72         if (count == 0)
73             return;
74
75         if (count > 0) {
76             for (var i = 0; i < count; ++i)
77                 this._electrons.push(new CanvasElectron(this));
78             return;
79         }
80
81         count = Math.min(-count, this._electrons.length);
82         this._electrons.splice(-count, count);
83     },
84
85     animate: function(timeDelta)
86     {
87         this.context.clearRect(0, 0, this.size.x, this.size.y);
88
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);
93         this.context.fill();
94
95         this._electrons.forEach(function(electron) {
96             electron.animate(timeDelta);
97         });
98     },
99
100     complexity: function()
101     {
102         return this._electrons.length;
103     }
104 });
105
106 CanvasElectronsBenchmark = Utilities.createSubclass(Benchmark,
107     function(options)
108     {
109         Benchmark.call(this, new CanvasElectronsStage(), options);
110     }
111 );
112
113 window.benchmarkClass = CanvasElectronsBenchmark;
114
115 })();