@@ -72,6 +72,14 @@ The `<circle>` and `<rect>` tags, which do not exist in HTML, do have
72
72
a meaning in SVG—they draw shapes using the style and position
73
73
specified by their attributes.
74
74
75
+ ifdef::book_target[]
76
+
77
+ The document is displayed like this:
78
+
79
+ image::img/svg-demo.png[alt="An embedded SVG image",width="4.5cm"]
80
+
81
+ endif::book_target[]
82
+
75
83
These tags create ((DOM)) elements, just like ((HTML)) tags. For
76
84
example, this changes the `<circle>` element to be ((color))ed cyan
77
85
instead:
@@ -124,16 +132,16 @@ through the `getContext` method on the `<canvas>` element.
124
132
</script>
125
133
----
126
134
135
+ After creating the context object, the example draws a red
136
+ ((rectangle)) 100 ((pixel))s wide and 50 pixels high, with its top-left
137
+ corner at coordinates (10,10).
138
+
127
139
ifdef::book_target[]
128
140
129
- image::img/canvas_fill.png[alt="A canvas with a rectangle",width="3cm "]
141
+ image::img/canvas_fill.png[alt="A canvas with a rectangle",width="2.5cm "]
130
142
131
143
endif::book_target[]
132
144
133
- After creating the context object, the example draws a red
134
- ((rectangle)) 100 ((pixel))s wide and 50 pixels high, with its top-left
135
- corner at coordinates (10,10).
136
-
137
145
(((SVG)))(((coordinates)))Just like in ((HTML)) (and SVG), the
138
146
coordinate system that the canvas uses puts (0,0) at the top-left
139
147
corner, and the positive y-((axis)) goes down from there. So, (10,10)
@@ -181,6 +189,9 @@ positive number.
181
189
182
190
ifdef::book_target[]
183
191
192
+ This code draws two blue squares, using a thicker line for the second
193
+ one.
194
+
184
195
image::img/canvas_stroke.png[alt="Two stroked squares",width="5cm"]
185
196
186
197
endif::book_target[]
@@ -212,12 +223,6 @@ make a sequence of method calls to describe its shape.
212
223
</script>
213
224
----
214
225
215
- ifdef::book_target[]
216
-
217
- image::img/canvas_path.png[alt="Stroking a number of lines",width="2.1cm"]
218
-
219
- endif::book_target[]
220
-
221
226
(((canvas)))(((stroke method)))(((lineTo method)))(((moveTo
222
227
method)))(((shape)))This example creates a path with a number of
223
228
horizontal ((line)) segments and then strokes it using the `stroke`
@@ -226,6 +231,14 @@ _current_ position. That position is usually the end of the last segment,
226
231
unless `moveTo` was called. In that case, the next segment would start
227
232
at the position passed to `moveTo`.
228
233
234
+ ifdef::book_target[]
235
+
236
+ The path described by the previous program looks like this:
237
+
238
+ image::img/canvas_path.png[alt="Stroking a number of lines",width="2.1cm"]
239
+
240
+ endif::book_target[]
241
+
229
242
(((path,canvas)))(((filling)))(((path,closing)))(((fill method)))When
230
243
filling a path (using the `fill` method), each ((shape)) is filled
231
244
separately. A path can contain multiple shapes—each `moveTo` motion
@@ -247,17 +260,17 @@ start, and the shape enclosed by the completed path is filled.
247
260
</script>
248
261
----
249
262
263
+ This example draws a filled triangle. Note that only two of the triangle's
264
+ sides are explicitly drawn. The third, from the bottom-right corner
265
+ back to the top, is implied and won't be there when you stroke the
266
+ path.
267
+
250
268
ifdef::book_target[]
251
269
252
270
image::img/canvas_triangle.png[alt="Filling a path",width="2.2cm"]
253
271
254
272
endif::book_target[]
255
273
256
- This example draws a filled triangle. Note that only two of the triangle's
257
- sides are explicitly drawn. The third, from the bottom-right corner
258
- back to the top, is implied and won't be there when you stroke the
259
- path.
260
-
261
274
(((stroke method)))(((closePath
262
275
method)))(((path,closing)))(((canvas)))You could also use the `closePath` method
263
276
to explicitly close a path by adding an actual ((line)) segment back to
@@ -295,6 +308,8 @@ example illustrates this:
295
308
296
309
ifdef::book_target[]
297
310
311
+ It produces a path that looks like this:
312
+
298
313
image::img/canvas_quadraticcurve.png[alt="A quadratic curve",width="2.3cm"]
299
314
300
315
endif::book_target[]
@@ -328,16 +343,16 @@ illustrate the behavior of such a curve:
328
343
</script>
329
344
----
330
345
346
+ The two control points specify the direction at both ends of the
347
+ curve. The further they are away from their corresponding point, the
348
+ more the curve will “bulge” in that direction.
349
+
331
350
ifdef::book_target[]
332
351
333
352
image::img/canvas_beziercurve.png[alt="A bezier curve",width="2.2cm"]
334
353
335
354
endif::book_target[]
336
355
337
- The two control points specify the direction at both ends of the
338
- curve. The further they are away from their corresponding point, the
339
- more the curve will “bulge” in that direction.
340
-
341
356
(((trial and error)))Such ((curve))s can be hard to work with—it's
342
357
not always clear how to find the ((control point))s that provide the
343
358
((shape)) you are looking for. Sometimes you can compute
@@ -374,6 +389,8 @@ start of the rounded part.
374
389
375
390
ifdef::book_target[]
376
391
392
+ This produces two rounded corners with different radiuses.
393
+
377
394
image::img/canvas_arc.png[alt="Two arcs with different radii",width="2.3cm"]
378
395
379
396
endif::book_target[]
@@ -411,19 +428,19 @@ to draw a full circle.
411
428
</script>
412
429
----
413
430
414
- ifdef::book_target[]
415
-
416
- image::img/canvas_circle.png[alt="Drawing a circle",width="4.9cm"]
417
-
418
- endif::book_target[]
419
-
420
431
(((moveTo method)))(((arc method)))(((path, canvas)))The resulting picture
421
432
contains a ((line)) from the left of the full circle (first call to
422
433
`arc`) to the left of the quarter-((circle)) (second call). Like other
423
434
path-drawing methods, a line drawn with `arc` is connected to the
424
435
previous path segment by default. You'd have to call `moveTo` or
425
436
start a new path if you want to avoid this.
426
437
438
+ ifdef::book_target[]
439
+
440
+ image::img/canvas_circle.png[alt="Drawing a circle",width="4.9cm"]
441
+
442
+ endif::book_target[]
443
+
427
444
[[pie_chart]]
428
445
== Drawing a pie chart ==
429
446
@@ -469,8 +486,8 @@ people who picked a given choice.
469
486
var sliceAngle = (result.count / total) * 2 * Math.PI;
470
487
cx.beginPath();
471
488
// center=100,100, radius=100
472
- cx.arc(100, 100, 100,
473
489
// from current angle, clockwise by slice's angle
490
+ cx.arc(100, 100, 100,
474
491
currentAngle, currentAngle + sliceAngle);
475
492
currentAngle += sliceAngle;
476
493
cx.lineTo(100, 100);
@@ -482,6 +499,8 @@ people who picked a given choice.
482
499
483
500
ifdef::book_target[]
484
501
502
+ This draws the following chart:
503
+
485
504
image::img/canvas_pie_chart.png[alt="A pie chart",width="5cm"]
486
505
487
506
endif::book_target[]
@@ -647,6 +666,9 @@ set a horizontal scale and one to set a vertical scale.
647
666
648
667
ifdef::book_target[]
649
668
669
+ Due to the call to `scale`, the circle is drawn three times as wide
670
+ and half as high.
671
+
650
672
image::img/canvas_scale.png[alt="A scaled circle",width="6.6cm"]
651
673
652
674
endif::book_target[]
@@ -760,7 +782,7 @@ management. They conceptually keep a stack of transformation
760
782
stack, and when you call `restore`, the state on top of the stack is
761
783
taken off and used as the context's current transformation.
762
784
763
- (((branching recursion)))(((branching fractal
785
+ (((branching recursion)))(((fractal
764
786
example)))(((recursion)))The `branch` function in the following example
765
787
illustrates what you can do with a function that changes the
766
788
transformation and then calls another function (in this case itself),
@@ -795,6 +817,8 @@ recursion stops when the length drops below 8.
795
817
796
818
ifdef::book_target[]
797
819
820
+ The result is a simple fractal.
821
+
798
822
image::img/canvas_tree.png[alt="A recursive picture",width="5cm"]
799
823
800
824
endif::book_target[]
@@ -1055,7 +1079,7 @@ CanvasDisplay.prototype.drawPlayer = function(x, y, width,
1055
1079
};
1056
1080
----
1057
1081
1058
- The previous function is called by `drawActors`, which is responsible for
1082
+ The `drawPlayer` method is called by `drawActors`, which is responsible for
1059
1083
drawing all the actors in the game.
1060
1084
1061
1085
// include_code
0 commit comments