@widgetic/canvas 0.5.24 → 0.5.26

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/LICENSE ADDED
@@ -0,0 +1,122 @@
1
+ Widgetic Integration License
2
+ Version 1.0
3
+
4
+ Copyright (c) Widgetic. All rights reserved.
5
+
6
+ This license covers the software package that it came with, in source and
7
+ compiled form, together with its documentation (the "Software"). By
8
+ installing, copying or using the Software you accept this license. If you do
9
+ not accept it, do not install or use the Software.
10
+
11
+ In this license, "Widgetic" is the company that publishes the Software, "you"
12
+ is the person or organization that uses it, and "Your Application" is an
13
+ application, website, product or service that you build or operate, for
14
+ yourself or for a client. "Widgetic Services" are the services that Widgetic
15
+ operates: the Widgetic API, widgets, compositions, embeds, editors, file
16
+ browser, chat and AI creator, and the other services that can be reached from
17
+ widgetic.com or its API hosts.
18
+
19
+ 1. What you may do
20
+
21
+ Widgetic grants you a worldwide, non-exclusive, non-transferable and
22
+ non-sublicensable license to use the Software, unmodified, for one purpose: to
23
+ integrate the Widgetic Services into Your Application. Within that purpose you
24
+ may:
25
+
26
+ (a) install the Software and run it in Your Application, in development,
27
+ testing and production;
28
+
29
+ (b) use it to call the Widgetic API, to show Widgetic widgets and
30
+ compositions, and to open Widgetic's editors, file browser, chat and AI
31
+ creator inside Your Application;
32
+
33
+ (c) bundle, minify and tree-shake the Software with the build tools of Your
34
+ Application, deliver the result to the people who use Your Application as a
35
+ part of it, and let them run it there;
36
+
37
+ (d) keep the copies of the Software that (a), (b) and (c) need, including
38
+ copies in your version control, your build systems and your backups.
39
+
40
+ 2. What you may not do
41
+
42
+ You may not, and may not allow anyone else to:
43
+
44
+ (a) use the Software for any purpose other than the one in section 1;
45
+
46
+ (b) use the Software to build, run or improve a service that offers what the
47
+ Widgetic Services offer, or to connect Your Application to anything other than
48
+ the Widgetic Services (a proxy of yours that forwards calls to the Widgetic
49
+ Services is fine);
50
+
51
+ (c) copy, modify, translate, adapt or make derivative works of the Software,
52
+ except as section 1(c) allows;
53
+
54
+ (d) publish, sell, rent, lease, sublicense or distribute the Software on its
55
+ own, or inside a library, SDK, template or product that others could use for
56
+ anything other than integrating the Widgetic Services, including by
57
+ publishing it, or a copy of it, to a package registry or a public CDN;
58
+
59
+ (e) reverse engineer, decompile or disassemble the Software, or try to get at
60
+ its source code, except as far as the law lets you despite this restriction;
61
+
62
+ (f) remove or change a copyright, license or attribution notice in the
63
+ Software;
64
+
65
+ (g) bypass, disable or interfere with the authentication, rate limits, quotas,
66
+ credits, plan limits or other protections of the Widgetic Services, or help
67
+ anyone else to;
68
+
69
+ (h) use the name, logo or trademarks of Widgetic, except to say truthfully
70
+ that Your Application integrates Widgetic.
71
+
72
+ 3. The Widgetic Services are separate
73
+
74
+ This license is for the Software only. It does not give you the right to use
75
+ the Widgetic Services. That right comes from your account or agreement with
76
+ Widgetic and from Widgetic's Terms of Service (https://widgetic.com/terms),
77
+ which this license does not change.
78
+
79
+ 4. Ownership
80
+
81
+ The Software is licensed to you, not sold. Widgetic and its licensors keep all
82
+ rights in it. Any right that this license does not give you in plain words is
83
+ reserved to Widgetic.
84
+
85
+ 5. Third-party software
86
+
87
+ The Software may include or depend on software from others. That software is
88
+ under its own license, which applies to it instead of this one, and nothing in
89
+ this license limits your rights under it.
90
+
91
+ 6. How long it lasts
92
+
93
+ This license lasts while you follow it and while you are entitled to use the
94
+ Widgetic Services. It ends on its own if you break it or that entitlement
95
+ ends. When it ends, you stop using the Software and delete your copies of it,
96
+ except copies that the law requires you to keep. Sections 4, 8 and 9 still
97
+ apply after it has ended.
98
+
99
+ 7. Versions
100
+
101
+ This license applies to the version of the Software that it came with and to
102
+ later versions that carry it. A version that Widgetic published under another
103
+ license stays under that license, for the copies of it that you already have.
104
+
105
+ 8. No warranty
106
+
107
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
108
+ IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
109
+ PURPOSE AND NON-INFRINGEMENT.
110
+
111
+ 9. Limit of liability
112
+
113
+ TO THE EXTENT THE LAW ALLOWS, WIDGETIC IS NOT LIABLE FOR ANY INDIRECT,
114
+ INCIDENTAL, SPECIAL, CONSEQUENTIAL OR PUNITIVE DAMAGES, OR FOR LOST PROFITS,
115
+ REVENUE, DATA OR GOODWILL, THAT ARISE FROM THE SOFTWARE OR FROM THIS LICENSE,
116
+ EVEN IF IT HAS BEEN TOLD THAT THEY ARE POSSIBLE. NOTHING IN THIS LICENSE
117
+ LIMITS A LIABILITY THAT THE LAW DOES NOT ALLOW TO BE LIMITED.
118
+
119
+ 10. Questions
120
+
121
+ If you need to do something that this license does not allow, ask first:
122
+ support@widgetic.com.
package/README.md CHANGED
@@ -159,3 +159,7 @@ reads the language of that canvas, so two canvases on one page can speak two lan
159
159
  ## Publishing
