-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathShapes.js
305 lines (285 loc) · 8.32 KB
/
Shapes.js
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
import * as util from './utils.js'
import * as emoji from './emoji.js'
function cssColor(color) {
if (color) return color.css || color
return color
}
// Shapes are Canvas2D drawings in -0.5 to +0.5, unit squares
// They are drawn on a Canvas2D using transforms for x, y, theta
class Shapes {
constructor() {
this.cache = {}
this.paths = paths // For accessing the paths thru the Shapes instance
}
// Add a new path. Error if name already used
addPath(name, pathFunction) {
if (this.getPath(name)) Error('addPath: ${name} already defined')
paths[name] = pathFunction
}
// NOTE: convention: if shapeName ends in 2, then needs strokeColor
needsStrokeColor(shapeName) {
return shapeName.endsWith('2')
}
// Get an array of path names. Can change over time via addPath()
getPathNames() {
return Object.keys(paths)
}
// Get a path drawing function by name. Returns 'undefined' if not found.
getPath(name) {
return paths[name]
}
// Return random shape function. It's name is shape.name
oneOf() {
// return paths[util.oneValOf(this.getPathNames())]
return util.oneValOf(paths)
}
// Return the name/path at index of path array.
// Wrap the index to be within the array.
nameAtIndex(index) {
const names = this.getPathNames()
return names[util.mod(index, names.length)]
}
atIndex(index) {
const name = this.nameAtIndex(index)
return paths[name]
}
imagePathPromise(name, imgURL) {
return util.imagePromise(imgURL).then(img => {
this.createImagePath(name, img)
})
}
// Create a shape that is an imageable (img, canvas, ...)
createImagePath(name, img, flip = true) {
if (!util.isImageable(img)) {
throw Error('Shapes createImagePath: img not an imageable ' + img)
}
if (flip) img = flipImage(img)
function imagePath(ctx) {
ctx.drawImage(img, -0.5, -0.5, 1, 1)
}
// paths[name] = imagePath
this.addPath(name, imagePath)
}
// createEmojiPath(name, codePoint) {
// const can = emoji.emoji2can(codePoint)
// this.createImagePath(name, can)
// }
createEmojiPath(name, codePoint) {
const can = emoji.emoji2can(codePoint)
this.createImagePath(name, can)
}
imageName(name, pixels, fill, stroke) {
const path = this.getPath(name)
if (!Number.isInteger(pixels))
throw Error(`imageName: pixels is not integer: ${name}`)
if (!path) throw Error(`imageName: ${name} not in Shapes`)
if (path.name === 'imagePath') return `${name}_${pixels}_image`
if (!fill) throw Error(`imageName: No color for shape ${name}`)
// OK to give strokeColor when not needed.
if (!this.needsStrokeColor(name)) stroke = null
return `${name}_${pixels}_${fill}${stroke ? `_${stroke}` : ''}`
}
shapeToImage(name, pixels, fill, stroke) {
pixels = Math.ceil(pixels)
const imgName = this.imageName(name, pixels, fill, stroke)
if (this.cache && this.cache[imgName]) return this.cache[imgName]
const ctx = util.createCtx(pixels, pixels)
ctx.fillStyle = cssColor(fill)
ctx.strokeStyle = cssColor(stroke)
ctx.scale(pixels, -pixels)
ctx.translate(0.5, -0.5)
ctx.beginPath()
paths[name](ctx)
ctx.closePath()
ctx.fill()
ctx.canvas.name = imgName
if (this.cache) this.cache[imgName] = ctx.canvas
return ctx.canvas
}
imageNameToImage(imageName) {
const [name, pixels, fill, stroke] = imageName.split('_')
return this.shapeToImage(name, pixels, fill, stroke)
}
}
// Utilities:
// Flip an image so will be upright in euclidean coords
function flipImage(img) {
const { width, height } = img
const ctx = util.createCtx(width, height)
ctx.scale(1, -1)
ctx.drawImage(img, 0, -height)
return ctx.canvas
}
// Centered polygon, x,y in [-1, 1] (size = 2)
function poly(ctx, points) {
points.forEach((pt, i) => {
if (i === 0) ctx.moveTo(pt[0], pt[1])
else ctx.lineTo(pt[0], pt[1])
})
}
// centered circle
function circle(ctx, x, y, radius, anticlockwise = false) {
ctx.arc(x, y, radius, 0, 2 * Math.PI, anticlockwise)
}
// centered square
function square(ctx, x, y, size) {
ctx.fillRect(x - size / 2, y - size / 2, size, size)
}
// The paths object containing shape path procedures for common shapes.
// Paths are unit size, -.5 -> +.5
// Use "addPath()" to add your own new shapes.
//
// These are "upsidedown", i.e. not upright. This is so we can traansform
// to euclidean coords.
// Does not impact most paths. But will impact images & person, person2
const paths = {
// default(ctx) {
// this.dart(ctx)
// },
arrow(ctx) {
poly(ctx, [
[0.5, 0],
[0, 0.5],
[0, 0.2],
[-0.5, 0.2],
[-0.5, -0.2],
[0, -0.2],
[0, -0.5],
])
},
bug(ctx) {
ctx.strokeStyle = ctx.fillStyle
this.bug2(ctx)
},
bug2(ctx) {
ctx.lineWidth = 0.05
poly(ctx, [
[0.4, 0.225],
[0.2, 0],
[0.4, -0.225],
])
ctx.stroke()
ctx.beginPath()
circle(ctx, 0.12, 0, 0.13)
circle(ctx, -0.05, 0, 0.13)
circle(ctx, -0.27, 0, 0.2)
},
circle(ctx) {
// ctx.arc(0, 0, 1, 0, 2 * Math.PI)
circle(ctx, 0, 0, 0.5)
},
dart(ctx) {
poly(ctx, [
[0.5, 0],
[-0.5, 0.4],
[-0.25, 0],
[-0.5, -0.4],
])
},
frame(ctx) {
const inset = 0.2
const r = 0.5 - inset
poly(ctx, [
[-0.5, -0.5],
[0.5, -0.5],
[0.5, 0.5],
[-0.5, 0.5],
]) //cclockwise
ctx.closePath()
// reverse direction for non-zero winding rule to leave empty center rect.
poly(ctx, [
[-r, -r],
[-r, r],
[r, r],
[r, -r],
]) // clockwise
},
frame2(ctx) {
const inset = 0.2
square(ctx, 0, 0, 1)
ctx.fillStyle = ctx.strokeStyle
square(ctx, 0, 0, 1 - 2 * inset) //2 - 2 * inset)
},
person(ctx) {
ctx.strokeStyle = ctx.fillStyle
this.person2(ctx)
},
person2(ctx) {
poly(ctx, [
[0.15, 0.2],
[0.3, 0],
[0.125, -0.1],
[0.125, 0.05],
[0.1, -0.15],
[0.25, -0.5],
[0.05, -0.5],
[0, -0.25],
[-0.05, -0.5],
[-0.25, -0.5],
[-0.1, -0.15],
[-0.125, 0.05],
[-0.125, -0.1],
[-0.3, 0],
[-0.15, 0.2],
])
ctx.closePath()
ctx.fill()
ctx.beginPath()
ctx.fillStyle = ctx.strokeStyle
circle(ctx, 0, 0.35, 0.15)
},
ring(ctx) {
const [rOuter, rInner] = [0.5, 0.3]
circle(ctx, 0, 0, rOuter)
ctx.lineTo(rInner, 0)
circle(ctx, 0, 0, rInner, true)
},
ring2(ctx) {
// fileStyle is outer color, strokeStyle inner color
const [rOuter, rInner] = [0.5, 0.3]
circle(ctx, 0, 0, rOuter)
ctx.closePath()
ctx.fill()
ctx.beginPath()
ctx.fillStyle = ctx.strokeStyle
circle(ctx, 0, 0, rInner)
},
square(ctx) {
square(ctx, 0, 0, 1)
},
triangle(ctx) {
poly(ctx, [
[0.5, 0],
[-0.5, -0.4],
[-0.5, 0.4],
])
},
pentagon(ctx) {
poly(ctx, [
[0, -0.5],
[0.48, -0.15],
[0.29, 0.4],
[-0.29, 0.4],
[-0.48, -0.15],
])
},
butterfly(ctx) {
poly(ctx, [
[-0.5, -0.5],
[0.5, -0.5],
[-0.5, 0.5],
[0.5, 0.5],
])
},
}
export default Shapes
/*
ToDo:
- simplify async images: make a "slot" then fill it.
- manage inversion/upright. Only needed for: person/2, images
- optimize:
- cache images .. like spritesheet
- use shortcuts via the helper functions (avoid transforms)
See shapes.coffee in as0
- paths: use object w/ meta-data, & draw(). needsStroke, for example
*/