Refactor helper methods for getting random values for a stage.
[WebKit-https.git] / PerformanceTests / Animometer / tests / bouncing-particles / resources / bouncing-canvas-shapes.js
1 (function() {
2
3 BouncingCanvasShape = Utilities.createSubclass(BouncingCanvasParticle,
4     function(stage)
5     {
6         BouncingCanvasParticle.call(this, stage, stage.shape);
7         this._fill = stage.fill;
8         this._color0 = Stage.randomColor();
9         this._color1 = Stage.randomColor();
10     }, {
11
12     _applyFill: function()
13     {
14         switch (this._fill) {
15         case "gradient":
16             var gradient = this.context.createLinearGradient(0, 0, this.size.width, 0);
17             gradient.addColorStop(0, this._color0);
18             gradient.addColorStop(1, this._color1);
19             this.context.fillStyle = gradient;
20             break;
21
22         case "solid":
23         default:
24             this.context.fillStyle = this._color0;
25             break;
26         }
27     },
28
29     _drawShape: function()
30     {
31         this.context.beginPath();
32
33         switch (this._shape) {
34         case "rect":
35             this.context.rect(0, 0, this.size.width, this.size.height);
36             break;
37
38         case "circle":
39         default:
40             var center = this.size.center;
41             var radius = Math.min(this.size.x, this.size.y) / 2;
42             this.context.arc(center.x, center.y, radius, 0, Math.PI * 2, true);
43             break;
44         }
45
46         this.context.fill();
47     },
48
49     _draw: function()
50     {
51         this.context.save();
52             this._applyFill();
53             this.applyRotation();
54             this.applyClipping();
55             this._drawShape();
56         this.context.restore();
57     }
58 });
59
60 BouncingCanvasShapesStage = Utilities.createSubclass(BouncingCanvasParticlesStage,
61     function ()
62     {
63         BouncingCanvasParticlesStage.call(this);
64     }, {
65
66     initialize: function(benchmark)
67     {
68         BouncingCanvasParticlesStage.prototype.initialize.call(this, benchmark);
69         this.parseShapeParameters(benchmark.options);
70     },
71
72     createParticle: function()
73     {
74         return new BouncingCanvasShape(this);
75     }
76 });
77
78 BouncingCanvasShapesBenchmark = Utilities.createSubclass(Benchmark,
79     function(options)
80     {
81         Benchmark.call(this, new BouncingCanvasShapesStage(), options);
82     }
83 );
84
85 window.benchmarkClass = BouncingCanvasShapesBenchmark;
86
87 })();