160
160
 
161
161
  GitLab CI on `main` publishes this package. Bump `version` in `package.json` and push `main`. The job is an npm Trusted Publisher (`NPM_ID_TOKEN`) and skips a version that is already on npm. A laptop publish is the fallback when CI minutes are gone. The token stays in `~/.npmrc` and is never committed.
162
+
163
+ ## License
164
+
165
+ This package is licensed under the Widgetic Integration License (see the `LICENSE` file in the package): you may use it to integrate Widgetic into your own application, and for nothing else. It is not open source. For any other use, write to support@widgetic.com.
@@ -38,6 +38,7 @@
38
38
  measureStroke,
39
39
  planSnapPlacement,
40
40
  rankSnapShapes,
41
+ renderStrokeSnapImage,
41
42
  type CanvasSnapShape,
42
43
  type PathCommand
43
44
  } from './shapes/strokeShapeSnap';
@@ -7271,7 +7272,8 @@ function checkAndAddNewShapeToFrame(shape: any) {
7271
7272
  const commands = (path?.path ?? null) as PathCommand[] | null;
7272
7273
  if (!Array.isArray(commands) || commands.length < 2) return;
7273
7274
 
7274
- const measurements = measureStroke(flattenPathCommands(commands));
7275
+ const flattenedPoints = flattenPathCommands(commands);
7276
+ const measurements = measureStroke(flattenedPoints);
7275
7277
  if (!measurements) return;
7276
7278
 
7277
7279
  // Only shapes the measurements cannot rule out are ever considered, and
@@ -7295,10 +7297,12 @@ function checkAndAddNewShapeToFrame(shape: any) {
7295
7297
  });
7296
7298
  return;
7297
7299
  }
7300
+ const strokeImage = renderStrokeSnapImage(flattenedPoints);
7298
7301
  const answer = await onRecognizeStroke({
7299
7302
  measurements,
7300
7303
  candidates,
7301
- best: candidates[0] ?? null
7304
+ best: candidates[0] ?? null,
7305
+ ...(strokeImage ? { image: strokeImage } : {})
7302
7306
  });
7303
7307
  const shape = answer?.shape ?? null;
