3 // === PAINT OBJECTS ===
5 CanvasLineSegment = Utilities.createClass(
7 var radius = Stage.randomInt(10, 100);
8 var center = Stage.randomPosition(stage.size);
9 var delta = Point.pointOnCircle(Stage.randomAngle(), radius/2);
11 this._point1 = center.add(delta);
12 this._point2 = center.subtract(delta);
13 this._color = Stage.randomColor();
14 this._lineWidth = Stage.randomInt(1, 100);
17 draw: function(context) {
18 context.strokeStyle = this._color;
19 context.lineWidth = this._lineWidth;
21 context.moveTo(this._point1.x, this._point1.y);
22 context.lineTo(this._point2.x, this._point2.y);
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));
33 draw: function(context) {
34 context.lineTo(this._point.x, this._point.y);
38 CanvasQuadraticSegment = Utilities.createClass(
40 var maxSize = Stage.randomInt(20, 200);
41 var toCenter = Stage.randomPosition(stage.size).subtract(new Point(maxSize/2, maxSize/2));
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);
50 draw: function(context) {
51 context.strokeStyle = this._color;
52 context.lineWidth = this._lineWidth;
54 context.moveTo(this._point1.x, this._point1.y);
55 context.quadraticCurveTo(this._point2.x, this._point2.y, this._point3.x, this._point3.y);
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));
67 draw: function(context) {
68 context.quadraticCurveTo(this._point1.x, this._point1.y, this._point2.x, this._point2.y);
72 CanvasBezierSegment = Utilities.createClass(
74 var maxSize = Stage.randomInt(20, 200);
75 var toCenter = Stage.randomPosition(stage.size).subtract(new Point(maxSize/2, maxSize/2));
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);
85 draw: function(context) {
86 context.strokeStyle = this._color;
87 context.lineWidth = this._lineWidth;
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);
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));
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);
108 CanvasArcToSegment = Utilities.createClass(
110 var maxSize = Stage.randomInt(20, 200);
111 var toCenter = Stage.randomPosition(stage.size).subtract(new Point(maxSize/2, maxSize/2));
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);
121 draw: function(context) {
122 context.strokeStyle = this._color;
123 context.lineWidth = this._lineWidth;
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);
131 CanvasArcToSegmentFill = Utilities.createClass(
133 CanvasArcToSegment.call(this, stage);
136 draw: function(context) {
137 context.fillStyle = this._color;
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);
145 CanvasArcSegment = Utilities.createClass(
147 var maxSize = Stage.randomInt(20, 200);
148 var toCenter = Stage.randomPosition(stage.size).subtract(new Point(maxSize/2, maxSize/2));
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);
159 draw: function(context) {
160 context.strokeStyle = this._color;
161 context.lineWidth = this._lineWidth;
163 context.arc(this._point.x, this._point.y, this._radius, this._startAngle, this._endAngle, this._counterclockwise);
168 CanvasArcSegmentFill = Utilities.createClass(
170 CanvasArcSegment.call(this, stage);
173 draw: function(context) {
174 context.fillStyle = this._color;
176 context.arc(this._point.x, this._point.y, this._radius, this._startAngle, this._endAngle, this._counterclockwise);
181 CanvasRect = Utilities.createClass(
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);
190 draw: function(context) {
191 context.strokeStyle = this._color;
192 context.lineWidth = this._lineWidth;
194 context.rect(this._point.x, this._point.y, this._width, this._height);
199 CanvasRectFill = Utilities.createClass(
201 CanvasRect.call(this, stage);
204 draw: function(context) {
205 context.fillStyle = this._color;
207 context.rect(this._point.x, this._point.y, this._width, this._height);
214 SimpleCanvasPathStrokeStage = Utilities.createSubclass(SimpleCanvasStage,
215 function(canvasObject) {
216 SimpleCanvasStage.call(this, canvasObject);
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();
226 context.moveTo(this.size.x / 2, this.size.y / 2);
227 this.objects.forEach(function(object) {
228 object.draw(context);
234 SimpleCanvasPathFillStage = Utilities.createSubclass(SimpleCanvasStage,
235 function(canvasObject) {
236 SimpleCanvasStage.call(this, canvasObject);
241 var context = this.context;
242 context.clearRect(0, 0, this.size.x, this.size.y);
243 context.fillStyle = Stage.rotatingColor();
245 context.moveTo(this.size.x / 2, this.size.y / 2);
246 this.objects.forEach(function(object) {
247 object.draw(context);
253 CanvasLineSegmentStage = Utilities.createSubclass(SimpleCanvasStage,
256 SimpleCanvasStage.call(this, CanvasLineSegment);
259 initialize: function(benchmark, options)
261 SimpleCanvasStage.prototype.initialize.call(this, benchmark, options);
262 this.context.lineCap = options["lineCap"] || "butt";
266 CanvasLinePathStage = Utilities.createSubclass(SimpleCanvasPathStrokeStage,
269 SimpleCanvasPathStrokeStage.call(this, CanvasLinePoint);
272 initialize: function(benchmark, options)
274 SimpleCanvasPathStrokeStage.prototype.initialize.call(this, benchmark, options);
275 this.context.lineJoin = options["lineJoin"] || "bevel";
279 CanvasLineDashStage = Utilities.createSubclass(SimpleCanvasStage,
282 SimpleCanvasStage.call(this, CanvasLinePoint);
286 initialize: function(benchmark, options)
288 SimpleCanvasStage.prototype.initialize.call(this, benchmark, options);
289 this.context.setLineDash([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
290 this.context.lineWidth = 1;
291 this.context.strokeStyle = "#000";
296 var context = this.context;
297 context.clearRect(0, 0, this.size.x, this.size.y);
298 context.lineDashOffset = this._step++;
300 context.moveTo(this.size.x / 2, this.size.y / 2);
301 this.objects.forEach(function(object) {
302 object.draw(context);
310 CanvasPathBenchmark = Utilities.createSubclass(Benchmark,
314 switch (options["pathType"]) {
316 stage = new CanvasLineSegmentStage();
319 if ("lineJoin" in options)
320 stage = new CanvasLinePathStage();
321 if ("lineDash" in options)
322 stage = new CanvasLineDashStage();
326 stage = new SimpleCanvasStage(CanvasQuadraticSegment);
328 case "quadraticPath":
329 stage = new SimpleCanvasPathStrokeStage(CanvasQuadraticPoint);
332 stage = new SimpleCanvasStage(CanvasBezierSegment);
335 stage = new SimpleCanvasPathStrokeStage(CanvasBezierPoint);
338 stage = new SimpleCanvasStage(CanvasArcToSegment);
341 stage = new SimpleCanvasStage(CanvasArcSegment);
344 stage = new SimpleCanvasStage(CanvasRect);
347 stage = new SimpleCanvasPathFillStage(CanvasLinePoint);
349 case "quadraticFill":
350 stage = new SimpleCanvasPathFillStage(CanvasQuadraticPoint);
353 stage = new SimpleCanvasPathFillStage(CanvasBezierPoint);
356 stage = new SimpleCanvasStage(CanvasArcToSegmentFill);
359 stage = new SimpleCanvasStage(CanvasArcSegmentFill);
362 stage = new SimpleCanvasStage(CanvasRectFill);
366 Benchmark.call(this, stage, options);
370 window.benchmarkClass = CanvasPathBenchmark;