UNPKG

@thi.ng/scenegraph

Version:

Extensible 2D/3D scene graph with @thi.ng/hiccup-canvas support

82 lines (81 loc) 1.66 kB
import { isNumber } from "@thi.ng/checks/is-number"; import { assert } from "@thi.ng/errors/assert"; class ANode { id; parent; children; body; mat; invMat; enabled; display; constructor({ id, parent, body, enabled = true, display = true }) { this.id = id; this.parent = parent; this.children = []; if (parent) { parent.appendChild(this); } this.body = body; this.mat = []; this.invMat = []; this.enabled = enabled; this.display = display; } appendChild(node) { this.children.push(node); return this; } insertChild(i, node) { const children = this.children; i < 0 && (i += children.length); assert(i >= 0 && i <= children.length, "index out of bounds"); children.splice(i, 0, node); node.parent = this; node.update(); return this; } deleteChild(node) { const { children } = this; const i = isNumber(node) ? node : children.indexOf(node); if (i >= 0 && i < children.length) { children.splice(i, 1); return true; } return false; } draw(ctx) { if (this.display) { for (let c of this.children) { c.draw(ctx); } } } childForPoint(p) { if (this.enabled) { const children = this.children; for (let i = children.length; i-- > 0; ) { const n = children[i].childForPoint(p); if (n) { return n; } } const q = this.mapGlobalPoint(p); if (q && this.containsLocalPoint(q)) { return { node: this, p: q }; } } } containsLocalPoint(_) { return false; } } export { ANode };