7304
7308
  if (!shape || !candidates.some((candidate) => candidate.shape === shape)) {
@@ -11,7 +11,7 @@ export { default as PanZoomPanel } from './PanZoomPanel.svelte';
11
11
  export { default as PropsPanel } from './props-panel/PropsPanel.svelte';
12
12
  export type { WidgetConversionPayload } from './types';
13
13
  export type { StrokeRecognizer, StrokeRecognitionRequest, StrokeRecognitionResult } from './types';
14
- export { CANVAS_SHAPE_SNAP_ENABLED } from './shapes/strokeShapeSnap';
14
+ export { CANVAS_SHAPE_SNAP_ENABLED, STROKE_SNAP_IMAGE_SIDE, projectStrokeToImagePoints, renderStrokeSnapImage } from './shapes/strokeShapeSnap';
15
15
  export { canvasLog, canvasWarn, canvasDebug, setCanvasLogsEnabled, isCanvasLogsEnabled } from './canvasLogger';
16
16
  export { setShapeImageStore, getShapeImageStore } from './shapes/shapeImageStore';
17
17
  export type { ShapeImageStore } from './shapes/shapeImageStore';
@@ -9,6 +9,6 @@ export { default as CanvasToolbar } from './CanvasToolbar.svelte';
9
9
  export { default as ContextMenu } from './ContextMenu.svelte';
10
10
  export { default as PanZoomPanel } from './PanZoomPanel.svelte';
11
11
  export { default as PropsPanel } from './props-panel/PropsPanel.svelte';
12
- export { CANVAS_SHAPE_SNAP_ENABLED } from './shapes/strokeShapeSnap';
12
+ export { CANVAS_SHAPE_SNAP_ENABLED, STROKE_SNAP_IMAGE_SIDE, projectStrokeToImagePoints, renderStrokeSnapImage } from './shapes/strokeShapeSnap';
13
13
  export { canvasLog, canvasWarn, canvasDebug, setCanvasLogsEnabled, isCanvasLogsEnabled } from './canvasLogger';
14
14
  export { setShapeImageStore, getShapeImageStore } from './shapes/shapeImageStore';
@@ -7,9 +7,7 @@
7
7
  * edges that bow. This module measures that polyline and decides whether it is a
8
8
  * clean enough instance of a shape the canvas can actually build.
9
9
  *
10
- * **Geometry belongs in code.** JEV cannot see a stroke: it judges text, and the
11
- * only thing that could be said to it in text is a *measurement* of the stroke,
12
- * which code has to compute anyway. So the pipeline is:
10
+ * **Geometry gates first; JEV 1.14 vision breaks ties.** The pipeline is:
13
11
  *
14
12
  * 1. measure the stroke here, deterministically (closed? how many corners? how
15
13
  * circular? how far off axis? aspect ratio?) — 0ms, no network;
@@ -19,9 +17,11 @@
19
17
  * 3. rank the survivors by how well they match, and a decisive winner is
20
18
  * applied immediately with no model call;
21
19
  * 4. only when the survivors are ambiguous (a near-square, an oval that is
22
- * almost a circle) does the caller escalate to JEV, which is exactly the
23
- * closed-set choice with a confidence gate it is good at — and which can
24
- * only ever replace a wrong *snap* with the stroke left as drawn.
20
+ * almost a circle) does the caller escalate to JEV (`jev-1.14.0`) with both
21
+ * a rendered 320×320 PNG of the stroke (`renderStrokeSnapImage`) and the
22
+ * geometric measurements, which is the closed-set choice with a confidence
23
+ * gate it is good at — and which can only ever replace a wrong *snap* with
24
+ * the stroke left as drawn.
25
25
  *
26
26
  * Everything here is pure: it takes command data and points, never a Fabric
27
27
  * object, so it can be tested with `npm test` and reused by any caller that can
