/
node2.ts
77 lines (69 loc) · 2.14 KB
/
node2.ts
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
import type { ICopy, IToHiccup, Nullable } from "@thi.ng/api";
import { isNumber } from "@thi.ng/checks";
import { invert23, mulM23, mulV23, transform23 } from "@thi.ng/matrices";
import { ReadonlyVec, set2, Vec } from "@thi.ng/vectors";
import { ANode } from "./anode";
import type { ISceneNode } from "./api";
import { toHiccup } from "./hiccup";
export class Node2D
extends ANode<Node2D>
implements ICopy<Node2D>, ISceneNode<Node2D>, IToHiccup {
translate: Vec;
rotate: number;
scale: Vec | number;
constructor(
id: string,
parent?: Nullable<Node2D>,
translate: Vec = [0, 0],
rotate = 0,
scale: Vec | number = 1,
body?: any
) {
super(id, parent, body);
this.translate = translate;
this.rotate = rotate;
this.scale = isNumber(scale) ? [scale, scale] : scale;
this.update();
}
copy() {
return new Node2D(
this.id,
this.parent,
set2([], this.translate),
this.rotate,
set2([], <Vec>this.scale),
this.body
);
}
deleteChild(node: number | Node2D) {
return this._deleteChild(node, Node2D);
}
update() {
if (this.enabled) {
this.mat = transform23([], this.translate, this.rotate, this.scale);
if (this.parent) {
mulM23(this.mat, this.parent.mat, this.mat);
}
invert23(this.invMat, this.mat);
for (let c of this.children) {
c.update();
}
}
}
mapGlobalPoint(p: ReadonlyVec) {
return mulV23([], this.invMat, p);
}
mapLocalPointToNode(dest: Node2D, p: ReadonlyVec) {
return mulV23(null, dest.invMat, mulV23([], this.mat, p));
}
/**
* By implementing this method (`IToHiccup` interface), scene graph nodes
* can be directly used by hdom-canvas, hiccup-canvas, rdom-canvas or
* hiccup-svg (for the latter one needs to call `convertTree()` first).
*
* @param ctx - arbitrary user data
*/
toHiccup(ctx?: any): any {
return toHiccup<Node2D>(this, ctx);
}
}