@thi.ng/hdom-canvas 4.1.96 → 4.1.98
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/index.js +115 -153
- package/package.json +12 -9
package/CHANGELOG.md
CHANGED
package/index.js
CHANGED
|
@@ -7,165 +7,127 @@ import { equiv, releaseTree } from "@thi.ng/hdom/diff";
|
|
|
7
7
|
import { draw } from "@thi.ng/hiccup-canvas/draw";
|
|
8
8
|
const FN = "function";
|
|
9
9
|
const STR = "string";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
*
|
|
23
|
-
* - Shape component objects with life cycle methods are only partially
|
|
24
|
-
* supported, i.e. only the
|
|
25
|
-
* [`ILifecycle.render()`](https://docs.thi.ng/umbrella/hdom/interfaces/ILifecycle.html#render)
|
|
26
|
-
* &
|
|
27
|
-
* [`ILifecycle.release()`](https://docs.thi.ng/umbrella/hdom/interfaces/ILifecycle.html#release)
|
|
28
|
-
* methods are used (Note, for performance reasons `release` methods are
|
|
29
|
-
* ignored by default. If your shape tree contains stateful components which
|
|
30
|
-
* use the `release` life cycle method, you'll need to explicitly enable the
|
|
31
|
-
* canvas component's `__release` attribute by setting it to `true`).
|
|
32
|
-
* - Currently no event listeners can be assigned to shapes (ignored), though
|
|
33
|
-
* this is planned for a future version. The canvas element itself can of
|
|
34
|
-
* course have event handlers as usual.
|
|
35
|
-
*
|
|
36
|
-
* All embedded component functions receive the user context object just like
|
|
37
|
-
* normal hdom components.
|
|
38
|
-
*
|
|
39
|
-
* For best performance, it's recommended to ensure all resulting shapes
|
|
40
|
-
* elements are provided in already normalized hiccup format (i.e. `[tag,
|
|
41
|
-
* {attribs}, ...]`). That way the `__normalize: false` control attribute can be
|
|
42
|
-
* added either to the canvas component itself (or to individual shapes /
|
|
43
|
-
* groups), and if present, will skip normalization of all children.
|
|
44
|
-
*
|
|
45
|
-
* @param _ - hdom user context (ignored)
|
|
46
|
-
* @param attribs - canvas attribs
|
|
47
|
-
* @param shapes - shape components
|
|
48
|
-
*/
|
|
49
|
-
export const canvas = {
|
|
50
|
-
render(_, attribs, ...body) {
|
|
51
|
-
const cattribs = { ...attribs };
|
|
52
|
-
delete cattribs.__diff;
|
|
53
|
-
delete cattribs.__normalize;
|
|
54
|
-
const dpr = window.devicePixelRatio || 1;
|
|
55
|
-
if (dpr !== 1) {
|
|
56
|
-
!cattribs.style && (cattribs.style = {});
|
|
57
|
-
cattribs.style.width = `${cattribs.width}px`;
|
|
58
|
-
cattribs.style.height = `${cattribs.height}px`;
|
|
59
|
-
cattribs.width *= dpr;
|
|
60
|
-
cattribs.height *= dpr;
|
|
61
|
-
}
|
|
62
|
-
return [
|
|
63
|
-
"canvas",
|
|
64
|
-
cattribs,
|
|
65
|
-
[
|
|
66
|
-
"g",
|
|
67
|
-
{
|
|
68
|
-
__impl: IMPL,
|
|
69
|
-
__diff: attribs.__diff !== false,
|
|
70
|
-
__normalize: attribs.__normalize !== false,
|
|
71
|
-
__release: attribs.__release === true,
|
|
72
|
-
__serialize: false,
|
|
73
|
-
__clear: attribs.__clear,
|
|
74
|
-
scale: dpr !== 1 ? dpr : null,
|
|
75
|
-
},
|
|
76
|
-
...body,
|
|
77
|
-
],
|
|
78
|
-
];
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
/** @internal */
|
|
82
|
-
export const createTree = (_, canvas, tree) => {
|
|
83
|
-
// console.log(Date.now(), "draw");
|
|
84
|
-
const ctx = canvas.getContext("2d");
|
|
85
|
-
assert(!!ctx, "canvas ctx unavailable");
|
|
86
|
-
const attribs = tree[1];
|
|
87
|
-
if (attribs) {
|
|
88
|
-
if (attribs.__skip)
|
|
89
|
-
return;
|
|
90
|
-
if (attribs.__clear !== false) {
|
|
91
|
-
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
92
|
-
}
|
|
10
|
+
const canvas = {
|
|
11
|
+
render(_, attribs, ...body) {
|
|
12
|
+
const cattribs = { ...attribs };
|
|
13
|
+
delete cattribs.__diff;
|
|
14
|
+
delete cattribs.__normalize;
|
|
15
|
+
const dpr = window.devicePixelRatio || 1;
|
|
16
|
+
if (dpr !== 1) {
|
|
17
|
+
!cattribs.style && (cattribs.style = {});
|
|
18
|
+
cattribs.style.width = `${cattribs.width}px`;
|
|
19
|
+
cattribs.style.height = `${cattribs.height}px`;
|
|
20
|
+
cattribs.width *= dpr;
|
|
21
|
+
cattribs.height *= dpr;
|
|
93
22
|
}
|
|
94
|
-
|
|
23
|
+
return [
|
|
24
|
+
"canvas",
|
|
25
|
+
cattribs,
|
|
26
|
+
[
|
|
27
|
+
"g",
|
|
28
|
+
{
|
|
29
|
+
__impl: IMPL,
|
|
30
|
+
__diff: attribs.__diff !== false,
|
|
31
|
+
__normalize: attribs.__normalize !== false,
|
|
32
|
+
__release: attribs.__release === true,
|
|
33
|
+
__serialize: false,
|
|
34
|
+
__clear: attribs.__clear,
|
|
35
|
+
scale: dpr !== 1 ? dpr : null
|
|
36
|
+
},
|
|
37
|
+
...body
|
|
38
|
+
]
|
|
39
|
+
];
|
|
40
|
+
}
|
|
95
41
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
if (typeof tag === STR) {
|
|
107
|
-
const attribs = tree[1];
|
|
108
|
-
if (attribs && attribs.__normalize === false) {
|
|
109
|
-
return tree;
|
|
110
|
-
}
|
|
111
|
-
const res = [tree[0], attribs];
|
|
112
|
-
for (let i = 2, n = tree.length; i < n; i++) {
|
|
113
|
-
const n = normalizeTree(opts, tree[i]);
|
|
114
|
-
n != null && res.push(n);
|
|
115
|
-
}
|
|
116
|
-
return res;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
else if (typeof tree === FN) {
|
|
120
|
-
return normalizeTree(opts, tree(opts.ctx));
|
|
121
|
-
}
|
|
122
|
-
else if (typeof tree.toHiccup === FN) {
|
|
123
|
-
return normalizeTree(opts, tree.toHiccup(opts.ctx));
|
|
124
|
-
}
|
|
125
|
-
else if (typeof tree.deref === FN) {
|
|
126
|
-
return normalizeTree(opts, tree.deref());
|
|
127
|
-
}
|
|
128
|
-
else if (isNotStringAndIterable(tree)) {
|
|
129
|
-
const res = [];
|
|
130
|
-
for (let t of tree) {
|
|
131
|
-
const n = normalizeTree(opts, t);
|
|
132
|
-
n != null && res.push(n);
|
|
133
|
-
}
|
|
134
|
-
return res;
|
|
42
|
+
const createTree = (_, canvas2, tree) => {
|
|
43
|
+
const ctx = canvas2.getContext("2d");
|
|
44
|
+
assert(!!ctx, "canvas ctx unavailable");
|
|
45
|
+
const attribs = tree[1];
|
|
46
|
+
if (attribs) {
|
|
47
|
+
if (attribs.__skip)
|
|
48
|
+
return;
|
|
49
|
+
if (attribs.__clear !== false) {
|
|
50
|
+
ctx.clearRect(0, 0, canvas2.width, canvas2.height);
|
|
135
51
|
}
|
|
136
|
-
|
|
52
|
+
}
|
|
53
|
+
draw(ctx, tree);
|
|
137
54
|
};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
55
|
+
const normalizeTree = (opts, tree) => {
|
|
56
|
+
if (tree == null) {
|
|
57
|
+
return tree;
|
|
58
|
+
}
|
|
59
|
+
if (isArray(tree)) {
|
|
60
|
+
const tag = tree[0];
|
|
61
|
+
if (typeof tag === FN) {
|
|
62
|
+
return normalizeTree(
|
|
63
|
+
opts,
|
|
64
|
+
tag.apply(null, [opts.ctx, ...tree.slice(1)])
|
|
65
|
+
);
|
|
146
66
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
return
|
|
67
|
+
if (typeof tag === STR) {
|
|
68
|
+
const attribs = tree[1];
|
|
69
|
+
if (attribs && attribs.__normalize === false) {
|
|
70
|
+
return tree;
|
|
71
|
+
}
|
|
72
|
+
const res = [tree[0], attribs];
|
|
73
|
+
for (let i = 2, n = tree.length; i < n; i++) {
|
|
74
|
+
const n2 = normalizeTree(opts, tree[i]);
|
|
75
|
+
n2 != null && res.push(n2);
|
|
76
|
+
}
|
|
77
|
+
return res;
|
|
151
78
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
79
|
+
} else if (typeof tree === FN) {
|
|
80
|
+
return normalizeTree(opts, tree(opts.ctx));
|
|
81
|
+
} else if (typeof tree.toHiccup === FN) {
|
|
82
|
+
return normalizeTree(opts, tree.toHiccup(opts.ctx));
|
|
83
|
+
} else if (typeof tree.deref === FN) {
|
|
84
|
+
return normalizeTree(opts, tree.deref());
|
|
85
|
+
} else if (isNotStringAndIterable(tree)) {
|
|
86
|
+
const res = [];
|
|
87
|
+
for (let t of tree) {
|
|
88
|
+
const n = normalizeTree(opts, t);
|
|
89
|
+
n != null && res.push(n);
|
|
155
90
|
}
|
|
91
|
+
return res;
|
|
92
|
+
}
|
|
93
|
+
return tree;
|
|
94
|
+
};
|
|
95
|
+
const diffTree = (opts, parent, prev, curr, child) => {
|
|
96
|
+
const attribs = curr[1];
|
|
97
|
+
if (attribs.__skip)
|
|
98
|
+
return;
|
|
99
|
+
if (attribs.__diff === false) {
|
|
100
|
+
releaseTree(prev);
|
|
101
|
+
return createTree(opts, parent, curr);
|
|
102
|
+
}
|
|
103
|
+
let impl = attribs.__impl;
|
|
104
|
+
if (impl && impl !== IMPL) {
|
|
105
|
+
return impl.diffTree(opts, parent, prev, curr, child);
|
|
106
|
+
}
|
|
107
|
+
const delta = diffArray(prev, curr, "only-distance", equiv);
|
|
108
|
+
if (delta.distance > 0) {
|
|
109
|
+
return createTree(opts, parent, curr);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const IMPL = {
|
|
113
|
+
createTree,
|
|
114
|
+
normalizeTree,
|
|
115
|
+
diffTree,
|
|
116
|
+
hydrateTree: NO_OP,
|
|
117
|
+
getElementById: NO_OP,
|
|
118
|
+
createElement: NO_OP,
|
|
119
|
+
createTextElement: NO_OP,
|
|
120
|
+
replaceChild: NO_OP,
|
|
121
|
+
getChild: NO_OP,
|
|
122
|
+
removeAttribs: NO_OP,
|
|
123
|
+
removeChild: NO_OP,
|
|
124
|
+
setAttrib: NO_OP,
|
|
125
|
+
setContent: NO_OP
|
|
156
126
|
};
|
|
157
|
-
export
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
createElement: NO_OP,
|
|
164
|
-
createTextElement: NO_OP,
|
|
165
|
-
replaceChild: NO_OP,
|
|
166
|
-
getChild: NO_OP,
|
|
167
|
-
removeAttribs: NO_OP,
|
|
168
|
-
removeChild: NO_OP,
|
|
169
|
-
setAttrib: NO_OP,
|
|
170
|
-
setContent: NO_OP,
|
|
127
|
+
export {
|
|
128
|
+
IMPL,
|
|
129
|
+
canvas,
|
|
130
|
+
createTree,
|
|
131
|
+
diffTree,
|
|
132
|
+
normalizeTree
|
|
171
133
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/hdom-canvas",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.98",
|
|
4
4
|
"description": "@thi.ng/hdom component wrapper for declarative canvas scenegraphs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -27,7 +27,9 @@
|
|
|
27
27
|
],
|
|
28
28
|
"license": "Apache-2.0",
|
|
29
29
|
"scripts": {
|
|
30
|
-
"build": "yarn
|
|
30
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
31
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
32
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
31
33
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc draw",
|
|
32
34
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
33
35
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -36,15 +38,16 @@
|
|
|
36
38
|
"test": "bun test"
|
|
37
39
|
},
|
|
38
40
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.9.
|
|
40
|
-
"@thi.ng/checks": "^3.4.
|
|
41
|
-
"@thi.ng/diff": "^5.1.
|
|
42
|
-
"@thi.ng/errors": "^2.4.
|
|
43
|
-
"@thi.ng/hdom": "^9.
|
|
44
|
-
"@thi.ng/hiccup-canvas": "^2.4.
|
|
41
|
+
"@thi.ng/api": "^8.9.12",
|
|
42
|
+
"@thi.ng/checks": "^3.4.12",
|
|
43
|
+
"@thi.ng/diff": "^5.1.45",
|
|
44
|
+
"@thi.ng/errors": "^2.4.6",
|
|
45
|
+
"@thi.ng/hdom": "^9.4.0",
|
|
46
|
+
"@thi.ng/hiccup-canvas": "^2.4.13"
|
|
45
47
|
},
|
|
46
48
|
"devDependencies": {
|
|
47
49
|
"@microsoft/api-extractor": "^7.38.3",
|
|
50
|
+
"esbuild": "^0.19.8",
|
|
48
51
|
"rimraf": "^5.0.5",
|
|
49
52
|
"tools": "^0.0.1",
|
|
50
53
|
"typedoc": "^0.25.4",
|
|
@@ -90,5 +93,5 @@
|
|
|
90
93
|
],
|
|
91
94
|
"year": 2018
|
|
92
95
|
},
|
|
93
|
-
"gitHead": "
|
|
96
|
+
"gitHead": "22e36fa838e5431d40165384918b395603bbd92f\n"
|
|
94
97
|
}
|