@@ -163,3 +163,22 @@ export interface SnapPlacement {
163
163
  * matter never reach here (see `SHAPE_GATES`).
164
164
  */
165
165
  export declare function planSnapPlacement(shape: CanvasSnapShape, bounds: SnapBounds): SnapPlacement;
166
+ /**
167
+ * Side length (px) of the square PNG rendered for JEV 1.14 vision.
168
+ * TypeSafe's System One API enforces 280–1568 px on each axis (78,400–2,458,624 px²);
169
+ * 320×320 sits safely inside the floor while keeping input tokens low (~335 tokens).
170
+ */
171
+ export declare const STROKE_SNAP_IMAGE_SIDE = 320;
172
+ /** Padding (px) around the stroke inside the rendered square canvas. */
173
+ export declare const STROKE_SNAP_IMAGE_PADDING = 28;
174
+ /**
175
+ * Project a polyline into a `side × side` square, preserving aspect ratio and
176
+ * centering the shorter axis. Pure, so the geometry is testable without a DOM.
177
+ */
178
+ export declare function projectStrokeToImagePoints(points: readonly StrokePoint[], side?: number, padding?: number): StrokePoint[];
179
+ /**
180
+ * Render the user's freehand stroke onto an offscreen 320×320 white canvas as a
181
+ * `data:image/png;base64,...` URL for JEV 1.14 vision evaluation. Returns
182
+ * `undefined` in non-DOM environments or if a 2D context is unavailable.
183
+ */
184
+ export declare function renderStrokeSnapImage(points: readonly StrokePoint[], side?: number): string | undefined;
@@ -7,9 +7,7 @@
7
7
  * edges that bow. This module measures that polyline and decides whether it is a
8
8
  * clean enough instance of a shape the canvas can actually build.
9
9
  *
10
- * **Geometry belongs in code.** JEV cannot see a stroke: it judges text, and the
11
- * only thing that could be said to it in text is a *measurement* of the stroke,
12
- * which code has to compute anyway. So the pipeline is:
10
+ * **Geometry gates first; JEV 1.14 vision breaks ties.** The pipeline is:
13
11
  *
14
12
  * 1. measure the stroke here, deterministically (closed? how many corners? how
15
13
  * circular? how far off axis? aspect ratio?) — 0ms, no network;
@@ -19,9 +17,11 @@
19
17
  * 3. rank the survivors by how well they match, and a decisive winner is
20
18
  * applied immediately with no model call;
21
19
  * 4. only when the survivors are ambiguous (a near-square, an oval that is
22
- * almost a circle) does the caller escalate to JEV, which is exactly the
23
- * closed-set choice with a confidence gate it is good at — and which can
24
- * only ever replace a wrong *snap* with the stroke left as drawn.
20
+ * almost a circle) does the caller escalate to JEV (`jev-1.14.0`) with both
21
+ * a rendered 320×320 PNG of the stroke (`renderStrokeSnapImage`) and the
22
+ * geometric measurements, which is the closed-set choice with a confidence
23
+ * gate it is good at — and which can only ever replace a wrong *snap* with
24
+ * the stroke left as drawn.
25
25
  *
26
26
  * Everything here is pure: it takes command data and points, never a Fabric
27
27
  * object, so it can be tested with `npm test` and reused by any caller that can
