Simplify the test harness
[WebKit-https.git] / PerformanceTests / Animometer / tests / simple / resources / simple-canvas-paths.js
1 (function() {
2
3 // === PAINT OBJECTS ===
4
5 function CanvasLineSegment(stage) {
6     var radius = stage.randomInt(10, 100);
7     var center = stage.randomPosition(stage.size);
8     var delta = Point.pointOnCircle(stage.randomAngle(), radius/2);
9
10     this._point1 = center.add(delta);
11     this._point2 = center.subtract(delta);
12     this._color = stage.randomColor();
13     this._lineWidth = stage.randomInt(1, 100);
14 }
15 CanvasLineSegment.prototype.draw = function(context) {
16     context.strokeStyle = this._color;
17     context.lineWidth = this._lineWidth;
18     context.beginPath();
19     context.moveTo(this._point1.x, this._point1.y);
20     context.lineTo(this._point2.x, this._point2.y);
21     context.stroke();
22 };
23
24 function CanvasLinePoint(stage, coordinateMaximum) {
25     this._point = stage.randomPosition(new Point(Math.min(stage.size.x, coordinateMaximum), Math.min(stage.size.y, coordinateMaximum)));
26 }
27 CanvasLinePoint.prototype.draw = function(context) {
28     context.lineTo(this._point.x, this._point.y);
29 };
30
31 function CanvasQuadraticSegment(stage) {
32     var maxSize = stage.randomInt(20, 200);
33     var toCenter = stage.randomPosition(stage.size).subtract(new Point(maxSize/2, maxSize/2));
34
35     this._point1 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
36     this._point2 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
37     this._point3 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
38     this._color = stage.randomColor();
39     this._lineWidth = stage.randomInt(1, 50);
40 };
41 CanvasQuadraticSegment.prototype.draw = function(context) {
42     context.strokeStyle = this._color;
43     context.lineWidth = this._lineWidth;
44     context.beginPath();
45     context.moveTo(this._point1.x, this._point1.y);
46     context.quadraticCurveTo(this._point2.x, this._point2.y, this._point3.x, this._point3.y);
47     context.stroke();
48 };
49
50 function CanvasQuadraticPoint(stage, coordinateMaximum) {
51     var pointMaximum = new Point(Math.min(stage.size.x, coordinateMaximum), Math.min(stage.size.y, coordinateMaximum));
52     this._point1 = stage.randomPosition(pointMaximum);
53     this._point2 = stage.randomPosition(pointMaximum);
54 };
55 CanvasQuadraticPoint.prototype.draw = function(context) {
56     context.quadraticCurveTo(this._point1.x, this._point1.y, this._point2.x, this._point2.y);
57 };
58
59 function CanvasBezierSegment(stage) {
60     var maxSize = stage.randomInt(20, 200);
61     var toCenter = stage.randomPosition(stage.size).subtract(new Point(maxSize/2, maxSize/2));
62
63     this._point1 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
64     this._point2 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
65     this._point3 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
66     this._point4 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
67     this._color = stage.randomColor();
68     this._lineWidth = stage.randomInt(1, 50);
69 };
70 CanvasBezierSegment.prototype.draw = function(context) {
71     context.strokeStyle = this._color;
72     context.lineWidth = this._lineWidth;
73     context.beginPath();
74     context.moveTo(this._point1.x, this._point1.y);
75     context.bezierCurveTo(this._point2.x, this._point2.y, this._point3.x, this._point3.y, this._point4.x, this._point4.y);
76     context.stroke();
77 };
78
79 function CanvasBezierPoint(stage, coordinateMaximum) {
80     var pointMaximum = new Point(Math.min(stage.size.x, coordinateMaximum), Math.min(stage.size.y, coordinateMaximum));
81     this._point1 = stage.randomPosition(pointMaximum);
82     this._point2 = stage.randomPosition(pointMaximum);
83     this._point3 = stage.randomPosition(pointMaximum);
84 };
85 CanvasBezierPoint.prototype.draw = function(context) {
86     context.bezierCurveTo(this._point1.x, this._point1.y, this._point2.x, this._point2.y, this._point3.x, this._point3.y);
87 };
88
89 function CanvasArcToSegment(stage) {
90     var maxSize = stage.randomInt(20, 200);
91     var toCenter = stage.randomPosition(stage.size).subtract(new Point(maxSize/2, maxSize/2));
92
93     this._point1 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
94     this._point2 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
95     this._point3 = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
96     this._radius = stage.randomInt(20, 200);
97     this._color = stage.randomColor();
98     this._lineWidth = stage.randomInt(1, 50);
99 };
100 CanvasArcToSegment.prototype.draw = function(context) {
101     context.strokeStyle = this._color;
102     context.lineWidth = this._lineWidth;
103     context.beginPath();
104     context.moveTo(this._point1.x, this._point1.y);
105     context.arcTo(this._point2.x, this._point2.y, this._point3.x, this._point3.y, this._radius);
106     context.stroke();
107 };
108
109 function CanvasArcToSegmentFill(stage) {
110     CanvasArcToSegment.call(this, stage);
111 };
112 CanvasArcToSegmentFill.prototype.draw = function(context) {
113     context.fillStyle = this._color;
114     context.beginPath();
115     context.moveTo(this._point1.x, this._point1.y);
116     context.arcTo(this._point2.x, this._point2.y, this._point3.x, this._point3.y, this._radius);
117     context.fill();
118 };
119
120 function CanvasArcSegment(stage) {
121     var maxSize = stage.randomInt(20, 200);
122     var toCenter = stage.randomPosition(stage.size).subtract(new Point(maxSize/2, maxSize/2));
123
124     this._point = stage.randomPosition(new Point(maxSize, maxSize)).add(toCenter);
125     this._radius = stage.randomInt(20, 200);
126     this._startAngle = stage.randomAngle();
127     this._endAngle = stage.randomAngle();
128     this._counterclockwise = stage.randomBool();
129     this._color = stage.randomColor();
130     this._lineWidth = stage.randomInt(1, 50);
131 };
132 CanvasArcSegment.prototype.draw = function(context) {
133     context.strokeStyle = this._color;
134     context.lineWidth = this._lineWidth;
135     context.beginPath();
136     context.arc(this._point.x, this._point.y, this._radius, this._startAngle, this._endAngle, this._counterclockwise);
137     context.stroke();
138 };
139
140 function CanvasArcSegmentFill(stage) {
141     CanvasArcSegment.call(this, stage);
142 };
143 CanvasArcSegmentFill.prototype.draw = function(context) {
144     context.fillStyle = this._color;
145     context.beginPath();
146     context.arc(this._point.x, this._point.y, this._radius, this._startAngle, this._endAngle, this._counterclockwise);
147     context.fill();
148 };
149
150 function CanvasRect(stage) {
151     this._width = stage.randomInt(20, 200);
152     this._height = stage.randomInt(20, 200);
153     this._point = stage.randomPosition(stage.size).subtract(new Point(this._width/2, this._height/2));
154     this._color = stage.randomColor();
155     this._lineWidth = stage.randomInt(1, 20);
156 }
157 CanvasRect.prototype.draw = function(context) {
158     context.strokeStyle = this._color;
159     context.lineWidth = this._lineWidth;
160     context.beginPath();
161     context.rect(this._point.x, this._point.y, this._width, this._height);
162     context.stroke();
163 };
164
165 function CanvasRectFill(stage) {
166     CanvasRect.call(this, stage);
167 }
168 CanvasRectFill.prototype.draw = function(context) {
169     context.fillStyle = this._color;
170     context.beginPath();
171     context.rect(this._point.x, this._point.y, this._width, this._height);
172     context.fill();
173 };
174
175 // === STAGES ===
176
177 SimpleCanvasPathStrokeStage = Utilities.createSubclass(SimpleCanvasStage,
178     function(canvasObject) {
179         SimpleCanvasStage.call(this, canvasObject);
180     }, {
181
182     animate: function()
183     {
184         var context = this.context;
185         context.clearRect(0, 0, this.size.x, this.size.y);
186         context.lineWidth = this.randomInt(1, 20);
187         context.strokeStyle = this.randomColor();
188         context.beginPath();
189         context.moveTo(0,0);
190         this.objects.forEach(function(object) {
191             object.draw(context);
192         });
193         context.stroke();
194     }
195 });
196
197 SimpleCanvasPathFillStage = Utilities.createSubclass(SimpleCanvasStage,
198     function(canvasObject) {
199         SimpleCanvasStage.call(this, canvasObject);
200     }, {
201
202     animate: function()
203     {
204         var context = this.context;
205         context.clearRect(0, 0, this.size.x, this.size.y);
206         context.fillStyle = this.randomColor();
207         context.beginPath();
208         context.moveTo(0,0);
209         this.objects.forEach(function(object) {
210             object.draw(context);
211         });
212         context.fill();
213     }
214 });
215
216 CanvasLineSegmentStage = Utilities.createSubclass(SimpleCanvasStage,
217     function()
218     {
219         SimpleCanvasStage.call(this, CanvasLineSegment);
220     }, {
221
222     initialize: function(benchmark)
223     {
224         SimpleCanvasStage.prototype.initialize.call(this, benchmark);
225         this.context.lineCap = benchmark.options["lineCap"] || "butt";
226     }
227 });
228
229 CanvasLinePathStage = Utilities.createSubclass(SimpleCanvasPathStrokeStage,
230     function()
231     {
232         SimpleCanvasPathStrokeStage.call(this, CanvasLinePoint);
233     }, {
234
235     initialize: function(benchmark)
236     {
237         SimpleCanvasPathStrokeStage.prototype.initialize.call(this, benchmark);
238         this.context.lineJoin = benchmark.options["lineJoin"] || "bevel";
239     }
240 });
241
242 CanvasLineDashStage = Utilities.createSubclass(SimpleCanvasStage,
243     function()
244     {
245         SimpleCanvasStage.call(this, CanvasLinePoint);
246         this._step = 0;
247     }, {
248
249     initialize: function(benchmark)
250     {
251         SimpleCanvasStage.prototype.initialize.call(this, benchmark);
252         this.context.setLineDash([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
253         this.context.lineWidth = 1;
254         this.context.strokeStyle = "#000";
255     },
256
257     animate: function()
258     {
259         var context = this.context;
260         context.clearRect(0, 0, this.size.x, this.size.y);
261         context.lineDashOffset = this._step++;
262         context.beginPath();
263         context.moveTo(0,0);
264         this.objects.forEach(function(object) {
265             object.draw(context);
266         });
267         context.stroke();
268     }
269 });
270
271 // === BENCHMARK ===
272
273 CanvasPathBenchmark = Utilities.createSubclass(Benchmark,
274     function(options)
275     {
276         var stage;
277         switch (options["pathType"]) {
278         case "line":
279             stage = new CanvasLineSegmentStage();
280             break;
281         case "linePath": {
282             if ("lineJoin" in options)
283                 stage = new CanvasLinePathStage();
284             if ("lineDash" in options)
285                 stage = new CanvasLineDashStage();
286             break;
287         }
288         case "quadratic":
289             stage = new SimpleCanvasStage(CanvasQuadraticSegment);
290             break;
291         case "quadraticPath":
292             stage = new SimpleCanvasPathStrokeStage(CanvasQuadraticPoint);
293             break;
294         case "bezier":
295             stage = new SimpleCanvasStage(CanvasBezierSegment);
296             break;
297         case "bezierPath":
298             stage = new SimpleCanvasPathStrokeStage(CanvasBezierPoint);
299             break;
300         case "arcTo":
301             stage = new SimpleCanvasStage(CanvasArcToSegment);
302             break;
303         case "arc":
304             stage = new SimpleCanvasStage(CanvasArcSegment);
305             break;
306         case "rect":
307             stage = new SimpleCanvasStage(CanvasRect);
308             break;
309         case "lineFill":
310             stage = new SimpleCanvasPathFillStage(CanvasLinePoint);
311             break;
312         case "quadraticFill":
313             stage = new SimpleCanvasPathFillStage(CanvasQuadraticPoint);
314             break;
315         case "bezierFill":
316             stage = new SimpleCanvasPathFillStage(CanvasBezierPoint);
317             break;
318         case "arcToFill":
319             stage = new SimpleCanvasStage(CanvasArcToSegmentFill);
320             break;
321         case "arcFill":
322             stage = new SimpleCanvasStage(CanvasArcSegmentFill);
323             break;
324         case "rectFill":
325             stage = new SimpleCanvasStage(CanvasRectFill);
326             break;
327         }
328
329         Benchmark.call(this, stage, options);
330     }
331 );
332
333 window.benchmarkClass = CanvasPathBenchmark;
334
335 })();