Repository navigation
Expand file tree
/
Copy pathshapes.html
More file actions
449 lines (338 loc) · 27.5 KB
/
Copy pathshapes.html
File metadata and controls
449 lines (338 loc) · 27.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html>
<head>
<title>shapes.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>shapes.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<p>A <em>very</em> simple shapes module for drawing
<a href="http://ccl.northwestern.edu/netlogo/docs/">NetLogo-like</a> agents.</p>
</div>
<div class="content"><div class='highlight'><pre>
Shapes = <span class="hljs-keyword">do</span> -></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Each shape is a named object with two members:
a boolean rotate and a draw procedure and two optional
properties: img for images, and shortcut for a transform-less
version of draw.</p>
<p>The shape is used in the following context with a color set
and a transform such that the shape should be drawn in a -.5 to .5 square
The draw procedure has an optional strokeColor which can be used,
if specified, see “bug” for example (“#000000” is the un-set color value).
Also see “filledRing” which uses the strokeColor as an outer fillStyle.
The shape doesn’t call “fill” which is called by the shape user.</p>
<pre><code>ctx.save()
ctx.fillStyle = color.css
ctx.strokeStyle = strokeColor.css <span class="hljs-keyword">if</span> strokeColor
ctx.translate x, y; ctx.scale size, size;
ctx.rotate heading <span class="hljs-keyword">if</span> shape.rotate
ctx.beginPath(); shape.draw(ctx); ctx.closePath()
ctx.fill()
ctx.restore()
</code></pre><p>The list of current shapes, via <code>ABM.Shapes.names()</code> below, is:</p>
<pre><code>[<span class="hljs-string">"default"</span>, <span class="hljs-string">"triangle"</span>, <span class="hljs-string">"arrow"</span>, <span class="hljs-string">"bug"</span>, <span class="hljs-string">"pyramid"</span>, <span class="hljs-string">"circle"</span>,
<span class="hljs-string">"square"</span>, <span class="hljs-string">"pentagon"</span>, <span class="hljs-string">"ring"</span>, <span class="hljs-string">"filledRing"</span>, <span class="hljs-string">"person"</span>]
</code></pre>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>A polygon utility: c is the 2D context, and a is an array of 2D points.
c.closePath() and c.fill() will be called by the calling turtle, see initial
discription of drawing context. It is used in adding a new shape above.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">poly</span> = <span class="hljs-params">(c, a)</span> -></span>
<span class="hljs-keyword">for</span> p, i <span class="hljs-keyword">in</span> a
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">is</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> c.moveTo p[<span class="hljs-number">0</span>], p[<span class="hljs-number">1</span>] <span class="hljs-keyword">else</span> c.lineTo p[<span class="hljs-number">0</span>], p[<span class="hljs-number">1</span>]
<span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Centered drawing primitives: centered on x,y with a given width/height size.
Useful for shortcuts</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">circ</span> = <span class="hljs-params">(c, x, y, s)</span> -></span> <span class="hljs-comment"># centered circle</span>
c.arc x, y, s/<span class="hljs-number">2</span>, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>*Math.PI
<span class="hljs-function"> <span class="hljs-title">ccirc</span> = <span class="hljs-params">(c, x, y, s)</span> -></span> <span class="hljs-comment"># counter clockwise circ</span>
c.arc x, y, s/<span class="hljs-number">2</span>, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>*Math.PI, <span class="hljs-literal">true</span>
<span class="hljs-function"> <span class="hljs-title">cimg</span> = <span class="hljs-params">(c, x, y, s, img)</span>-></span> <span class="hljs-comment"># Image, flippd for cartesian, y-up.</span>
c.scale <span class="hljs-number">1</span>, -<span class="hljs-number">1</span>
c.drawImage img, x-s/<span class="hljs-number">2</span>, y-s/<span class="hljs-number">2</span>, s, s
c.scale <span class="hljs-number">1</span>, -<span class="hljs-number">1</span>
<span class="hljs-function"> <span class="hljs-title">csq</span> = <span class="hljs-params">(c, x, y, s)</span> -></span> <span class="hljs-comment"># centered square</span>
c.fillRect x-s/<span class="hljs-number">2</span>, y-s/<span class="hljs-number">2</span>, s, s</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>An async utility for delayed drawing of images into sprite slots</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">fillSlot</span> = <span class="hljs-params">(slot, img)</span> -></span>
slot.ctx.save()
slot.ctx.scale <span class="hljs-number">1</span>, -<span class="hljs-number">1</span>
slot.ctx.drawImage img, slot.x, -(slot.y+slot.spriteSize), slot.spriteSize, slot.spriteSize
slot.ctx.restore()</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>The spritesheet data, indexed by sprite size</p>
</div>
<div class="content"><div class='highlight'><pre> spriteSheets = []</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>The module returns the following object:</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">default</span>:
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">true</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span> poly c, [[<span class="hljs-number">.5</span>,<span class="hljs-number">0</span>],[-<span class="hljs-number">.5</span>,-<span class="hljs-number">.5</span>],[-<span class="hljs-number">.25</span>,<span class="hljs-number">0</span>],[-<span class="hljs-number">.5</span>,<span class="hljs-number">.5</span>]]
<span class="hljs-attribute">triangle</span>:
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">true</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span> poly c, [[<span class="hljs-number">.5</span>,<span class="hljs-number">0</span>],[-<span class="hljs-number">.5</span>,-<span class="hljs-number">.4</span>],[-<span class="hljs-number">.5</span>,<span class="hljs-number">.4</span>]]
<span class="hljs-attribute">arrow</span>:
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">true</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span> poly c, [[<span class="hljs-number">.5</span>,<span class="hljs-number">0</span>],[<span class="hljs-number">0</span>,<span class="hljs-number">.5</span>],[<span class="hljs-number">0</span>,<span class="hljs-number">.2</span>],[-<span class="hljs-number">.5</span>,<span class="hljs-number">.2</span>],[-<span class="hljs-number">.5</span>,-<span class="hljs-number">.2</span>],[<span class="hljs-number">0</span>,-<span class="hljs-number">.2</span>],[<span class="hljs-number">0</span>,-<span class="hljs-number">.5</span>]]
<span class="hljs-attribute">bug</span>:
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">true</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span>
c.strokeStyle = c.fillStyle <span class="hljs-keyword">if</span> c.strokeStyle <span class="hljs-keyword">is</span> <span class="hljs-string">"#000000"</span>
c.lineWidth = <span class="hljs-number">.05</span>; poly c, [[<span class="hljs-number">.4</span>,<span class="hljs-number">.225</span>],[<span class="hljs-number">.2</span>,<span class="hljs-number">0</span>],[<span class="hljs-number">.4</span>,-<span class="hljs-number">.225</span>]]; c.stroke()
c.beginPath(); circ c,<span class="hljs-number">.12</span>,<span class="hljs-number">0</span>,<span class="hljs-number">.26</span>; circ c,-<span class="hljs-number">.05</span>,<span class="hljs-number">0</span>,<span class="hljs-number">.26</span>; circ c,-<span class="hljs-number">.27</span>,<span class="hljs-number">0</span>,<span class="hljs-number">.4</span>
<span class="hljs-attribute">pyramid</span>:
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">false</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span> poly c, [[<span class="hljs-number">0</span>,<span class="hljs-number">.5</span>],[-<span class="hljs-number">.433</span>,-<span class="hljs-number">.25</span>],[<span class="hljs-number">.433</span>,-<span class="hljs-number">.25</span>]]
<span class="hljs-attribute">circle</span>: <span class="hljs-comment"># Note: NetLogo's dot is simply circle with a small size</span>
<span class="hljs-attribute">shortcut</span>: <span class="hljs-function"><span class="hljs-params">(c,x,y,s)</span> -></span> c.beginPath(); circ c,x,y,s; c.closePath(); c.fill()
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">false</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span> circ c,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span> <span class="hljs-comment"># c.arc 0,0,.5,0,2*Math.PI</span>
<span class="hljs-attribute">square</span>:
<span class="hljs-attribute">shortcut</span>: <span class="hljs-function"><span class="hljs-params">(c,x,y,s)</span> -></span> csq c,x,y,s
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">false</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span> csq c,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span> <span class="hljs-comment">#c.fillRect -.5,-.5,1,1</span>
<span class="hljs-attribute">pentagon</span>:
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">false</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span> poly c, [[<span class="hljs-number">0</span>,<span class="hljs-number">.45</span>],[-<span class="hljs-number">.45</span>,<span class="hljs-number">.1</span>],[-<span class="hljs-number">.3</span>,-<span class="hljs-number">.45</span>],[<span class="hljs-number">.3</span>,-<span class="hljs-number">.45</span>],[<span class="hljs-number">.45</span>,<span class="hljs-number">.1</span>]]
<span class="hljs-attribute">ring</span>:
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">false</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span> circ c,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>; c.closePath(); ccirc c,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">.6</span>
<span class="hljs-attribute">filledRing</span>:
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">false</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span>
circ(c,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>)
tempStyle = c.fillStyle <span class="hljs-comment"># save fill style</span>
c.fillStyle = c.strokeStyle <span class="hljs-comment"># use stroke style for larger circle</span>
c.fill()
c.fillStyle = tempStyle
c.beginPath()
circ(c,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">.8</span>)
<span class="hljs-attribute">person</span>:
<span class="hljs-attribute">rotate</span>: <span class="hljs-literal">false</span>
<span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span>
poly c, [ [<span class="hljs-number">.15</span>,<span class="hljs-number">.2</span>],[<span class="hljs-number">.3</span>,<span class="hljs-number">0</span>],[<span class="hljs-number">.125</span>,-<span class="hljs-number">.1</span>],[<span class="hljs-number">.125</span>,<span class="hljs-number">.05</span>],
[<span class="hljs-number">.1</span>,-<span class="hljs-number">.15</span>],[<span class="hljs-number">.25</span>,-<span class="hljs-number">.5</span>],[<span class="hljs-number">.05</span>,-<span class="hljs-number">.5</span>],[<span class="hljs-number">0</span>,-<span class="hljs-number">.25</span>],
[-<span class="hljs-number">.05</span>,-<span class="hljs-number">.5</span>],[-<span class="hljs-number">.25</span>,-<span class="hljs-number">.5</span>],[-<span class="hljs-number">.1</span>,-<span class="hljs-number">.15</span>],[-<span class="hljs-number">.125</span>,<span class="hljs-number">.05</span>],
[-<span class="hljs-number">.125</span>,-<span class="hljs-number">.1</span>],[-<span class="hljs-number">.3</span>,<span class="hljs-number">0</span>],[-<span class="hljs-number">.15</span>,<span class="hljs-number">.2</span>] ]
c.closePath(); circ c,<span class="hljs-number">0</span>,<span class="hljs-number">.35</span>,<span class="hljs-number">.30</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Return a list of the available shapes, see above.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">names</span>: <span class="hljs-function">-></span>
(name <span class="hljs-keyword">for</span> own name, val <span class="hljs-keyword">of</span> @ <span class="hljs-keyword">when</span> val.rotate? <span class="hljs-keyword">and</span> val.draw?)</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Add your own shape. Will be included in names list. Usage:</p>
<pre><code>ABM.Shapes.add <span class="hljs-string">"test"</span>, <span class="hljs-literal">true</span>, <span class="hljs-function"><span class="hljs-params">(c)</span> -></span> <span class="hljs-comment"># bowtie/hourglass</span>
ABM.Shapes.poly c, [[-<span class="hljs-number">.5</span>,-<span class="hljs-number">.5</span>],[<span class="hljs-number">.5</span>,<span class="hljs-number">.5</span>],[-<span class="hljs-number">.5</span>,<span class="hljs-number">.5</span>],[<span class="hljs-number">.5</span>,-<span class="hljs-number">.5</span>]]
</code></pre><p>Note: an image that is not rotated automatically gets a shortcut.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">add</span>: <span class="hljs-function"><span class="hljs-params">(name, rotate, draw, shortcut)</span> -></span> <span class="hljs-comment"># draw can be an image, shortcut defaults to null</span>
s = @[name] =
<span class="hljs-keyword">if</span> u.isFunction draw <span class="hljs-keyword">then</span> {rotate,draw} <span class="hljs-keyword">else</span> {rotate,<span class="hljs-attribute">img</span>:draw,<span class="hljs-attribute">draw</span>:<span class="hljs-function"><span class="hljs-params">(c)</span>-></span>cimg c,<span class="hljs-number">.5</span>,<span class="hljs-number">.5</span>,<span class="hljs-number">1</span>,<span class="hljs-property">@img</span>}
(s.shortcut = <span class="hljs-function"><span class="hljs-params">(c,x,y,s)</span> -></span> cimg c,x,y,s,<span class="hljs-property">@img</span>) <span class="hljs-keyword">if</span> s.img? <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> s.rotate
s.shortcut = shortcut <span class="hljs-keyword">if</span> shortcut? <span class="hljs-comment"># can override img default shortcut if needed</span></pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Add local private objects for use by add() and debugging</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">poly</span>:poly, <span class="hljs-attribute">circ</span>:circ, <span class="hljs-attribute">ccirc</span>:ccirc, <span class="hljs-attribute">cimg</span>:cimg, <span class="hljs-attribute">csq</span>:csq
<span class="hljs-attribute">spriteSheets</span>:spriteSheets <span class="hljs-comment"># export spriteSheets for debugging, showing in html</span></pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Two draw procedures, one for shapes, the other for sprites made from shapes.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(ctx, shape, x, y, size, rad, color, strokeColor)</span> -></span>
<span class="hljs-keyword">if</span> shape.shortcut?
ctx.fillStyle = color.css <span class="hljs-keyword">unless</span> shape.img? <span class="hljs-comment"># u.colorStr color</span>
shape.shortcut ctx,x,y,size
<span class="hljs-keyword">else</span>
ctx.save()
ctx.translate x, y
ctx.scale size, size <span class="hljs-keyword">if</span> size <span class="hljs-keyword">isnt</span> <span class="hljs-number">1</span>
ctx.rotate rad <span class="hljs-keyword">if</span> rad <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">if</span> shape.img? <span class="hljs-comment"># is an image, not a path function</span>
shape.draw ctx
<span class="hljs-keyword">else</span>
ctx.fillStyle = color.css <span class="hljs-comment"># u.colorStr color</span>
ctx.strokeStyle = strokeColor.css <span class="hljs-keyword">if</span> strokeColor <span class="hljs-comment"># u.colorStr color</span>
ctx.beginPath(); shape.draw ctx; ctx.closePath()
ctx.fill()
ctx.restore()
shape</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Draw a sprite, called by turtles. The world transform is in effect.
see <a href="http://goo.gl/VUlhY">this post</a> for drawing centered rotated images
The sprite (s) properties are in pixels, x,y,size in world coordinates.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">drawSprite</span>: <span class="hljs-function"><span class="hljs-params">(ctx, s, x, y, size, rad)</span> -></span>
<span class="hljs-keyword">if</span> rad <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
ctx.drawImage s.ctx.canvas, s.x, s.y, s.spriteSize, s.spriteSize,
x-size/<span class="hljs-number">2</span>, y-size/<span class="hljs-number">2</span>, size, size
<span class="hljs-keyword">else</span>
ctx.save()
ctx.translate x, y
ctx.rotate rad
ctx.drawImage s.ctx.canvas, s.x, s.y, s.spriteSize, s.spriteSize,
-size/<span class="hljs-number">2</span>,-size/<span class="hljs-number">2</span>, size, size
ctx.restore()
s</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Convert a shape to a sprite by allocating a sprite sheet “slot” and drawing
the shape to fit it. Return existing sprite if duplicate.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">shapeToSprite</span>: <span class="hljs-function"><span class="hljs-params">(name, color, size, strokeColor)</span> -></span>
color = Color.convertColor color, <span class="hljs-string">"css"</span> <span class="hljs-comment"># if we're called directly by pgmr</span>
strokeColor = Color.convertColor strokeColor, <span class="hljs-string">"css"</span> <span class="hljs-keyword">if</span> strokeColor?
spriteSize = Math.ceil size
shape = @[name]
index = <span class="hljs-keyword">if</span> shape.img? <span class="hljs-keyword">then</span> name <span class="hljs-keyword">else</span> <span class="hljs-string">"<span class="hljs-subst">#{name}</span>-<span class="hljs-subst">#{color}</span>"</span>
ctx = spriteSheets[spriteSize]</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Create sheet for this bit size if it does not yet exist</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">unless</span> ctx?
spriteSheets[spriteSize] = ctx = u.createCtx spriteSize*<span class="hljs-number">10</span>, spriteSize
ctx.nextX = <span class="hljs-number">0</span>; ctx.nextY = <span class="hljs-number">0</span>; ctx.index = {}</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Return matching sprite if index match found</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> foundSlot <span class="hljs-keyword">if</span> (foundSlot = ctx.index[index])?</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Extend the sheet if we’re out of space</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> spriteSize*ctx.nextX <span class="hljs-keyword">is</span> ctx.canvas.width
u.resizeCtx ctx, ctx.canvas.width, ctx.canvas.height+spriteSize
ctx.nextX = <span class="hljs-number">0</span>; ctx.nextY++</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>Create the sprite “slot” object and install in index object</p>
</div>
<div class="content"><div class='highlight'><pre> x = spriteSize*ctx.nextX; y = spriteSize*ctx.nextY
slot = {ctx, x, y, spriteSize, name, color, strokeColor, index}
ctx.index[index] = slot</pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>Draw the shape into the sprite slot</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (img=shape.img)? <span class="hljs-comment"># is an image, not a path function</span>
<span class="hljs-keyword">if</span> img.height <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> fillSlot(slot, img)
<span class="hljs-keyword">else</span> img.onload = <span class="hljs-function">-></span> fillSlot(slot, img)
<span class="hljs-keyword">else</span>
ctx.save()
ctx.scale spriteSize, spriteSize
ctx.translate ctx.nextX+<span class="hljs-number">.5</span>, ctx.nextY+<span class="hljs-number">.5</span>
ctx.fillStyle = color <span class="hljs-comment"># u.colorStr color</span>
ctx.strokeStyle = strokeColor <span class="hljs-keyword">if</span> strokeColor
ctx.beginPath(); shape.draw ctx; ctx.closePath()
ctx.fill()
ctx.restore()
ctx.nextX++
slot <span class="hljs-comment"># return the sprite (slot)</span></pre></div></div>
</li>
</ul>
</div>
</body>
</html>