@@ -699,3 +699,74 @@ function clampUnit(value) {
699
699
  function round(value) {
700
700
  return Math.round(value * 1000) / 1000;
701
701
  }
702
+ // ─── Vision rendering for JEV 1.14 ──────────────────────────────────────────
703
+ /**
704
+ * Side length (px) of the square PNG rendered for JEV 1.14 vision.
705
+ * TypeSafe's System One API enforces 280–1568 px on each axis (78,400–2,458,624 px²);
706
+ * 320×320 sits safely inside the floor while keeping input tokens low (~335 tokens).
707
+ */
708
+ export const STROKE_SNAP_IMAGE_SIDE = 320;
709
+ /** Padding (px) around the stroke inside the rendered square canvas. */
710
+ export const STROKE_SNAP_IMAGE_PADDING = 28;
711
+ /**
712
+ * Project a polyline into a `side × side` square, preserving aspect ratio and
713
+ * centering the shorter axis. Pure, so the geometry is testable without a DOM.
714
+ */
715
+ export function projectStrokeToImagePoints(points, side = STROKE_SNAP_IMAGE_SIDE, padding = STROKE_SNAP_IMAGE_PADDING) {
716
+ if (points.length < 2)
717
+ return [];
718
+ const bounds = boundingBox(points);
719
+ if (!Number.isFinite(bounds.width) || !Number.isFinite(bounds.height))
720
+ return [];
721
+ if (bounds.width <= 0 && bounds.height <= 0)
722
+ return [];
723
+ const drawable = Math.max(1, side - padding * 2);
724
+ const span = Math.max(bounds.width, bounds.height, 1);
725
+ const scale = drawable / span;
726
+ const offsetX = (side - bounds.width * scale) / 2;
727
+ const offsetY = (side - bounds.height * scale) / 2;
728
+ return points.map((point) => ({
729
+ x: round(offsetX + (point.x - bounds.left) * scale),
730
+ y: round(offsetY + (point.y - bounds.top) * scale)
731
+ }));
732
+ }
733
+ /**
734
+ * Render the user's freehand stroke onto an offscreen 320×320 white canvas as a
735
+ * `data:image/png;base64,...` URL for JEV 1.14 vision evaluation. Returns
736
+ * `undefined` in non-DOM environments or if a 2D context is unavailable.
737
+ */
738
+ export function renderStrokeSnapImage(points, side = STROKE_SNAP_IMAGE_SIDE) {
739
+ const projected = projectStrokeToImagePoints(points, side);
740
+ if (projected.length < 2)
741
+ return undefined;
742
+ if (typeof document === 'undefined' || typeof document.createElement !== 'function') {
743
+ return undefined;
744
+ }
745
+ try {
746
+ const offscreen = document.createElement('canvas');
747
+ offscreen.width = side;
748
+ offscreen.height = side;
749
+ const ctx = offscreen.getContext('2d');
750
+ if (!ctx)
751
+ return undefined;
752
+ ctx.fillStyle = '#ffffff';
753
+ ctx.fillRect(0, 0, side, side);
754
+ ctx.strokeStyle = '#111827';
755
+ ctx.lineWidth = 4;
756
+ ctx.lineCap = 'round';
757
+ ctx.lineJoin = 'round';
758
+ ctx.beginPath();
759
+ ctx.moveTo(projected[0].x, projected[0].y);
760
+ for (let i = 1; i < projected.length; i++) {
761
+ ctx.lineTo(projected[i].x, projected[i].y);
762
+ }
763
+ ctx.stroke();
764
+ const dataUrl = offscreen.toDataURL('image/png');
765
+ return typeof dataUrl === 'string' && dataUrl.startsWith('data:image/png;base64,')
766
+ ? dataUrl
767
+ : undefined;
768
+ }
769
+ catch {
770
+ return undefined;
771
+ }
772
+ }
@@ -29,10 +29,12 @@ export interface WidgetConversionPayload {
29
29
  * A hand-drawn stroke the canvas could not classify on its own, handed to the
30
30
  * host to classify with a model.
31
31
  *
32
- * The canvas has already done the geometry — a model cannot see a stroke, only
33
- * what can be said about it — so this carries the stroke's *measurements* and the
34
- * shapes those measurements still allow. The host owns the call (in Widgetic, one
35
- * JEV decision), and the canvas applies only a shape from `candidates`.
32
+ /**
33
+ * The canvas has already gated out impossible shapes in geometry; for the
34
+ * ambiguous residue, it sends the stroke's measurements, the surviving
35
+ * candidates, and (in browsers) a 320×320 PNG rendering of the stroke for
36
+ * JEV 1.14 vision. The host owns the call, and the canvas applies only a shape
37
+ * from `candidates`.
36
38
  */
37
39
  export interface StrokeRecognitionRequest {
38
40
  /** The stroke's geometry, as ratios and bounds — never the raw path. */
@@ -45,6 +47,8 @@ export interface StrokeRecognitionRequest {
45
47
  best: (SnapRanking & {
46
48
  label: string;
47
49
  }) | null;
50
+ /** Optional `data:image/png;base64,...` rendering of the freehand stroke for JEV 1.14 vision. */
51
+ image?: string;
48
52
  }
49
53
  /**
50
54
  * The host's answer. `null` — or a shape the geometry ruled out — means the
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@widgetic/canvas",
3
- "version": "0.5.24",
3
+ "version": "0.5.26",
4
+ "license": "SEE LICENSE IN LICENSE",
4
5
  "type": "module",
5
6
  "svelte": "./dist/index.js",
6
7
  "module": "./dist/index.js",
@@ -97,8 +98,11 @@
97
98
  "form-data": "^4.0.6",
98
99
  "tar": "^7.5.22"
99
100
  },
101
+ "engines": {
102
+ "node": ">=26.12.1"
103
+ },
100
104
  "volta": {
101
- "node": "22.14.0",
105
+ "node": "26.12.1",
102
106
  "npm": "11.1.0"
103
107
  },
104
108
  "publishConfig": {