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