@thi.ng/hiccup-canvas 2.3.26 → 2.4.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-10-12T11:53:09Z
3
+ - **Last updated**: 2023-10-23T07:37:37Z
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.
@@ -9,6 +9,14 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/hiccup-canvas@2.4.0) (2023-10-23)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add/update packed shape support ([14ce2f8](https://github.com/thi-ng/umbrella/commit/14ce2f8))
17
+ - add packedPolyline() / packedPolygon() fns
18
+ - update draw() to add support for new shape types
19
+
12
20
  ### [2.3.24](https://github.com/thi-ng/umbrella/tree/@thi.ng/hiccup-canvas@2.3.24) (2023-10-11)
13
21
 
14
22
  #### 🩹 Bug fixes
package/README.md CHANGED
@@ -32,10 +32,10 @@ This project is part of the
32
32
  - [Path](#path)
33
33
  - [SVG paths with arc segments](#svg-paths-with-arc-segments)
34
34
  - [Points](#points)
35
- - [Packed points](#packed-points)
36
35
  - [Text](#text)
37
36
  - [Image](#image)
38
37
  - [Gradients](#gradients)
38
+ - [Using flat, packed vertex buffers](#using-flat-packed-vertex-buffers)
39
39
  - [Attributes](#attributes)
40
40
  - [Color attributes](#color-attributes)
41
41
  - [String](#string)
@@ -96,7 +96,7 @@ For Node.js REPL:
96
96
  const hiccupCanvas = await import("@thi.ng/hiccup-canvas");
97
97
  ```
98
98
 
99
- Package sizes (brotli'd, pre-treeshake): ESM: 2.43 KB
99
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.52 KB
100
100
 
101
101
  ## Dependencies
102
102
 
@@ -314,34 +314,6 @@ The following shape specific attributes are used:
314
314
  - `shape`: `circle` or `rect` (default)
315
315
  - `size`: point size (radius for circles, width for rects) - default: 1
316
316
 
317
- ### Packed points
318
-
319
- Similar to `points`, but uses a single packed buffer for all point
320
- coordinates.
321
-
322
- ```ts
323
- ["packedPoints", attribs, [x1,y1, x2,y2,...]]
324
- ```
325
-
326
- Optional start index, number of points, component & point stride lengths
327
- (number of indices between each vector component and each point
328
- respectively) can be given as attributes.
329
-
330
- Defaults:
331
-
332
- - start index: 0
333
- - number of points: (array_length - start) / estride
334
- - component stride: 1
335
- - element stride: 2
336
-
337
- ```ts
338
- ["packedPoints", { cstride: 1, estride: 4 },
339
- [x1, y1, 0, 0, x2, y2, 0, 0, ...]]
340
-
341
- ["packedPoints", { offset: 8, num: 3, cstride: 4, estride: 1 },
342
- [0, 0, 0, 0, 0, 0, 0, 0, x1, x2, x3, 0, y1, y2, y3, 0...]]
343
- ```
344
-
345
317
  ### Text
346
318
 
347
319
  ```ts
@@ -387,6 +359,42 @@ a gradient in a `fill` or `stroke` attribute, e.g. `{stroke: "$foo" }`
387
359
  ]
388
360
  ```
389
361
 
362
+ ### Using flat, packed vertex buffers
363
+
364
+ The `points`, `polyline` and `polygon` shape types also provide alternative
365
+ versions, each allowing the use of a single packed buffer (e.g. typed array) for
366
+ all point coordinates instead of individual arrays/views per vertex. This much
367
+ simplifies & speeds up WASM interop usecases, which can now skip creating vector
368
+ views of a vertexbuffer memory region.
369
+
370
+ ```ts
371
+ ["packedPoints", attribs, [x1,y1, x2,y2,...]]
372
+
373
+ ["packedPolyline", attribs, [x1,y1, x2,y2,...]]
374
+
375
+ ["packedPolygon", attribs, [x1,y1, x2,y2,...]]
376
+ ```
377
+
378
+ Optional start index, number of points, component & element stride lengths (i.e.
379
+ the number of indices between each vector x/y component and/or each point
380
+ respectively) can be given as attributes and thus these packaged shapes support
381
+ both AOS and SOA memory layouts/arrangements.
382
+
383
+ Defaults:
384
+
385
+ - start index: 0
386
+ - number of vertices: (array_length - start) / estride
387
+ - component stride: 1
388
+ - element stride: 2
389
+
390
+ ```ts
391
+ ["packedPoints", { cstride: 1, estride: 4 },
392
+ [x1, y1, 0, 0, x2, y2, 0, 0, ...]]
393
+
394
+ ["packedPoints", { offset: 8, num: 3, cstride: 4, estride: 1 },
395
+ [0, 0, 0, 0, 0, 0, 0, 0, x1, x2, x3, 0, y1, y2, y3, 0...]]
396
+ ```
397
+
390
398
  ## Attributes
391
399
 
392
400
  Some attributes use different names than their actual names in the
package/draw.js CHANGED
@@ -8,8 +8,8 @@ import { line, lines } from "./line.js";
8
8
  import { packedPoints } from "./packed-points.js";
9
9
  import { path } from "./path.js";
10
10
  import { points } from "./points.js";
11
- import { polygon } from "./polygon.js";
12
- import { polyline } from "./polyline.js";
11
+ import { packedPolygon, polygon } from "./polygon.js";
12
+ import { packedPolyline, polyline } from "./polyline.js";
13
13
  import { rect } from "./rect.js";
14
14
  import { text } from "./text.js";
15
15
  export const draw = (ctx, shape, pstate = { attribs: {}, edits: [] }) => {
@@ -62,9 +62,15 @@ export const draw = (ctx, shape, pstate = { attribs: {}, edits: [] }) => {
62
62
  case "polyline":
63
63
  polyline(ctx, attribs, shape[2]);
64
64
  break;
65
+ case "packedPolyline":
66
+ packedPolyline(ctx, attribs, shape[2]);
67
+ break;
65
68
  case "polygon":
66
69
  polygon(ctx, attribs, shape[2]);
67
70
  break;
71
+ case "packedPolygon":
72
+ packedPolygon(ctx, attribs, shape[2]);
73
+ break;
68
74
  case "path":
69
75
  path(ctx, attribs, shape[2]);
70
76
  break;
@@ -1,4 +1,4 @@
1
1
  import type { IObjectOf } from "@thi.ng/api";
2
2
  /** @internal */
3
- export declare const __endShape: (ctx: CanvasRenderingContext2D, attribs: IObjectOf<any>) => void;
3
+ export declare const __endShape: (ctx: CanvasRenderingContext2D, attribs: IObjectOf<any>, doFill?: boolean) => void;
4
4
  //# sourceMappingURL=end-shape.d.ts.map
@@ -1,7 +1,7 @@
1
1
  /** @internal */
2
- export const __endShape = (ctx, attribs) => {
2
+ export const __endShape = (ctx, attribs, doFill = true) => {
3
3
  let v;
4
- if ((v = attribs.fill) && v !== "none") {
4
+ if (doFill && (v = attribs.fill) && v !== "none") {
5
5
  ctx.fill();
6
6
  }
7
7
  if ((v = attribs.stroke) && v !== "none") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/hiccup-canvas",
3
- "version": "2.3.26",
3
+ "version": "2.4.1",
4
4
  "description": "Hiccup shape tree renderer for vanilla Canvas 2D contexts",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,19 +34,19 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.9.5",
38
- "@thi.ng/checks": "^3.4.5",
39
- "@thi.ng/color": "^5.5.23",
40
- "@thi.ng/math": "^5.6.2",
41
- "@thi.ng/pixel": "^4.2.26",
42
- "@thi.ng/vectors": "^7.7.19"
37
+ "@thi.ng/api": "^8.9.6",
38
+ "@thi.ng/checks": "^3.4.6",
39
+ "@thi.ng/color": "^5.5.25",
40
+ "@thi.ng/math": "^5.6.3",
41
+ "@thi.ng/pixel": "^4.2.28",
42
+ "@thi.ng/vectors": "^7.7.21"
43
43
  },
44
44
  "devDependencies": {
45
- "@microsoft/api-extractor": "^7.36.4",
46
- "@thi.ng/testament": "^0.3.23",
47
- "rimraf": "^5.0.1",
45
+ "@microsoft/api-extractor": "^7.38.0",
46
+ "@thi.ng/testament": "^0.3.24",
47
+ "rimraf": "^5.0.5",
48
48
  "tools": "^0.0.1",
49
- "typedoc": "^0.25.0",
49
+ "typedoc": "^0.25.2",
50
50
  "typescript": "^5.2.2"
51
51
  },
52
52
  "keywords": [
@@ -130,5 +130,5 @@
130
130
  ],
131
131
  "year": 2018
132
132
  },
133
- "gitHead": "46e445c09f2909d1aeaa9fdc8d8b3aa61c114db2\n"
133
+ "gitHead": "dc7eeaba72f3c3977f2d6cc201e595ed6d68e2ad\n"
134
134
  }
package/polygon.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { IObjectOf } from "@thi.ng/api";
2
2
  import type { ReadonlyVec } from "@thi.ng/vectors";
3
3
  export declare const polygon: (ctx: CanvasRenderingContext2D, attribs: IObjectOf<any>, pts: ReadonlyVec[]) => void;
4
+ export declare const packedPolygon: (ctx: CanvasRenderingContext2D, attribs: IObjectOf<any>, pts: ArrayLike<number>) => void;
4
5
  /**
5
6
  * Shared internal helper for polygon & polyline fns.
6
7
  *
@@ -10,4 +11,14 @@ export declare const polygon: (ctx: CanvasRenderingContext2D, attribs: IObjectOf
10
11
  * @internal
11
12
  */
12
13
  export declare const __drawPoly: (ctx: CanvasRenderingContext2D, pts: ReadonlyVec[]) => void;
14
+ /**
15
+ * Shared internal helper for packed polygon & polyline fns.
16
+ *
17
+ * @param ctx - canvas context
18
+ * @param ctx - shape attribs (incl. packing config)
19
+ * @param pts - flat vertex buffer
20
+ *
21
+ * @internal
22
+ */
23
+ export declare const __drawPackedPoly: (ctx: CanvasRenderingContext2D, opts: IObjectOf<any>, pts: ArrayLike<number>) => void;
13
24
  //# sourceMappingURL=polygon.d.ts.map
package/polygon.js CHANGED
@@ -6,6 +6,13 @@ export const polygon = (ctx, attribs, pts) => {
6
6
  ctx.closePath();
7
7
  __endShape(ctx, attribs);
8
8
  };
9
+ export const packedPolygon = (ctx, attribs, pts) => {
10
+ if (pts.length < 2)
11
+ return;
12
+ __drawPackedPoly(ctx, attribs, pts);
13
+ ctx.closePath();
14
+ __endShape(ctx, attribs);
15
+ };
9
16
  /**
10
17
  * Shared internal helper for polygon & polyline fns.
11
18
  *
@@ -23,3 +30,28 @@ export const __drawPoly = (ctx, pts) => {
23
30
  ctx.lineTo(p[0], p[1]);
24
31
  }
25
32
  };
33
+ /**
34
+ * Shared internal helper for packed polygon & polyline fns.
35
+ *
36
+ * @param ctx - canvas context
37
+ * @param ctx - shape attribs (incl. packing config)
38
+ * @param pts - flat vertex buffer
39
+ *
40
+ * @internal
41
+ */
42
+ export const __drawPackedPoly = (ctx, opts, pts) => {
43
+ const { start, cstride, estride } = {
44
+ start: 0,
45
+ cstride: 1,
46
+ estride: 2,
47
+ ...opts,
48
+ };
49
+ let num = opts && opts.num != null
50
+ ? opts.num
51
+ : ((pts.length - start) / estride) | 0;
52
+ ctx.beginPath();
53
+ ctx.moveTo(pts[start], pts[start + cstride]);
54
+ for (let i = start + estride; num-- > 1; i += estride) {
55
+ ctx.lineTo(pts[i], pts[i + cstride]);
56
+ }
57
+ };
package/polyline.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { IObjectOf } from "@thi.ng/api";
2
2
  import type { ReadonlyVec } from "@thi.ng/vectors";
3
3
  export declare const polyline: (ctx: CanvasRenderingContext2D, attribs: IObjectOf<any>, pts: ReadonlyVec[]) => void;
4
+ export declare const packedPolyline: (ctx: CanvasRenderingContext2D, attribs: IObjectOf<any>, pts: ArrayLike<number>) => void;
4
5
  //# sourceMappingURL=polyline.d.ts.map
package/polyline.js CHANGED
@@ -1,7 +1,14 @@
1
- import { __drawPoly } from "./polygon.js";
1
+ import { __endShape } from "./internal/end-shape.js";
2
+ import { __drawPackedPoly, __drawPoly } from "./polygon.js";
2
3
  export const polyline = (ctx, attribs, pts) => {
3
4
  if (pts.length < 2 || attribs.stroke == "none")
4
5
  return;
5
6
  __drawPoly(ctx, pts);
6
- ctx.stroke();
7
+ __endShape(ctx, attribs, false);
8
+ };
9
+ export const packedPolyline = (ctx, attribs, pts) => {
10
+ if (pts.length < 2)
11
+ return;
12
+ __drawPackedPoly(ctx, attribs, pts);
13
+ __endShape(ctx, attribs, false);
7
14
  };