@widgetic/canvas 0.5.14 → 0.5.15

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.
@@ -2,6 +2,7 @@
2
2
  * Canvas component shared types.
3
3
  * Importable by both the canvas project and consuming apps (e.g. widget-creator).
4
4
  */
5
+ import type { CanvasSnapShape, SnapRanking, StrokeMeasurements } from './shapes/strokeShapeSnap';
5
6
  /** Payload emitted when a frame is converted to a widget via onConvertToWidget callback. */
6
7
  export interface WidgetConversionPayload {
7
8
  frameName: string;
@@ -24,3 +25,37 @@ export interface WidgetConversionPayload {
24
25
  hasText: boolean;
25
26
  };
26
27
  }
28
+ /**
29
+ * A hand-drawn stroke the canvas could not classify on its own, handed to the
30
+ * host to classify with a model.
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`.
36
+ */
37
+ export interface StrokeRecognitionRequest {
38
+ /** The stroke's geometry, as ratios and bounds — never the raw path. */
39
+ measurements: StrokeMeasurements;
40
+ /** The shapes the geometry could not rule out, best match first. */
41
+ candidates: Array<SnapRanking & {
42
+ label: string;
43
+ }>;
44
+ /** The deterministic engine's own best guess, when its score is not decisive. */
45
+ best: (SnapRanking & {
46
+ label: string;
47
+ }) | null;
48
+ }
49
+ /**
50
+ * The host's answer. `null` — or a shape the geometry ruled out — means the
51
+ * stroke stays exactly as drawn, which is always a valid answer.
52
+ */
53
+ export interface StrokeRecognitionResult {
54
+ /** One of the request's candidate shapes, or null to decline. */
55
+ shape: CanvasSnapShape | string | null;
56
+ confidence?: number | null;
57
+ /** Why, for logging and telemetry. */
58
+ reason?: string;
59
+ }
60
+ /** The recognizer a host supplies to enable stroke snapping. */
61
+ export type StrokeRecognizer = (request: StrokeRecognitionRequest) => Promise<StrokeRecognitionResult | null>;
@@ -3,12 +3,17 @@
3
3
  * Fabric matrices include rotation, skew, flips and owning groups. CSS starts
4
4
  * at the top-left, while Fabric transforms around the object's center.
5
5
  */
