@thi.ng/hdom-canvas 4.1.95 → 4.1.97

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/index.js +115 -153
  3. package/package.json +12 -10
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-03T12:13:31Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
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
- * Special HTML5 canvas component which injects a branch-local hdom
12
- * implementation for virtual SVG-like shape components / elements. These
13
- * elements are then translated into canvas draw commands during the hdom update
14
- * process.
15
- *
16
- * The canvas component automatically adjusts its size for HDPI displays by
17
- * adding CSS `width` & `height` properties and pre-scaling the drawing context
18
- * accordingly before shapes are processed.
19
- *
20
- * Shape components are expressed in standard hiccup syntax, however with the
21
- * following restrictions:
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
- draw(ctx, tree);
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
- /** @internal */
97
- export const normalizeTree = (opts, tree) => {
98
- if (tree == null) {
99
- return tree;
100
- }
101
- if (isArray(tree)) {
102
- const tag = tree[0];
103
- if (typeof tag === FN) {
104
- return normalizeTree(opts, tag.apply(null, [opts.ctx, ...tree.slice(1)]));
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
- return tree;
52
+ }
53
+ draw(ctx, tree);
137
54
  };
138
- /** @internal */
139
- export const diffTree = (opts, parent, prev, curr, child) => {
140
- const attribs = curr[1];
141
- if (attribs.__skip)
142
- return;
143
- if (attribs.__diff === false) {
144
- releaseTree(prev);
145
- return createTree(opts, parent, curr);
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
- // delegate to branch-local implementation
148
- let impl = attribs.__impl;
149
- if (impl && impl !== IMPL) {
150
- return impl.diffTree(opts, parent, prev, curr, child);
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
- const delta = diffArray(prev, curr, "only-distance", equiv);
153
- if (delta.distance > 0) {
154
- return createTree(opts, parent, curr);
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 const IMPL = {
158
- createTree,
159
- normalizeTree,
160
- diffTree,
161
- hydrateTree: NO_OP,
162
- getElementById: NO_OP,
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.95",
3
+ "version": "4.1.97",
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 clean && tsc --declaration",
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,16 +38,16 @@
36
38
  "test": "bun test"
37
39
  },
38
40
  "dependencies": {
39
- "@thi.ng/api": "^8.9.10",
40
- "@thi.ng/checks": "^3.4.10",
41
- "@thi.ng/diff": "^5.1.43",
42
- "@thi.ng/errors": "^2.4.4",
43
- "@thi.ng/hdom": "^9.3.30",
44
- "@thi.ng/hiccup-canvas": "^2.4.10"
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.12"
45
47
  },
46
48
  "devDependencies": {
47
49
  "@microsoft/api-extractor": "^7.38.3",
48
- "@thi.ng/testament": "^0.4.3",
50
+ "esbuild": "^0.19.8",
49
51
  "rimraf": "^5.0.5",
50
52
  "tools": "^0.0.1",
51
53
  "typedoc": "^0.25.4",
@@ -91,5 +93,5 @@
91
93
  ],
92
94
  "year": 2018
93
95
  },
94
- "gitHead": "04d1de79f256d7a53c6b5fd157b37f49bc88e11d\n"
96
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
95
97
  }