@widgetic/canvas 0.5.41 → 0.5.43
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/README.md +1 -0
- package/dist/canvas/DomCanvas.svelte +49 -20
- package/dist/canvas/DomCanvas.svelte.d.ts +13 -7
- package/dist/canvas/autosaveScheduler.js +37 -89
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +7 -19
- package/dist/canvas/chromeLayout.d.ts +14 -3
- package/dist/canvas/chromeLayout.js +30 -20
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.d.ts +47 -5
- package/dist/canvas/host/CanvasHost.js +1481 -1566
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/browserServices.js +65 -116
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/nearCards.d.ts +33 -0
- package/dist/canvas/host/nearCards.js +78 -0
- package/dist/canvas/host/pageEvents.d.ts +16 -4
- package/dist/canvas/host/pageEvents.js +17 -8
- package/dist/canvas/host/pictureSaves.d.ts +33 -0
- package/dist/canvas/host/pictureSaves.js +42 -0
- package/dist/canvas/host/screenRects.d.ts +5 -0
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/storedPictures.d.ts +88 -0
- package/dist/canvas/host/storedPictures.js +288 -0
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/index.d.ts +1 -1
- package/dist/canvas/index.js +1 -1
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +6 -7
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +13 -25
- package/dist/canvas/shapes/ShapeLibrary.js +138 -148
- package/dist/canvas/shapes/shapeImageStore.d.ts +45 -2
- package/dist/canvas/shapes/shapeImageStore.js +10 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.d.ts +2 -2
- package/dist/canvas/toolbarPosition.js +4 -4
- package/dist/canvas/ui/SelectionPanel.svelte +28 -5
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/SelectorIndicator.svelte +81 -0
- package/dist/canvas/ui/SelectorIndicator.svelte.d.ts +11 -0
- package/dist/canvas/ui/ToolButton.svelte +8 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +2 -2
- package/dist/canvas/ui/floatingCard.js +20 -20
- package/dist/canvas/ui/floatingLayer.d.ts +13 -2
- package/dist/canvas/ui/floatingLayer.js +18 -7
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.js +34 -40
- package/dist/canvas/view/CanvasStage.svelte +101 -67
- package/dist/canvas/view/LeftToRightScope.svelte +21 -0
- package/dist/canvas/view/LeftToRightScope.svelte.d.ts +7 -0
- package/dist/canvas/view/ObjectView.svelte +15 -10
- package/dist/canvas/view/ObjectView.svelte.d.ts +1 -1
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/canvas/view/liftedScene.d.ts +31 -0
- package/dist/canvas/view/liftedScene.js +146 -0
- package/dist/canvas/view/mountedScene.d.ts +71 -0
- package/dist/canvas/view/mountedScene.js +363 -0
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +15 -1
- package/dist/engine/EngineCore.js +724 -797
- package/dist/engine/codec/canvasDocument.d.ts +5 -0
- package/dist/engine/codec/canvasDocument.js +212 -149
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +76 -89
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +67 -94
- package/dist/engine/core/defaults.js +90 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.d.ts +6 -0
- package/dist/engine/core/history.js +49 -50
- package/dist/engine/core/hitTest.js +68 -81
- package/dist/engine/core/matrix.js +18 -20
- package/dist/engine/core/pathData.js +183 -210
- package/dist/engine/core/rotationReset.js +5 -6
- package/dist/engine/core/scene.d.ts +10 -0
- package/dist/engine/core/scene.js +153 -187
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +31 -64
- package/dist/engine/core/transform.js +83 -94
- package/dist/engine/core/types.d.ts +8 -2
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +467 -474
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +265 -309
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +27 -30
- package/dist/engine/input/selectionHandles.js +45 -50
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +86 -158
- package/dist/engine/state.d.ts +20 -0
- package/dist/engine/state.js +5 -15
- package/dist/engine/text/layoutText.js +43 -62
- package/dist/engine/text/textMetrics.js +8 -10
- package/dist/engine/widgets.js +101 -137
- package/dist/fonts/interFont.d.ts +10 -1
- package/dist/fonts/interFont.js +47 -75
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.d.ts +8 -1
- package/dist/i18n/messages.js +10 -2
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/settings/workspaceToolSettings.js +32 -48
- package/dist/tools/components/catalog.js +64 -74
- package/dist/tools/components/grouping.js +6 -28
- package/dist/tools/definitions.js +5 -5
- package/dist/tools/executors.js +105 -141
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/types.js +1 -1
- package/package.json +1 -1
- package/dist/canvas/ui/SelectorBar.svelte +0 -84
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +0 -12
|
@@ -27,33 +27,13 @@
|
|
|
27
27
|
* the board, so it can be tested with `npm test` and reused by any caller that can
|
|
28
28
|
* produce a polyline.
|
|
29
29
|
*/
|
|
30
|
-
var __assign = (this && this.__assign) || function () {
|
|
31
|
-
__assign = Object.assign || function(t) {
|
|
32
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
33
|
-
s = arguments[i];
|
|
34
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
35
|
-
t[p] = s[p];
|
|
36
|
-
}
|
|
37
|
-
return t;
|
|
38
|
-
};
|
|
39
|
-
return __assign.apply(this, arguments);
|
|
40
|
-
};
|
|
41
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
42
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
43
|
-
if (ar || !(i in from)) {
|
|
44
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
45
|
-
ar[i] = from[i];
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
49
|
-
};
|
|
50
30
|
/**
|
|
51
31
|
* The feature flag for this whole behaviour, as requested for the experiment:
|
|
52
32
|
* `true` means a recognized stroke is replaced by the clean shape, `false` means
|
|
53
33
|
* every stroke stays exactly as it was drawn. One line to roll back.
|
|
54
34
|
*/
|
|
55
|
-
export
|
|
56
|
-
export
|
|
35
|
+
export const CANVAS_SHAPE_SNAP_ENABLED = false;
|
|
36
|
+
export const CANVAS_SNAP_SHAPES = [
|
|
57
37
|
'rect',
|
|
58
38
|
'circle',
|
|
59
39
|
'oval',
|
|
@@ -62,7 +42,7 @@ export var CANVAS_SNAP_SHAPES = [
|
|
|
62
42
|
'hexagon'
|
|
63
43
|
];
|
|
64
44
|
/** How each candidate is described when the roster is shown to JEV. */
|
|
65
|
-
export
|
|
45
|
+
export const CANVAS_SNAP_SHAPE_LABELS = {
|
|
66
46
|
rect: 'rectangle or square',
|
|
67
47
|
circle: 'circle',
|
|
68
48
|
oval: 'oval (ellipse)',
|
|
@@ -74,12 +54,12 @@ export var CANVAS_SNAP_SHAPE_LABELS = {
|
|
|
74
54
|
* A decisive winner needs a solid score *and* a lead over the runner-up. Below
|
|
75
55
|
* either bar the caller escalates to JEV instead of snapping on a coin flip.
|
|
76
56
|
*/
|
|
77
|
-
export
|
|
78
|
-
export
|
|
57
|
+
export const STROKE_SNAP_MIN_SCORE = 0.78;
|
|
58
|
+
export const STROKE_SNAP_MIN_MARGIN = 0.12;
|
|
79
59
|
/** Ends closer than this fraction of the diagonal count as a closed shape. */
|
|
80
|
-
|
|
60
|
+
const CLOSED_GAP_RATIO = 0.25;
|
|
81
61
|
/** A turn this far from straight counts as a corner. */
|
|
82
|
-
|
|
62
|
+
const CORNER_TURN_RAD = Math.PI / 4;
|
|
83
63
|
/**
|
|
84
64
|
* Corner detection runs on the resampled ring, in units of samples: the turn at
|
|
85
65
|
* a point is measured between the directions `CORNER_HALF_WINDOW` samples either
|
|
@@ -91,14 +71,14 @@ var CORNER_TURN_RAD = Math.PI / 4;
|
|
|
91
71
|
* at the local turn; what makes them different is that a rectangle is straight
|
|
92
72
|
* on both sides of its corner and an oval never is.
|
|
93
73
|
*/
|
|
94
|
-
|
|
95
|
-
|
|
74
|
+
const CORNER_HALF_WINDOW = 3;
|
|
75
|
+
const CORNER_FLAT_WINDOW = 5;
|
|
96
76
|
/** Corners within this many samples of each other are one corner, not two. */
|
|
97
|
-
|
|
77
|
+
const CORNER_MERGE_SAMPLES = 6;
|
|
98
78
|
/** A direction within this many radians of an axis counts as axis-aligned. */
|
|
99
|
-
|
|
79
|
+
const AXIS_TOLERANCE_RAD = Math.PI / 9;
|
|
100
80
|
/** Uniform arc-length samples taken before measuring — decoupled from draw speed. */
|
|
101
|
-
|
|
81
|
+
const RESAMPLE_COUNT = 96;
|
|
102
82
|
// ── Path disassembly ────────────────────────────────────────────────────────
|
|
103
83
|
/**
|
|
104
84
|
* Flatten path commands into a polyline.
|
|
@@ -111,25 +91,23 @@ var RESAMPLE_COUNT = 96;
|
|
|
111
91
|
* tool is measured through the line it holds, not through its outline: see
|
|
112
92
|
* `strokeMeasurementPoints` in `host/strokeSnap.ts`.)
|
|
113
93
|
*/
|
|
114
|
-
export function flattenPathCommands(commands, samplesPerCurve) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
var push = function (point) {
|
|
121
|
-
var last = points[points.length - 1];
|
|
94
|
+
export function flattenPathCommands(commands, samplesPerCurve = 10) {
|
|
95
|
+
const points = [];
|
|
96
|
+
let cursor = { x: 0, y: 0 };
|
|
97
|
+
let subpathStart = { x: 0, y: 0 };
|
|
98
|
+
const push = (point) => {
|
|
99
|
+
const last = points[points.length - 1];
|
|
122
100
|
if (!last || Math.abs(last.x - point.x) > 1e-6 || Math.abs(last.y - point.y) > 1e-6) {
|
|
123
101
|
points.push(point);
|
|
124
102
|
}
|
|
125
103
|
};
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
104
|
+
for (const command of commands) {
|
|
105
|
+
const op = String(command[0] ?? '').toUpperCase();
|
|
106
|
+
const n = (i) => Number(command[i]) || 0;
|
|
129
107
|
switch (op) {
|
|
130
108
|
case 'M':
|
|
131
109
|
cursor = { x: n(1), y: n(2) };
|
|
132
|
-
subpathStart =
|
|
110
|
+
subpathStart = { ...cursor };
|
|
133
111
|
push(cursor);
|
|
134
112
|
break;
|
|
135
113
|
case 'L':
|
|
@@ -145,97 +123,92 @@ export function flattenPathCommands(commands, samplesPerCurve) {
|
|
|
145
123
|
push(cursor);
|
|
146
124
|
break;
|
|
147
125
|
case 'Q': {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
for (
|
|
126
|
+
const control = { x: n(1), y: n(2) };
|
|
127
|
+
const end = { x: n(3), y: n(4) };
|
|
128
|
+
for (let i = 1; i <= samplesPerCurve; i++) {
|
|
151
129
|
push(quadraticPoint(cursor, control, end, i / samplesPerCurve));
|
|
152
130
|
}
|
|
153
131
|
cursor = end;
|
|
154
132
|
break;
|
|
155
133
|
}
|
|
156
134
|
case 'C': {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
for (
|
|
135
|
+
const c1 = { x: n(1), y: n(2) };
|
|
136
|
+
const c2 = { x: n(3), y: n(4) };
|
|
137
|
+
const end = { x: n(5), y: n(6) };
|
|
138
|
+
for (let i = 1; i <= samplesPerCurve; i++) {
|
|
161
139
|
push(cubicPoint(cursor, c1, c2, end, i / samplesPerCurve));
|
|
162
140
|
}
|
|
163
141
|
cursor = end;
|
|
164
142
|
break;
|
|
165
143
|
}
|
|
166
144
|
case 'Z':
|
|
167
|
-
cursor =
|
|
145
|
+
cursor = { ...subpathStart };
|
|
168
146
|
push(cursor);
|
|
169
147
|
break;
|
|
170
148
|
default:
|
|
171
149
|
break;
|
|
172
150
|
}
|
|
173
|
-
};
|
|
174
|
-
for (var _i = 0, commands_1 = commands; _i < commands_1.length; _i++) {
|
|
175
|
-
var command = commands_1[_i];
|
|
176
|
-
_loop_1(command);
|
|
177
151
|
}
|
|
178
152
|
return points;
|
|
179
153
|
}
|
|
180
154
|
function quadraticPoint(p0, p1, p2, t) {
|
|
181
|
-
|
|
155
|
+
const u = 1 - t;
|
|
182
156
|
return {
|
|
183
157
|
x: u * u * p0.x + 2 * u * t * p1.x + t * t * p2.x,
|
|
184
158
|
y: u * u * p0.y + 2 * u * t * p1.y + t * t * p2.y
|
|
185
159
|
};
|
|
186
160
|
}
|
|
187
161
|
function cubicPoint(p0, p1, p2, p3, t) {
|
|
188
|
-
|
|
162
|
+
const u = 1 - t;
|
|
189
163
|
return {
|
|
190
|
-
x:
|
|
191
|
-
y:
|
|
164
|
+
x: u ** 3 * p0.x + 3 * u * u * t * p1.x + 3 * u * t * t * p2.x + t ** 3 * p3.x,
|
|
165
|
+
y: u ** 3 * p0.y + 3 * u * u * t * p1.y + 3 * u * t * t * p2.y + t ** 3 * p3.y
|
|
192
166
|
};
|
|
193
167
|
}
|
|
194
168
|
// ── Resampling and simplification ───────────────────────────────────────────
|
|
195
169
|
/** Uniform arc-length resample, so fast and slow strokes are measured the same. */
|
|
196
|
-
export function resample(points, count) {
|
|
197
|
-
if (count === void 0) { count = RESAMPLE_COUNT; }
|
|
170
|
+
export function resample(points, count = RESAMPLE_COUNT) {
|
|
198
171
|
if (points.length < 2)
|
|
199
|
-
return points.map(
|
|
200
|
-
|
|
201
|
-
for (
|
|
172
|
+
return points.map((p) => ({ ...p }));
|
|
173
|
+
const cumulative = [0];
|
|
174
|
+
for (let i = 1; i < points.length; i++) {
|
|
202
175
|
cumulative.push(cumulative[i - 1] + distance(points[i - 1], points[i]));
|
|
203
176
|
}
|
|
204
|
-
|
|
177
|
+
const total = cumulative[cumulative.length - 1];
|
|
205
178
|
if (total <= 0)
|
|
206
|
-
return points.map(
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
for (
|
|
211
|
-
|
|
179
|
+
return points.map((p) => ({ ...p }));
|
|
180
|
+
const step = total / (count - 1);
|
|
181
|
+
const out = [{ ...points[0] }];
|
|
182
|
+
let segment = 0;
|
|
183
|
+
for (let i = 1; i < count - 1; i++) {
|
|
184
|
+
const target = i * step;
|
|
212
185
|
while (segment < cumulative.length - 2 && cumulative[segment + 1] < target)
|
|
213
186
|
segment++;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
187
|
+
const spanStart = cumulative[segment];
|
|
188
|
+
const spanLength = cumulative[segment + 1] - spanStart;
|
|
189
|
+
const t = spanLength > 0 ? (target - spanStart) / spanLength : 0;
|
|
217
190
|
out.push({
|
|
218
191
|
x: points[segment].x + (points[segment + 1].x - points[segment].x) * t,
|
|
219
192
|
y: points[segment].y + (points[segment + 1].y - points[segment].y) * t
|
|
220
193
|
});
|
|
221
194
|
}
|
|
222
|
-
out.push(
|
|
195
|
+
out.push({ ...points[points.length - 1] });
|
|
223
196
|
return out;
|
|
224
197
|
}
|
|
225
198
|
/** Ramer–Douglas–Peucker — the vertices a human would name as the corners. */
|
|
226
199
|
export function simplify(points, tolerance) {
|
|
227
200
|
if (points.length <= 2)
|
|
228
|
-
return points.map(
|
|
229
|
-
|
|
201
|
+
return points.map((p) => ({ ...p }));
|
|
202
|
+
const keep = new Array(points.length).fill(false);
|
|
230
203
|
keep[0] = true;
|
|
231
204
|
keep[points.length - 1] = true;
|
|
232
|
-
|
|
205
|
+
const stack = [[0, points.length - 1]];
|
|
233
206
|
while (stack.length > 0) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
for (
|
|
238
|
-
|
|
207
|
+
const [first, last] = stack.pop();
|
|
208
|
+
let worst = -1;
|
|
209
|
+
let worstDistance = tolerance;
|
|
210
|
+
for (let i = first + 1; i < last; i++) {
|
|
211
|
+
const d = perpendicularDistance(points[i], points[first], points[last]);
|
|
239
212
|
if (d > worstDistance) {
|
|
240
213
|
worst = i;
|
|
241
214
|
worstDistance = d;
|
|
@@ -246,30 +219,30 @@ export function simplify(points, tolerance) {
|
|
|
246
219
|
stack.push([first, worst], [worst, last]);
|
|
247
220
|
}
|
|
248
221
|
}
|
|
249
|
-
return points.filter(
|
|
222
|
+
return points.filter((_, i) => keep[i]).map((p) => ({ ...p }));
|
|
250
223
|
}
|
|
251
224
|
// ── Measurement ─────────────────────────────────────────────────────────────
|
|
252
225
|
export function measureStroke(rawPoints) {
|
|
253
226
|
if (rawPoints.length < 3)
|
|
254
227
|
return null;
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
228
|
+
const points = resample(rawPoints);
|
|
229
|
+
const bounds = boundingBox(points);
|
|
230
|
+
const diagonal = Math.hypot(bounds.width, bounds.height);
|
|
258
231
|
if (diagonal <= 0)
|
|
259
232
|
return null;
|
|
260
233
|
// Simplify with a tolerance proportional to the stroke, so the same drawing
|
|
261
234
|
// at any zoom yields the same vertices — this is the edge direction set that
|
|
262
235
|
// axis alignment and tilt are read from.
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
236
|
+
const simplified = simplify(points, Math.max(1.5, diagonal * 0.025));
|
|
237
|
+
const gap = distance(points[0], points[points.length - 1]);
|
|
238
|
+
const gapRatio = gap / diagonal;
|
|
239
|
+
const closed = gapRatio <= CLOSED_GAP_RATIO;
|
|
240
|
+
const corners = detectCorners(points, closed, diagonal);
|
|
241
|
+
const perimeter = pathLength(points);
|
|
242
|
+
const area = Math.abs(polygonArea(points));
|
|
270
243
|
return {
|
|
271
244
|
pointCount: points.length,
|
|
272
|
-
closed
|
|
245
|
+
closed,
|
|
273
246
|
gapRatio: round(gapRatio),
|
|
274
247
|
vertexCount: simplified.length,
|
|
275
248
|
cornerCount: corners.count,
|
|
@@ -280,7 +253,7 @@ export function measureStroke(rawPoints) {
|
|
|
280
253
|
axisAlignment: round(axisAlignment(simplified)),
|
|
281
254
|
axisTilt: round(axisTilt(simplified)),
|
|
282
255
|
radialUniformity: round(radialUniformity(points)),
|
|
283
|
-
bounds
|
|
256
|
+
bounds
|
|
284
257
|
};
|
|
285
258
|
}
|
|
286
259
|
/**
|
|
@@ -295,23 +268,23 @@ export function measureStroke(rawPoints) {
|
|
|
295
268
|
* straddles the joint from being counted twice.
|
|
296
269
|
*/
|
|
297
270
|
function detectCorners(ring, closed, diagonal) {
|
|
298
|
-
|
|
271
|
+
const count = ring.length;
|
|
299
272
|
if (count < 2 * CORNER_HALF_WINDOW + 3)
|
|
300
273
|
return { count: 0, maxSharpness: 0 };
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
for (
|
|
306
|
-
|
|
307
|
-
|
|
274
|
+
const flatnessTolerance = diagonal * 0.02;
|
|
275
|
+
const at = (index) => ring[closed ? (index + count) % count : index];
|
|
276
|
+
const inRange = (index) => closed || (index >= 0 && index < count);
|
|
277
|
+
const candidates = [];
|
|
278
|
+
for (let i = 0; i < count; i++) {
|
|
279
|
+
const start = i - CORNER_HALF_WINDOW;
|
|
280
|
+
const end = i + CORNER_HALF_WINDOW;
|
|
308
281
|
if (!inRange(start) || !inRange(end))
|
|
309
282
|
continue;
|
|
310
|
-
|
|
283
|
+
const turn = turnAngle(at(start), at(i), at(end));
|
|
311
284
|
if (turn < CORNER_TURN_RAD)
|
|
312
285
|
continue;
|
|
313
|
-
|
|
314
|
-
|
|
286
|
+
const before = i - CORNER_HALF_WINDOW - CORNER_FLAT_WINDOW;
|
|
287
|
+
const after = i + CORNER_HALF_WINDOW + CORNER_FLAT_WINDOW;
|
|
315
288
|
if (!inRange(before) || !inRange(after))
|
|
316
289
|
continue;
|
|
317
290
|
if (!segmentIsFlat(ring, closed, before, i, flatnessTolerance))
|
|
@@ -324,21 +297,20 @@ function detectCorners(ring, closed, diagonal) {
|
|
|
324
297
|
sharpness: Math.min(1, (turn - CORNER_TURN_RAD) / (Math.PI / 2 - CORNER_TURN_RAD))
|
|
325
298
|
});
|
|
326
299
|
}
|
|
327
|
-
|
|
300
|
+
const merged = mergeAdjacentCorners(candidates, count, closed);
|
|
328
301
|
return {
|
|
329
302
|
count: merged.length,
|
|
330
|
-
maxSharpness: merged.length > 0 ? Math.max
|
|
303
|
+
maxSharpness: merged.length > 0 ? Math.max(...merged.map((c) => c.sharpness)) : 0
|
|
331
304
|
};
|
|
332
305
|
}
|
|
333
306
|
/** One corner per cluster, keeping the sharpest reading in it. */
|
|
334
307
|
function mergeAdjacentCorners(candidates, ringLength, closed) {
|
|
335
308
|
if (candidates.length === 0)
|
|
336
309
|
return [];
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
for (
|
|
340
|
-
|
|
341
|
-
var last = merged[merged.length - 1];
|
|
310
|
+
const sorted = [...candidates].sort((a, b) => a.index - b.index);
|
|
311
|
+
const merged = [];
|
|
312
|
+
for (const candidate of sorted) {
|
|
313
|
+
const last = merged[merged.length - 1];
|
|
342
314
|
if (last && candidate.index - last.index <= CORNER_MERGE_SAMPLES) {
|
|
343
315
|
if (candidate.sharpness > last.sharpness)
|
|
344
316
|
merged[merged.length - 1] = candidate;
|
|
@@ -349,8 +321,8 @@ function mergeAdjacentCorners(candidates, ringLength, closed) {
|
|
|
349
321
|
// The ring's first and last clusters can be the same corner either side of
|
|
350
322
|
// the joint.
|
|
351
323
|
if (closed && merged.length > 1) {
|
|
352
|
-
|
|
353
|
-
|
|
324
|
+
const first = merged[0];
|
|
325
|
+
const last = merged[merged.length - 1];
|
|
354
326
|
if (ringLength - last.index + first.index <= CORNER_MERGE_SAMPLES) {
|
|
355
327
|
if (first.sharpness >= last.sharpness)
|
|
356
328
|
merged.pop();
|
|
@@ -362,15 +334,15 @@ function mergeAdjacentCorners(candidates, ringLength, closed) {
|
|
|
362
334
|
}
|
|
363
335
|
/** Whether the ring samples between two indices stay on their own chord. */
|
|
364
336
|
function segmentIsFlat(ring, closed, from, to, tolerance) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
337
|
+
const length = ring.length;
|
|
338
|
+
const start = ring[closed ? ((from % length) + length) % length : from];
|
|
339
|
+
const end = ring[closed ? ((to % length) + length) % length : to];
|
|
368
340
|
if (!start || !end)
|
|
369
341
|
return false;
|
|
370
|
-
|
|
371
|
-
for (
|
|
372
|
-
|
|
373
|
-
|
|
342
|
+
const steps = Math.abs(to - from);
|
|
343
|
+
for (let step = 1; step < steps; step++) {
|
|
344
|
+
const index = closed ? (((from + step) % length) + length) % length : from + step;
|
|
345
|
+
const point = ring[index];
|
|
374
346
|
if (!point)
|
|
375
347
|
return false;
|
|
376
348
|
if (perpendicularDistance(point, start, end) > tolerance)
|
|
@@ -380,32 +352,30 @@ function segmentIsFlat(ring, closed, from, to, tolerance) {
|
|
|
380
352
|
}
|
|
381
353
|
/** How far the stroke drifts sideways from the straight chord between its ends. */
|
|
382
354
|
function straightness(points) {
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
355
|
+
const start = points[0];
|
|
356
|
+
const end = points[points.length - 1];
|
|
357
|
+
const chord = distance(start, end);
|
|
386
358
|
if (chord <= 1e-6)
|
|
387
359
|
return 0;
|
|
388
|
-
|
|
389
|
-
for (
|
|
390
|
-
var point = points_1[_i];
|
|
360
|
+
let worst = 0;
|
|
361
|
+
for (const point of points)
|
|
391
362
|
worst = Math.max(worst, perpendicularDistance(point, start, end));
|
|
392
|
-
}
|
|
393
363
|
// A quarter of the chord of sideways drift is already decidedly not a line.
|
|
394
364
|
return clamp01(1 - worst / chord / 0.25);
|
|
395
365
|
}
|
|
396
366
|
/** Share of the outline running within `AXIS_TOLERANCE_RAD` of horizontal or vertical. */
|
|
397
367
|
function axisAlignment(simplified) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
for (
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
368
|
+
let aligned = 0;
|
|
369
|
+
let total = 0;
|
|
370
|
+
for (let i = 1; i < simplified.length; i++) {
|
|
371
|
+
const dx = simplified[i].x - simplified[i - 1].x;
|
|
372
|
+
const dy = simplified[i].y - simplified[i - 1].y;
|
|
373
|
+
const length = Math.hypot(dx, dy);
|
|
404
374
|
if (length <= 1e-6)
|
|
405
375
|
continue;
|
|
406
376
|
total += length;
|
|
407
|
-
|
|
408
|
-
|
|
377
|
+
const angle = Math.atan2(Math.abs(dy), Math.abs(dx));
|
|
378
|
+
const offAxis = Math.min(angle, Math.abs(Math.PI / 2 - angle));
|
|
409
379
|
if (offAxis <= AXIS_TOLERANCE_RAD)
|
|
410
380
|
aligned += length;
|
|
411
381
|
}
|
|
@@ -422,16 +392,16 @@ function axisAlignment(simplified) {
|
|
|
422
392
|
* as the distance from that axis over the 45° maximum.
|
|
423
393
|
*/
|
|
424
394
|
function axisTilt(simplified) {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
for (
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
395
|
+
let sin = 0;
|
|
396
|
+
let cos = 0;
|
|
397
|
+
let total = 0;
|
|
398
|
+
for (let i = 1; i < simplified.length; i++) {
|
|
399
|
+
const dx = simplified[i].x - simplified[i - 1].x;
|
|
400
|
+
const dy = simplified[i].y - simplified[i - 1].y;
|
|
401
|
+
const length = Math.hypot(dx, dy);
|
|
432
402
|
if (length <= 1e-6)
|
|
433
403
|
continue;
|
|
434
|
-
|
|
404
|
+
const angle = Math.atan2(dy, dx);
|
|
435
405
|
sin += Math.sin(angle * 4) * length;
|
|
436
406
|
cos += Math.cos(angle * 4) * length;
|
|
437
407
|
total += length;
|
|
@@ -439,20 +409,20 @@ function axisTilt(simplified) {
|
|
|
439
409
|
if (total <= 0)
|
|
440
410
|
return 0;
|
|
441
411
|
// Back out of the ×4 folding: the result is an axis direction in [0, π/2).
|
|
442
|
-
|
|
412
|
+
let axis = Math.atan2(sin / total, cos / total) / 4;
|
|
443
413
|
if (axis < 0)
|
|
444
414
|
axis += Math.PI / 2;
|
|
445
|
-
|
|
415
|
+
const offAxis = Math.min(axis, Math.PI / 2 - axis);
|
|
446
416
|
return clamp01(offAxis / (Math.PI / 4));
|
|
447
417
|
}
|
|
448
418
|
/** 1 when every boundary point is equidistant from the centroid, 0 when very uneven. */
|
|
449
419
|
function radialUniformity(points) {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
420
|
+
const centre = centroid(points);
|
|
421
|
+
const radii = points.map((p) => distance(p, centre));
|
|
422
|
+
const mean = radii.reduce((a, b) => a + b, 0) / radii.length;
|
|
453
423
|
if (mean <= 1e-6)
|
|
454
424
|
return 0;
|
|
455
|
-
|
|
425
|
+
const variance = radii.reduce((sum, r) => sum + (r - mean) ** 2, 0) / radii.length;
|
|
456
426
|
// A rectangle's radii vary enough to reach ~0.88; a circle stays at ~1.
|
|
457
427
|
return clamp01(1 - Math.sqrt(variance) / mean);
|
|
458
428
|
}
|
|
@@ -461,7 +431,7 @@ function radialUniformity(points) {
|
|
|
461
431
|
* function scores every candidate and a slightly-loose drawing still scores
|
|
462
432
|
* well on the features it does satisfy.
|
|
463
433
|
*/
|
|
464
|
-
|
|
434
|
+
const SHAPE_PROFILES = {
|
|
465
435
|
rect: {
|
|
466
436
|
corners: [3.5, 4.5],
|
|
467
437
|
circularity: [0, 0.85],
|
|
@@ -522,23 +492,19 @@ var SHAPE_PROFILES = {
|
|
|
522
492
|
* eligibility, the scores decide confidence. A stroke that passes no gate has no
|
|
523
493
|
* candidates at all, and the caller leaves it exactly as drawn.
|
|
524
494
|
*/
|
|
525
|
-
|
|
526
|
-
rect:
|
|
527
|
-
|
|
528
|
-
},
|
|
529
|
-
triangle: function (m, closedish) { return closedish && m.cornerCount === 3 && m.straightness < 0.55; },
|
|
495
|
+
const SHAPE_GATES = {
|
|
496
|
+
rect: (m, closedish) => closedish && m.cornerCount >= 3 && m.cornerCount <= 5 && m.axisTilt <= 0.25,
|
|
497
|
+
triangle: (m, closedish) => closedish && m.cornerCount === 3 && m.straightness < 0.55,
|
|
530
498
|
// A diamond is a square turned 45°, so its edges run along the diagonals.
|
|
531
499
|
// The bar is high on purpose: a quad tilted 30° is neither an upright
|
|
532
500
|
// rectangle nor a diamond, and snapping it to either would look wrong.
|
|
533
|
-
diamond:
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
circle: function (m, closedish) { return closedish && m.cornerCount <= 2 && m.aspectRatio <= 1.16; },
|
|
538
|
-
oval: function (m, closedish) { return closedish && m.cornerCount <= 2 && m.aspectRatio >= 1.14; }
|
|
501
|
+
diamond: (m, closedish) => closedish && m.cornerCount >= 4 && m.cornerCount <= 5 && m.axisTilt >= 0.8,
|
|
502
|
+
hexagon: (m, closedish) => closedish && m.cornerCount >= 5 && m.cornerCount <= 8,
|
|
503
|
+
circle: (m, closedish) => closedish && m.cornerCount <= 2 && m.aspectRatio <= 1.16,
|
|
504
|
+
oval: (m, closedish) => closedish && m.cornerCount <= 2 && m.aspectRatio >= 1.14
|
|
539
505
|
};
|
|
540
506
|
/** Feature weights — corners and roundness discriminate more than shape drift. */
|
|
541
|
-
|
|
507
|
+
const FEATURE_WEIGHTS = {
|
|
542
508
|
closed: 1.6,
|
|
543
509
|
corners: 2,
|
|
544
510
|
circularity: 1.6,
|
|
@@ -557,29 +523,29 @@ var FEATURE_WEIGHTS = {
|
|
|
557
523
|
*/
|
|
558
524
|
export function rankSnapShapes(measurements) {
|
|
559
525
|
// Nothing closed *and* nothing cornered is not an outline at all.
|
|
560
|
-
|
|
526
|
+
const closedish = measurements.closed || measurements.gapRatio <= 0.5;
|
|
561
527
|
if (!closedish && measurements.cornerCount < 3)
|
|
562
528
|
return [];
|
|
563
|
-
return CANVAS_SNAP_SHAPES.filter(
|
|
564
|
-
.map(
|
|
565
|
-
shape
|
|
529
|
+
return CANVAS_SNAP_SHAPES.filter((shape) => SHAPE_GATES[shape](measurements, closedish))
|
|
530
|
+
.map((shape) => ({
|
|
531
|
+
shape,
|
|
566
532
|
score: round(scoreShape(measurements, SHAPE_PROFILES[shape], measurements.closed))
|
|
567
|
-
})
|
|
568
|
-
.sort(
|
|
533
|
+
}))
|
|
534
|
+
.sort((a, b) => b.score - a.score || a.shape.localeCompare(b.shape));
|
|
569
535
|
}
|
|
570
536
|
function scoreShape(measurements, profile, closed) {
|
|
571
537
|
// A closed shape drawn with a visible gap is still that shape, just less
|
|
572
538
|
// certainly.
|
|
573
|
-
|
|
539
|
+
const closedMatch = closed
|
|
574
540
|
? 1
|
|
575
541
|
: clamp01(1 - (measurements.gapRatio - CLOSED_GAP_RATIO) / 0.5) * 0.5;
|
|
576
|
-
|
|
542
|
+
const cornerMatch = measurements.cornerCount >= profile.corners[0] && measurements.cornerCount <= profile.corners[1]
|
|
577
543
|
? 1
|
|
578
544
|
: clamp01(1 - rangeDistance(measurements.cornerCount, profile.corners) / 1.5);
|
|
579
|
-
|
|
545
|
+
const sharpnessMatch = profile.minCornerSharpness > 0
|
|
580
546
|
? clamp01(1 - Math.max(0, profile.minCornerSharpness - measurements.cornerSharpness) / 0.5)
|
|
581
547
|
: 1;
|
|
582
|
-
|
|
548
|
+
const weighted = [
|
|
583
549
|
[closedMatch, FEATURE_WEIGHTS.closed],
|
|
584
550
|
// Circularity is the sharpest circle/oval discriminator, so it is
|
|
585
551
|
// satisfied instead of graded: a perfect circle is nothing like an oval.
|
|
@@ -602,14 +568,8 @@ function scoreShape(measurements, profile, closed) {
|
|
|
602
568
|
[cornerMatch, FEATURE_WEIGHTS.corners],
|
|
603
569
|
[sharpnessMatch, FEATURE_WEIGHTS.cornerSharpness]
|
|
604
570
|
];
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
return sum + weight;
|
|
608
|
-
}, 0);
|
|
609
|
-
return clamp01(weighted.reduce(function (sum, _a) {
|
|
610
|
-
var value = _a[0], weight = _a[1];
|
|
611
|
-
return sum + value * weight;
|
|
612
|
-
}, 0) / totalWeight);
|
|
571
|
+
const totalWeight = weighted.reduce((sum, [, weight]) => sum + weight, 0);
|
|
572
|
+
return clamp01(weighted.reduce((sum, [value, weight]) => sum + value * weight, 0) / totalWeight);
|
|
613
573
|
}
|
|
614
574
|
/**
|
|
615
575
|
* The winner when it is decisive — a solid score *and* a clear lead over the
|
|
@@ -617,7 +577,7 @@ function scoreShape(measurements, profile, closed) {
|
|
|
617
577
|
* stroke as drawn.
|
|
618
578
|
*/
|
|
619
579
|
export function confidentSnapShape(rankings) {
|
|
620
|
-
|
|
580
|
+
const [best, runnerUp] = rankings;
|
|
621
581
|
if (!best || best.score < STROKE_SNAP_MIN_SCORE)
|
|
622
582
|
return null;
|
|
623
583
|
if (runnerUp && best.score - runnerUp.score < STROKE_SNAP_MIN_MARGIN)
|
|
@@ -638,7 +598,7 @@ export function confidentSnapShape(rankings) {
|
|
|
638
598
|
*/
|
|
639
599
|
export function planSnapPlacement(shape, bounds) {
|
|
640
600
|
if (shape === 'circle' || shape === 'diamond' || shape === 'hexagon') {
|
|
641
|
-
|
|
601
|
+
const side = Math.max(1, Math.min(bounds.width, bounds.height));
|
|
642
602
|
return {
|
|
643
603
|
bounds: {
|
|
644
604
|
left: bounds.left + (bounds.width - side) / 2,
|
|
@@ -649,29 +609,27 @@ export function planSnapPlacement(shape, bounds) {
|
|
|
649
609
|
forcedSide: side
|
|
650
610
|
};
|
|
651
611
|
}
|
|
652
|
-
return { bounds
|
|
612
|
+
return { bounds };
|
|
653
613
|
}
|
|
654
614
|
// ── Small geometry helpers ──────────────────────────────────────────────────
|
|
655
615
|
function distance(a, b) {
|
|
656
616
|
return Math.hypot(a.x - b.x, a.y - b.y);
|
|
657
617
|
}
|
|
658
618
|
function centroid(points) {
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
for (
|
|
662
|
-
var point = points_2[_i];
|
|
619
|
+
let x = 0;
|
|
620
|
+
let y = 0;
|
|
621
|
+
for (const point of points) {
|
|
663
622
|
x += point.x;
|
|
664
623
|
y += point.y;
|
|
665
624
|
}
|
|
666
625
|
return { x: x / points.length, y: y / points.length };
|
|
667
626
|
}
|
|
668
627
|
function boundingBox(points) {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
for (
|
|
674
|
-
var point = points_3[_i];
|
|
628
|
+
let minX = Infinity;
|
|
629
|
+
let minY = Infinity;
|
|
630
|
+
let maxX = -Infinity;
|
|
631
|
+
let maxY = -Infinity;
|
|
632
|
+
for (const point of points) {
|
|
675
633
|
if (point.x < minX)
|
|
676
634
|
minX = point.x;
|
|
677
635
|
if (point.y < minY)
|
|
@@ -684,29 +642,29 @@ function boundingBox(points) {
|
|
|
684
642
|
return { left: minX, top: minY, width: maxX - minX, height: maxY - minY };
|
|
685
643
|
}
|
|
686
644
|
function pathLength(points) {
|
|
687
|
-
|
|
688
|
-
for (
|
|
645
|
+
let total = 0;
|
|
646
|
+
for (let i = 1; i < points.length; i++)
|
|
689
647
|
total += distance(points[i - 1], points[i]);
|
|
690
648
|
return total;
|
|
691
649
|
}
|
|
692
650
|
/** Signed area of the point ring. */
|
|
693
651
|
function polygonArea(points) {
|
|
694
|
-
|
|
695
|
-
for (
|
|
696
|
-
|
|
697
|
-
|
|
652
|
+
let area = 0;
|
|
653
|
+
for (let i = 0; i < points.length; i++) {
|
|
654
|
+
const a = points[i];
|
|
655
|
+
const b = points[(i + 1) % points.length];
|
|
698
656
|
area += a.x * b.y - b.x * a.y;
|
|
699
657
|
}
|
|
700
658
|
return area / 2;
|
|
701
659
|
}
|
|
702
660
|
/** Shortest distance from a point to the segment `a`–`b`. */
|
|
703
661
|
function perpendicularDistance(point, a, b) {
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
662
|
+
const dx = b.x - a.x;
|
|
663
|
+
const dy = b.y - a.y;
|
|
664
|
+
const lengthSquared = dx * dx + dy * dy;
|
|
707
665
|
if (lengthSquared <= 1e-12)
|
|
708
666
|
return distance(point, a);
|
|
709
|
-
|
|
667
|
+
const t = clamp01(((point.x - a.x) * dx + (point.y - a.y) * dy) / lengthSquared);
|
|
710
668
|
return distance(point, { x: a.x + t * dx, y: a.y + t * dy });
|
|
711
669
|
}
|
|
712
670
|
/**
|
|
@@ -714,13 +672,13 @@ function perpendicularDistance(point, a, b) {
|
|
|
714
672
|
* straight through it, 45° for a hexagon's vertex, 90° for a rectangle's corner.
|
|
715
673
|
*/
|
|
716
674
|
function turnAngle(previous, current, next) {
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
675
|
+
const a = { x: current.x - previous.x, y: current.y - previous.y };
|
|
676
|
+
const b = { x: next.x - current.x, y: next.y - current.y };
|
|
677
|
+
const lengthA = Math.hypot(a.x, a.y);
|
|
678
|
+
const lengthB = Math.hypot(b.x, b.y);
|
|
721
679
|
if (lengthA <= 1e-6 || lengthB <= 1e-6)
|
|
722
680
|
return 0;
|
|
723
|
-
|
|
681
|
+
const cosine = clampUnit((a.x * b.x + a.y * b.y) / (lengthA * lengthB));
|
|
724
682
|
return Math.acos(cosine);
|
|
725
683
|
}
|
|
726
684
|
/** 1 inside the range, decaying linearly outside it over `tolerance`. */
|
|
@@ -749,51 +707,48 @@ function round(value) {
|
|
|
749
707
|
* TypeSafe's System One API enforces 280–1568 px on each axis (78,400–2,458,624 px²);
|
|
750
708
|
* 320×320 sits safely inside the floor while keeping input tokens low (~335 tokens).
|
|
751
709
|
*/
|
|
752
|
-
export
|
|
710
|
+
export const STROKE_SNAP_IMAGE_SIDE = 320;
|
|
753
711
|
/** Padding (px) around the stroke inside the rendered square canvas. */
|
|
754
|
-
export
|
|
712
|
+
export const STROKE_SNAP_IMAGE_PADDING = 28;
|
|
755
713
|
/**
|
|
756
714
|
* Project a polyline into a `side × side` square, preserving aspect ratio and
|
|
757
715
|
* centering the shorter axis. Pure, so the geometry is testable without a DOM.
|
|
758
716
|
*/
|
|
759
|
-
export function projectStrokeToImagePoints(points, side, padding) {
|
|
760
|
-
if (side === void 0) { side = STROKE_SNAP_IMAGE_SIDE; }
|
|
761
|
-
if (padding === void 0) { padding = STROKE_SNAP_IMAGE_PADDING; }
|
|
717
|
+
export function projectStrokeToImagePoints(points, side = STROKE_SNAP_IMAGE_SIDE, padding = STROKE_SNAP_IMAGE_PADDING) {
|
|
762
718
|
if (points.length < 2)
|
|
763
719
|
return [];
|
|
764
|
-
|
|
720
|
+
const bounds = boundingBox(points);
|
|
765
721
|
if (!Number.isFinite(bounds.width) || !Number.isFinite(bounds.height))
|
|
766
722
|
return [];
|
|
767
723
|
if (bounds.width <= 0 && bounds.height <= 0)
|
|
768
724
|
return [];
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
return points.map(
|
|
725
|
+
const drawable = Math.max(1, side - padding * 2);
|
|
726
|
+
const span = Math.max(bounds.width, bounds.height, 1);
|
|
727
|
+
const scale = drawable / span;
|
|
728
|
+
const offsetX = (side - bounds.width * scale) / 2;
|
|
729
|
+
const offsetY = (side - bounds.height * scale) / 2;
|
|
730
|
+
return points.map((point) => ({
|
|
775
731
|
x: round(offsetX + (point.x - bounds.left) * scale),
|
|
776
732
|
y: round(offsetY + (point.y - bounds.top) * scale)
|
|
777
|
-
})
|
|
733
|
+
}));
|
|
778
734
|
}
|
|
779
735
|
/**
|
|
780
736
|
* Render the user's freehand stroke onto an offscreen 320×320 white canvas as a
|
|
781
737
|
* `data:image/png;base64,...` URL for JEV 1.14 vision evaluation. Returns
|
|
782
738
|
* `undefined` in non-DOM environments or if a 2D context is unavailable.
|
|
783
739
|
*/
|
|
784
|
-
export function renderStrokeSnapImage(points, side) {
|
|
785
|
-
|
|
786
|
-
var projected = projectStrokeToImagePoints(points, side);
|
|
740
|
+
export function renderStrokeSnapImage(points, side = STROKE_SNAP_IMAGE_SIDE) {
|
|
741
|
+
const projected = projectStrokeToImagePoints(points, side);
|
|
787
742
|
if (projected.length < 2)
|
|
788
743
|
return undefined;
|
|
789
744
|
if (typeof document === 'undefined' || typeof document.createElement !== 'function') {
|
|
790
745
|
return undefined;
|
|
791
746
|
}
|
|
792
747
|
try {
|
|
793
|
-
|
|
748
|
+
const offscreen = document.createElement('canvas');
|
|
794
749
|
offscreen.width = side;
|
|
795
750
|
offscreen.height = side;
|
|
796
|
-
|
|
751
|
+
const ctx = offscreen.getContext('2d');
|
|
797
752
|
if (!ctx)
|
|
798
753
|
return undefined;
|
|
799
754
|
ctx.fillStyle = '#ffffff';
|
|
@@ -804,16 +759,16 @@ export function renderStrokeSnapImage(points, side) {
|
|
|
804
759
|
ctx.lineJoin = 'round';
|
|
805
760
|
ctx.beginPath();
|
|
806
761
|
ctx.moveTo(projected[0].x, projected[0].y);
|
|
807
|
-
for (
|
|
762
|
+
for (let i = 1; i < projected.length; i++) {
|
|
808
763
|
ctx.lineTo(projected[i].x, projected[i].y);
|
|
809
764
|
}
|
|
810
765
|
ctx.stroke();
|
|
811
|
-
|
|
766
|
+
const dataUrl = offscreen.toDataURL('image/png');
|
|
812
767
|
return typeof dataUrl === 'string' && dataUrl.startsWith('data:image/png;base64,')
|
|
813
768
|
? dataUrl
|
|
814
769
|
: undefined;
|
|
815
770
|
}
|
|
816
|
-
catch
|
|
771
|
+
catch {
|
|
817
772
|
return undefined;
|
|
818
773
|
}
|
|
819
774
|
}
|