-
Notifications
You must be signed in to change notification settings - Fork 0
/
libg.js
527 lines (465 loc) · 17.1 KB
/
libg.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
"use strict"
// **** mini scene and canvas based graphics lib ****
//depends on lib.js
//author: Onne Gorter
//license: CC0
/// This mini canvas graphics library maintains a scene graph based on `Layer`s (and `Box`es).
/// It makes it easy to load resources and build and maintain a scene graph. And to receive
/// and process events per object. The `Layer`s have `delegate`s that do the actual rendering
/// and event handing. Both graphics and images use native objects (`CanvasRenderingContext2D`
/// context and `Image`).
var _default_scene = null
// wraps native DOM events
class GEvent {
constructor(x, y, globalx, globaly, event, target, last, delta) {
this.x = x /// x coordinate relative to this.target
this.y = y /// y coordinate relative to this.target
this.globalx = globalx /// x coordinate absolute to root of scene
this.globaly = globaly /// y coordinate absolute to root of scene
this.target = target /// the target of this event
this.domevent = event /// the native DOM event
this.last = last || false /// set to true if this is the last event of a follow
this.delta = delta || null /// set to {x,y} as difference from start of follow
}
/// convert to a global point
toGlobal() {
return {x:this.globalx, y:this.globaly}
}
/// follow a `ondown` event, and call `cb` for every `onmove` event
/// last call will be from an `onup` event and `event.last` will be `true`
follow(cb) {
if (this.domevent instanceof Touch) return startFollowTouch(this.domevent, cb, this)
pushFollowHandlers(cb, this)
}
}
function runEvent(target, x, y, globalx, globaly, event, name) {
var handler = null
if (target.delegate) handler = target.delegate[name]
if (!handler) return false
var passToParent = handler.call(target.delegate, new GEvent(x, y, globalx, globaly, event, target))
return !passToParent
}
function startFollowTouch(touch, handler, event) {
_followtouch[touch.identifier] = {handler, target: event.target, start:{x:event.globalx, y:event.globaly}}
}
function followTouch(follow, touch, globalx, globaly, name) {
var handler = follow.handler
var target = follow.target
var start = follow.start
if (!handler) return
assert(target)
var l = target.toGlobal()
var x = globalx - l.x
var y = globaly - l.y
var delta = {x:globalx - start.x, y:globaly - start.y}
var last = name === "onup"
handler.call(target.delegate, new GEvent(x, y, globalx, globaly, touch, target, last, delta))
_default_scene.schedule()
}
function pushFollowHandlers(handler, event) {
_default_scene.follow = handler
_default_scene.followtarget = event.target
_default_scene.followstart = {x:event.globalx, y:event.globaly}
_default_scene.c.onmousemove = followmousemove
_default_scene.c.onmouseup = followmouseup
}
function restoreMouseHandlers() {
_default_scene.follow = null
_default_scene.followtarget = null
_default_scene.c.onmousemove = _default_scene.onmousemove
_default_scene.c.onmouseup = _default_scene.onmouseup
}
function _follow2(event, last) {
var handler = _default_scene.follow
var target = _default_scene.followtarget
var start = _default_scene.followstart
if (!handler) return
assert(target)
var globalx = event.offsetX
var globaly = event.offsetY
var l = target.toGlobal()
var x = globalx - l.x
var y = globaly - l.y
var delta = {x:globalx - start.x, y:globaly - start.y}
handler.call(target.delegate, new GEvent(x, y, globalx, globaly, event, target, last, delta))
event.preventDefault()
_default_scene.schedule()
}
function followmousemove(event) {
_follow2(event, false)
}
function followmouseup(event) {
_follow2(event, true)
restoreMouseHandlers()
}
/// scene
class GScene {
constructor(g, c, width, height) {
this.root = new Box() /// the root of the scene, `add()` other `Layer`s, set its `style`, or give it a `delegate`
this.onready = null /// handler that is called after all assets have loaded
this.onrender = null /// handler that is called before rerendering scene
// private
this.g = g
this.c = c
this.width = c.width = g.width = width
this.height = c.height = g.height = height
this.imageloader = null
if (!_default_scene) _default_scene = this
this.schedule()
}
draw(layer) {
var g = this.g
g.save()
g.translate(layer.x, layer.y)
if (!fequals(layer.rotate, 0.001)) g.rotate(layer.rotate)
layer.draw(g, this)
g.restore()
}
render() {
this.scheduled = false
if (this.imageloader && this.imageloader.loading > 0) return
if (this.onrender) this.onrender()
var root = this.root
root.x = root.y = 0
root.width = this.c.width = this.width
root.height = this.c.height = this.height
this.g.font = "24px Arial"
this.g.save()
this.draw(root)
this.g.restore()
}
/// call if some external event should rerender scene, harmless to call multiple times
/// internal events like `ondown`, or images that load, will call this automatically
schedule() {
if (this.scheduled) return
this.scheduled = true
requestAnimationFrame(_renderContext)
}
/// load an image from `url`, returns the img immediately
/// use `onready` to wait for all assets to have loaded
image(url) {
if (!this.imageloader) this.imageloader = new ImageLoader(this)
return this.imageloader.fetch(url)
}
}
function _renderContext() {
_default_scene.render()
}
function imageCheck(img) {
if (!img.complete) return false
if (img.naturalWidth !== "undefined" && img.naturalWidth === 0) return false
return true
}
function roundedRect(g, x, y, width, height, r) {
g.beginPath()
g.moveTo(x + r, y)
g.lineTo(x + width - r, y)
g.quadraticCurveTo(x + width, y, x + width, y + r)
g.lineTo(x + width, y + height - r)
g.quadraticCurveTo(x + width, y + height, x + width - r, y + height)
g.lineTo(x + r, y + height)
g.quadraticCurveTo(x, y + height, x, y + height - r)
g.lineTo(x, y + r)
g.quadraticCurveTo(x, y, x + r, y)
g.closePath()
}
class ImageLoader {
constructor(scene) {
this.images = {}
this.loading = 0
this.scene = scene
}
fetch(url) {
var img = this.images[url]
if (img) return img
this.loading += 1
img = this.images[url] = new Image()
img.onload = ()=> {
console.log("loaded:", img.src)
this.loaded()
}
img.onerror = (event)=> {
console.warn("error loading:", img.src)
this.loaded()
}
img.src = url
return img
}
loaded() {
this.loading -= 1
assert(this.loading >= 0)
if (this.loading === 0) {
console.log("all assets loaded")
if (this.scene) this.scene.schedule()
}
}
}
/// create a new scene, give it a canvas, and insert it in the dom
function createScene(width, height, parent) {
if (typeof(parent) === "string") parent = document.getElementById(parent)
parent = parent || document.body
var c = document.createElement("canvas")
c.setAttribute("style", "-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:-moz-none;-o-user-select:none;user-select:none;")
parent.appendChild(c)
var g = c.getContext("2d")
return new GScene(g, c, width || 400, height || 300)
}
function ensureMouseEvents(context, eventname, name) {
if (context[eventname]) return
context[eventname] = context.c[eventname] = (event)=> {
context.schedule()
var globalx = event.offsetX
var globaly = event.offsetY
var done = context.root.fireEvent(globalx, globaly, globalx, globaly, event, name)
if (done) event.preventDefault()
}
}
var _followtouch = {}
var _multitouch = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch
function ensureTouchEvents(context, eventname, name) {
if (!_multitouch) return
if (context[eventname]) return
context[eventname] = context.c[eventname] = (event)=> {
context.schedule()
for (var i = 0; i < event.changedTouches.length; i++) {
var touch = event.changedTouches[i]
var globalx = touch.pageX - touch.target.offsetLeft
var globaly = touch.pageY - touch.target.offsetTop
var follow = _followtouch[touch.identifier]
if (follow) {
event.preventDefault()
followTouch(follow, touch, globalx, globaly, name)
if (eventname === "ontouchend" || eventname === "ontouchcancel") delete _followtouch[touch.identifier]
assert(Object.keys(_followtouch).length < 10)
continue
}
var done = context.root.fireEvent(globalx, globaly, globalx, globaly, touch, name)
if (done) event.preventDefault()
}
}
}
// if a delegate has event handlers, make the layer interactive and ensure the handlers are set
function ensureInteractive(layer, delegate) {
layer.interactive = false
if (!delegate) return
// TODO perhaps get scene from layer? not possible at the moment
var context = _default_scene
if (!context) return
if (delegate["ondown"]) {
layer.interactive = true
ensureMouseEvents(context, "onmousedown", "ondown")
ensureTouchEvents(context, "ontouchstart", "ondown")
ensureTouchEvents(context, "ontouchend", "onup") // the rest is here for follow ...
ensureTouchEvents(context, "ontouchcancel", "onup")
ensureTouchEvents(context, "ontouchmove", "onmove")
}
if (delegate["onup"]) {
layer.interactive = true
ensureMouseEvents(context, "onmouseup", "onup")
ensureTouchEvents(context, "ontouchend", "onup")
ensureTouchEvents(context, "ontouchcancel", "onup")
}
if (delegate["onmove"]) {
layer.interactive = true
ensureMouseEvents(context, "onmousemove", "onmove")
ensureTouchEvents(context, "ontouchmove", "onmove")
ensureTouchEvents(context, "ontouchend", "onup")
ensureTouchEvents(context, "ontouchcancel", "onup")
}
}
/// a layer that can be rendered
/// set its `style` properties to render `{background:"red"}`
/// give it a `delegate` that has methods to `draw(g)` or receive events (`ondown`, `onup`, `onmove`)
/// if the `delegate` is a function, it is assumed to be the draw function
class Layer {
constructor(style, delegate) {
style = style || {}
delegate = delegate || style.delegate || null /// delegate
this.x = style.x || 0 /// x coordinate
this.y = style.y || 0 /// y coordinate
this.z = style.z || 0 /// z index, the highest z-index is drawn last
this.width = style.width || 0 /// width of layer
this.height = style.height || 0 /// height of layer
this.rotate = style.rotate || 0 /// rotation of layer, only used for rendering, events ignore rotation
this.style = style /// style of this object, can be used to set `background` color and such
this.delegate = null /// delegate, to `draw(g)` or handle events
// private
this.parent = null
this.interactive = false
this.setDelegate(delegate)
if (style.parent) style.parent.add(this)
}
setDelegate(delegate) {
this.delegate = delegate
ensureInteractive(this, delegate)
}
/// remove this layer from the scene, can re-add afterwards
remove() {
if (!this.parent) return
this.parent.removeChild(this)
assert(!this.parent)
}
toGlobalXY(x, y) {
if (!this.parent) return {x:this.x + x, y:this.y + y}
return this.parent.toGlobalXY(this.x + x, this.y + y)
}
toGlobal() {
if (!this.parent) return {x:this.x, y:this.y}
return this.parent.toGlobalXY(this.x, this.y)
}
/// from a point or event, return an object with relative coords
toLocal(point) {
if (point instanceof GEvent) {
var p = point.toGlobal()
var l = this.toGlobal()
p.x -= l.x
p.y -= l.y
return p
}
l = this.toGlobal()
l.x = point.x - l.x
l.y = point.y - l.y
return l
}
/// from a point or event, return if the point is in this layer
contains(point) {
var l = this.toLocal(point)
return l.x >= 0 && l.x <= this.width && l.y >= 0 && l.y <= this.height
}
/// return true if {x,y} in local coords are in this layer
containsLocalXY(x, y) {
return x >= 0 && x <= this.width && y >= 0 && y <= this.height
}
/// return true if {x,y} as global coords are in this layer
containsXY(x, y) {
return this.contains({x,y})
}
// private
draw(g, scene) {
if (this.style.background || this.style.border) {
if (this.style.borderRadius) {
roundedRect(g, 0, 0, this.width, this.height, this.style.borderRadius)
} else {
g.beginPath()
g.rect(0, 0, this.width, this.height)
}
if (this.style.background) {
g.fillStyle = this.style.background
g.fill()
}
if (this.style.border) {
g.strokeStyle = this.style.border
g.lineWidth = this.style.borderWidth || 1
g.stroke()
}
}
if (this.style.backgroundImage && imageCheck(this.style.backgroundImage)) {
g.drawImage(this.style.backgroundImage, 0, 0, this.width, this.height)
}
if (this.delegate) {
if (typeof(this.delegate) === "function") {
g.beginPath()
return this.delegate(g, this, scene)
}
if (this.delegate.draw) {
g.beginPath()
return this.delegate.draw(g, this, scene)
}
}
}
fireEvent(x, y, globalx, globaly, event, name) {
if (!this.interactive || !this.containsLocalXY(x, y)) return false
return runEvent(this, x, y, globalx, globaly, event, name)
}
}
function zsortfn(left, right) { return left.z - right.z }
function zsort(ls) {
var il = ls.length
if (il === 0) return ls
var z = ls[0]
var sort = false
for (var i = 1; i < il; i++) {
if (ls[i].z !== z) { sort = true; break }
}
if (!sort) return ls
return ls.slice().stablesort(zsortfn)
}
/// a container for layers, is a layer itself
class Box extends Layer {
constructor(delegate, width, height) {
super(delegate, width, height)
this.children = []
}
/// add layers, may also pass in objects that have a `layer` property
add() {
for (var i = 0, il = arguments.length; i < il; i++) {
var object = arguments[i]
var layer = (object instanceof Layer)? object : object.layer
assert(layer instanceof Layer)
assert(!layer.parent)
layer.parent = this
this.children.push(layer)
if (object !== layer && !layer.delegate) layer.setDelegate(object)
}
}
/// remove a layer, may also pass in object that has a `layer` property
removeChild(child) {
var layer = (child instanceof Layer)? child : child.layer
assert(layer.parent === this)
assert(this.children.indexOf(layer) >= 0)
layer.parent = null
this.children.remove(layer)
assert(this.children.indexOf(layer) < 0)
}
// private
draw(g, scene) {
super.draw(g, scene)
var cs = zsort(this.children)
for (var i = 0, il = cs.length; i < il; i++) {
var layer = cs[i]
assert(layer instanceof Layer)
assert(layer.parent === this)
scene.draw(layer)
}
}
fireEvent(x, y, globalx, globaly, event, name) {
// TODO should take zindex sorting into account
for (var i = 0, il = this.children.length; i < il; i++) {
var layer = this.children[il - 1 - i]
assert(layer instanceof Layer)
assert(layer.parent === this)
var done = layer.fireEvent(x - layer.x, y - layer.y, globalx, globaly, event, name)
if (done) return done
}
if (!this.interactive || !this.containsLocalXY(x, y)) return false
return runEvent(this, x, y, globalx, globaly, event, name)
}
layout() {
// TODO actually implement
}
}
// TODO layouting with rows/cols padding/margin etc
class Column extends Box {
layout() {
}
}
class Row extends Box {
layout() {
}
}
function col() {
var box = new Column()
for (var i = 0, il = arguments.length; i < il; i++) {
var child = arguments[i]
if (child) box.add(child)
}
return box
}
function row() {
var box = new Row()
for (var i = 0, il = arguments.length; i < il; i++) {
var child = arguments[i]
if (child) box.add(child)
}
return box
}