6
- export function getWidgetPreviewGeometry(object: any, canvas: any, canvasElement: any): {
6
+ /**
7
+ * @param {import('fabric').FabricObject} object
8
+ * @param {import('fabric').Canvas} canvas
9
+ * @param {HTMLCanvasElement} canvasElement
10
+ */
11
+ export function getWidgetPreviewGeometry(object: import("fabric").FabricObject, canvas: import("fabric").Canvas, canvasElement: HTMLCanvasElement): {
7
12
  left: number;
8
13
  top: number;
9
14
  width: number;
10
15
  height: number;
11
- localWidth: any;
12
- localHeight: any;
13
- matrix: any[];
16
+ localWidth: number;
17
+ localHeight: number;
18
+ matrix: number[];
14
19
  } | null;
@@ -1,33 +1,54 @@
1
- import { util } from 'fabric';
2
-
3
1
  /**
4
2
  * Local widget pixels -> CSS viewport pixels (no selection padding/stroke).
5
3
  * Fabric matrices include rotation, skew, flips and owning groups. CSS starts
6
4
  * at the top-left, while Fabric transforms around the object's center.
7
5
  */
6
+ /**
7
+ * @param {import('fabric').FabricObject} object
8
+ * @param {import('fabric').Canvas} canvas
9
+ * @param {HTMLCanvasElement} canvasElement
10
+ */
8
11
  export function getWidgetPreviewGeometry(object, canvas, canvasElement) {
9
- if (!canvas || !canvasElement) return null;
12
+ if (!canvas || !canvasElement || !object) return null;
10
13
  const bounds = canvasElement.getBoundingClientRect();
14
+ const canvasWidth = canvas.getWidth() || bounds.width || 1;
15
+ const canvasHeight = canvas.getHeight() || bounds.height || 1;
16
+ const cssScaleX = bounds.width / canvasWidth;
17
+ const cssScaleY = bounds.height / canvasHeight;
11
18
  const viewport = canvas.viewportTransform || [1, 0, 0, 1, 0, 0];
12
- const [a, b, c, d, centerX, centerY] = util.multiplyTransformMatrices(viewport, object.calcTransformMatrix());
13
- const cssScaleX = bounds.width / canvas.getWidth();
14
- const cssScaleY = bounds.height / canvas.getHeight();
15
- const localWidth = object.width;
16
- const localHeight = object.height;
19
+ // aCoords are the painted corners in scene space. The old center-matrix
20
+ // path drifted from those corners once a selection drag updated the
21
+ // bitmap without the same translation the iframe was using.
22
+ object.setCoords?.();
23
+ const scene = object.aCoords;
24
+ if (!scene?.tl || !scene?.tr || !scene?.bl || !scene?.br) return null;
25
+ const project = (point) => ({
26
+ x: bounds.left + (viewport[0] * point.x + viewport[2] * point.y + viewport[4]) * cssScaleX,
27
+ y: bounds.top + (viewport[1] * point.x + viewport[3] * point.y + viewport[5]) * cssScaleY,
28
+ });
29
+ const tl = project(scene.tl);
30
+ const tr = project(scene.tr);
31
+ const bl = project(scene.bl);
32
+ const br = project(scene.br);
33
+ const xs = [tl.x, tr.x, bl.x, br.x];
34
+ const ys = [tl.y, tr.y, bl.y, br.y];
35
+ const left = Math.min(...xs);
36
+ const top = Math.min(...ys);
37
+ const localWidth = object.width || 1;
38
+ const localHeight = object.height || 1;
17
39
  const matrix = [
18
- a * cssScaleX, b * cssScaleY, c * cssScaleX, d * cssScaleY,
19
- bounds.left + (centerX - a * localWidth / 2 - c * localHeight / 2) * cssScaleX,
20
- bounds.top + (centerY - b * localWidth / 2 - d * localHeight / 2) * cssScaleY,
40
+ (tr.x - tl.x) / localWidth,
41
+ (tr.y - tl.y) / localWidth,
42
+ (bl.x - tl.x) / localHeight,
43
+ (bl.y - tl.y) / localHeight,
44
+ tl.x,
45
+ tl.y,
21
46
  ];
22
- const corners = [[0, 0], [localWidth, 0], [localWidth, localHeight], [0, localHeight]]
23
- .map(([x, y]) => ({ x: matrix[0] * x + matrix[2] * y + matrix[4], y: matrix[1] * x + matrix[3] * y + matrix[5] }));
24
- const left = Math.min(...corners.map(point => point.x));
25
- const top = Math.min(...corners.map(point => point.y));
26
- // Chrome attaches to these body bounds; selection controls do not add clearance.
47
+ if (!matrix.every(Number.isFinite)) return null;
27
48
  return {
28
49
  left, top,
29
- width: Math.max(...corners.map(point => point.x)) - left,
30
- height: Math.max(...corners.map(point => point.y)) - top,
50
+ width: Math.max(...xs) - left,
51
+ height: Math.max(...ys) - top,
31
52
  localWidth, localHeight, matrix,
32
53
  };
33
54
  }
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  * - Dynamic: const { Canvas } = await import('@widgetic/canvas')
8
8
  */
9
9
  export { Canvas, CanvasToolbar, ContextMenu, PanZoomPanel, PropsPanel } from './canvas';
10
- export type { WidgetConversionPayload } from './canvas';
10
+ export type { WidgetConversionPayload, StrokeRecognizer, StrokeRecognitionRequest, StrokeRecognitionResult } from './canvas';
11
11
  export { setShapeImageStore, getShapeImageStore } from './canvas';
12
12
  export type { ShapeImageStore } from './canvas';
13
13
  export { Tooltip, ColorIC, FontSelectorIC, SliderUnitIC, TextAlignIC, TextStyleIC } from './components';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/canvas",
3
- "version": "0.5.14",
3
+ "version": "0.5.15",
4
4
  "type": "module",
5
5
  "svelte": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -47,8 +47,10 @@
47
47
  "build": "vite build && npm run package",
48
48
  "package": "NODE_ENV=production svelte-package",
49
49
  "package:watch": "NODE_ENV=development svelte-package --watch",
50
+ "test": "node --test \"scripts/*.test.ts\"",
50
51
  "preview": "vite preview",
51
52
  "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
53
+ "check:peers": "check-design-system-peers ..",
52
54
  "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
53
55
  "lint": "eslint . && prettier --check .",
54
56
  "format": "prettier --write ."
@@ -77,7 +79,8 @@
77
79
  "typescript": "^5.8.3",
78
80
  "typescript-eslint": "^8.20.0",
79
81
  "vite": "^6.2.6",
80
- "@widgetic/design-system": "^0.3.89"
82
+ "@widgetic/design-system": "^0.5.12",
83
+ "@widgetic/dev-tools": "^0.3.0"
81
84
  },
82
85
  "dependencies": {
83
86
  "@tailwindcss/typography": "^0.5.19",