@widgetic/canvas 0.5.13 → 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.
- package/dist/canvas/Canvas.svelte +453 -156
- package/dist/canvas/Canvas.svelte.d.ts +10 -7
- package/dist/canvas/CanvasToolbar.svelte +11 -6
- package/dist/canvas/CanvasToolbar.svelte.d.ts +4 -2
- package/dist/canvas/ContextMenu.svelte +3 -1
- package/dist/canvas/ContextMenu.svelte.d.ts +1 -0
- package/dist/canvas/PanZoomPanel.svelte +5 -0
- package/dist/canvas/WidgetSelectionLayer.svelte +26 -21
- package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +6 -15
- package/dist/canvas/index.d.ts +4 -0
- package/dist/canvas/index.js +2 -0
- package/dist/canvas/props-panel/PropsPanel.svelte +9 -3
- package/dist/canvas/shapes/WidgetShape.d.ts +4 -0
- package/dist/canvas/shapes/WidgetShape.js +60 -0
- package/dist/canvas/shapes/shapeImageStore.d.ts +37 -0
- package/dist/canvas/shapes/shapeImageStore.js +41 -0
- package/dist/canvas/shapes/strokeShapeSnap.d.ts +165 -0
- package/dist/canvas/shapes/strokeShapeSnap.js +701 -0
- package/dist/canvas/types.d.ts +35 -0
- package/dist/canvas/widgetPreviewGeometry.d.ts +9 -4
- package/dist/canvas/widgetPreviewGeometry.js +39 -18
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -0
- package/package.json +5 -2
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file strokeShapeSnap.ts
|
|
3
|
+
* @description Turn a hand-drawn freehand stroke into the shape it was meant to be.
|
|
4
|
+
*
|
|
5
|
+
* The Draw tool uses Fabric's `PencilBrush`, so a "shape" the user tries to draw
|
|
6
|
+
* arrives as a wobbly polyline — dozens of sampled points, corners that overshoot,
|
|
7
|
+
* edges that bow. This module measures that polyline and decides whether it is a
|
|
8
|
+
* clean enough instance of a shape the canvas can actually build.
|
|
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:
|
|
13
|
+
*
|
|
14
|
+
* 1. measure the stroke here, deterministically (closed? how many corners? how
|
|
15
|
+
* circular? how far off axis? aspect ratio?) — 0ms, no network;
|
|
16
|
+
* 2. gate every shape the canvas can build through the conditions it *cannot*
|
|
17
|
+
* be — a shape whose gate fails is never offered to anyone, so nothing
|
|
18
|
+
* impossible can be chosen;
|
|
19
|
+
* 3. rank the survivors by how well they match, and a decisive winner is
|
|
20
|
+
* applied immediately with no model call;
|
|
21
|
+
* 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.
|
|
25
|
+
*
|
|
26
|
+
* Everything here is pure: it takes command data and points, never a Fabric
|
|
27
|
+
* object, so it can be tested with `npm test` and reused by any caller that can
|
|
28
|
+
* produce a polyline.
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* The feature flag for this whole behaviour, as requested for the experiment:
|
|
32
|
+
* `true` means a recognized stroke is replaced by the clean shape, `false` means
|
|
33
|
+
* every stroke stays exactly as it was drawn. One line to roll back.
|
|
34
|
+
*/
|
|
35
|
+
export declare const CANVAS_SHAPE_SNAP_ENABLED = true;
|
|
36
|
+
/**
|
|
37
|
+
* The shapes snapping can produce — the shapes the canvas can *build from
|
|
38
|
+
* scratch*, which is what makes the replacement faithful to what the user would
|
|
39
|
+
* have got from the toolbar:
|
|
40
|
+
*
|
|
41
|
+
* - `rect` → Fabric `Rect` (a square is a `rect` whose bounds are square)
|
|
42
|
+
* - `circle` → Fabric `Circle` (the toolbar's Circle, not the Ellipse)
|
|
43
|
+
* - `oval` → Fabric `Ellipse` (the toolbar's oval/ellipse tool)
|
|
44
|
+
* - `triangle` → the same regenerated path the Triangle tool produces
|
|
45
|
+
* - `diamond` → the `diamond` library shape
|
|
46
|
+
* - `hexagon` → the `hexagon` library shape
|
|
47
|
+
*
|
|
48
|
+
* Deliberately absent: **line and arrow**. The canvas has no bare-line tool — the
|
|
49
|
+
* only straight-line object it offers is an arrow, which always draws a head, so
|
|
50
|
+
* snapping a straight stroke to one would *add* a mark the user did not draw. A
|
|
51
|
+
* straight stroke therefore has no candidates at all and is left alone.
|
|
52
|
+
*/
|
|
53
|
+
export type CanvasSnapShape = 'rect' | 'circle' | 'oval' | 'triangle' | 'diamond' | 'hexagon';
|
|
54
|
+
export declare const CANVAS_SNAP_SHAPES: readonly CanvasSnapShape[];
|
|
55
|
+
/** How each candidate is described when the roster is shown to JEV. */
|
|
56
|
+
export declare const CANVAS_SNAP_SHAPE_LABELS: Record<CanvasSnapShape, string>;
|
|
57
|
+
export interface StrokePoint {
|
|
58
|
+
x: number;
|
|
59
|
+
y: number;
|
|
60
|
+
}
|
|
61
|
+
/** One Fabric/SVG path command: `['M', x, y]`, `['Q', cx, cy, x, y]`, … */
|
|
62
|
+
export type PathCommand = [string, ...number[]];
|
|
63
|
+
/**
|
|
64
|
+
* A stroke's measurements — the vocabulary the shape profiles are written in and
|
|
65
|
+
* the vocabulary JEV is shown. Every field is scale-free (a ratio or a 0..1
|
|
66
|
+
* score) except the bounds, so the same drawing at any zoom measures the same.
|
|
67
|
+
*/
|
|
68
|
+
export interface StrokeMeasurements {
|
|
69
|
+
/** Sampled points the measurements were taken from (after resampling). */
|
|
70
|
+
pointCount: number;
|
|
71
|
+
/** True when the ends nearly meet (gap ≤ `CLOSED_GAP_RATIO` of the diagonal). */
|
|
72
|
+
closed: boolean;
|
|
73
|
+
/** Gap between the stroke's ends, as a fraction of the bounding-box diagonal. */
|
|
74
|
+
gapRatio: number;
|
|
75
|
+
/** Vertices left after simplification — the outline's straight edges. */
|
|
76
|
+
vertexCount: number;
|
|
77
|
+
/** Corners found on the outline (3 ≈ triangle, 4 ≈ rectangle, 6 ≈ hexagon). */
|
|
78
|
+
cornerCount: number;
|
|
79
|
+
/** Mean sharpness of the detected corners; 1 ≈ hard corners, 0 ≈ rounded. */
|
|
80
|
+
cornerSharpness: number;
|
|
81
|
+
/** Bounding-box width ÷ height, always ≥ 1 (orientation-independent). */
|
|
82
|
+
aspectRatio: number;
|
|
83
|
+
/** 1 = a single straight line. Only meaningful for an open stroke. */
|
|
84
|
+
straightness: number;
|
|
85
|
+
/** 4πA / P² — 1 for a perfect circle, lower for anything else. */
|
|
86
|
+
circularity: number;
|
|
87
|
+
/** Share of the outline (by length) running within 20° of horizontal or vertical. */
|
|
88
|
+
axisAlignment: number;
|
|
89
|
+
/** 0 = edges sit on the axes, 1 = edges sit on the diagonals (a diamond). */
|
|
90
|
+
axisTilt: number;
|
|
91
|
+
/** 1 = every boundary point the same distance from the centre (a circle). */
|
|
92
|
+
radialUniformity: number;
|
|
93
|
+
/** The stroke's bounds in the path's own coordinates. */
|
|
94
|
+
bounds: SnapBounds;
|
|
95
|
+
}
|
|
96
|
+
export interface SnapRanking {
|
|
97
|
+
shape: CanvasSnapShape;
|
|
98
|
+
/** 0..1 — how well the measurements match this shape's profile. */
|
|
99
|
+
score: number;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A decisive winner needs a solid score *and* a lead over the runner-up. Below
|
|
103
|
+
* either bar the caller escalates to JEV instead of snapping on a coin flip.
|
|
104
|
+
*/
|
|
105
|
+
export declare const STROKE_SNAP_MIN_SCORE = 0.78;
|
|
106
|
+
export declare const STROKE_SNAP_MIN_MARGIN = 0.12;
|
|
107
|
+
/**
|
|
108
|
+
* Flatten path commands into a polyline.
|
|
109
|
+
*
|
|
110
|
+
* Fabric's `PencilBrush` emits `M` followed by a run of quadratic curves whose
|
|
111
|
+
* control point is the sampled pointer and whose endpoint is the midpoint of the
|
|
112
|
+
* next segment, so the curves must be sampled rather than read as endpoints.
|
|
113
|
+
* `L`, `H`, `V` and `Z` are handled too, which makes this usable on any path the
|
|
114
|
+
* canvas holds — including the clean shapes themselves.
|
|
115
|
+
*/
|
|
116
|
+
export declare function flattenPathCommands(commands: readonly PathCommand[], samplesPerCurve?: number): StrokePoint[];
|
|
117
|
+
/** Uniform arc-length resample, so fast and slow strokes are measured the same. */
|
|
118
|
+
export declare function resample(points: readonly StrokePoint[], count?: number): StrokePoint[];
|
|
119
|
+
/** Ramer–Douglas–Peucker — the vertices a human would name as the corners. */
|
|
120
|
+
export declare function simplify(points: readonly StrokePoint[], tolerance: number): StrokePoint[];
|
|
121
|
+
export declare function measureStroke(rawPoints: readonly StrokePoint[]): StrokeMeasurements | null;
|
|
122
|
+
/**
|
|
123
|
+
* The shapes this stroke could still plausibly be, best first.
|
|
124
|
+
*
|
|
125
|
+
* Empty means "not a shape the canvas can build" — a line, a scribble, a
|
|
126
|
+
* signature, a check mark, a tilted quad — and the caller must leave the drawing
|
|
127
|
+
* untouched. The returned list doubles as the roster for the JEV escalation, so
|
|
128
|
+
* JEV is only ever asked to choose between shapes the stroke could actually be.
|
|
129
|
+
*/
|
|
130
|
+
export declare function rankSnapShapes(measurements: StrokeMeasurements): SnapRanking[];
|
|
131
|
+
/**
|
|
132
|
+
* The winner when it is decisive — a solid score *and* a clear lead over the
|
|
133
|
+
* runner-up. `null` means "ask JEV", or, when there is no JEV to ask, leave the
|
|
134
|
+
* stroke as drawn.
|
|
135
|
+
*/
|
|
136
|
+
export declare function confidentSnapShape(rankings: readonly SnapRanking[]): SnapRanking | null;
|
|
137
|
+
export interface SnapBounds {
|
|
138
|
+
left: number;
|
|
139
|
+
top: number;
|
|
140
|
+
width: number;
|
|
141
|
+
height: number;
|
|
142
|
+
}
|
|
143
|
+
export interface SnapPlacement {
|
|
144
|
+
/** Bounds the replacement shape should occupy. */
|
|
145
|
+
bounds: SnapBounds;
|
|
146
|
+
/**
|
|
147
|
+
* For shapes that must stay proportional (the circle, and the square-designed
|
|
148
|
+
* library shapes), the side length to use, centred inside `bounds`. Undefined
|
|
149
|
+
* when the shape may stretch to fill them.
|
|
150
|
+
*/
|
|
151
|
+
forcedSide?: number;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Where the clean shape goes: exactly the stroke's bounds.
|
|
155
|
+
*
|
|
156
|
+
* Three shapes refuse to stretch, because stretching them would change which
|
|
157
|
+
* shape they are: a circle whose bounds are 2:1 is an oval, and the library
|
|
158
|
+
* shapes are square-designed symbols. Those get the smaller side, centred on the
|
|
159
|
+
* stroke — the same thing the toolbar does when `lockAspectRatio` is set.
|
|
160
|
+
*
|
|
161
|
+
* Rotation is deliberately not applied: the replacement is upright, which is
|
|
162
|
+
* what the toolbar would have given the user, and strokes tilted far enough to
|
|
163
|
+
* matter never reach here (see `SHAPE_GATES`).
|
|
164
|
+
*/
|
|
165
|
+
export declare function planSnapPlacement(shape: CanvasSnapShape, bounds: SnapBounds): SnapPlacement;
|