@yuzhouu/canvas-kit 0.1.2
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 +145 -0
- package/_vendor/canvas-core/bindings/BindingUtil.ts +92 -0
- package/_vendor/canvas-core/bindings/BindingUtilRegistry.ts +22 -0
- package/_vendor/canvas-core/bindings/arrow/arrowBindingHelpers.ts +52 -0
- package/_vendor/canvas-core/contextMenu.ts +85 -0
- package/_vendor/canvas-core/editor/Editor.ts +2614 -0
- package/_vendor/canvas-core/editor/HistoryManager.ts +83 -0
- package/_vendor/canvas-core/editor/StateNode.ts +162 -0
- package/_vendor/canvas-core/editor/camera.ts +6 -0
- package/_vendor/canvas-core/editor/clipboard.ts +478 -0
- package/_vendor/canvas-core/editor/commands.ts +833 -0
- package/_vendor/canvas-core/editor/edgeScroll.ts +37 -0
- package/_vendor/canvas-core/editor/editorComputedHelpers.ts +257 -0
- package/_vendor/canvas-core/editor/editorSelectionTransforms.ts +116 -0
- package/_vendor/canvas-core/editor/events.ts +93 -0
- package/_vendor/canvas-core/editor/index.ts +12 -0
- package/_vendor/canvas-core/editor/interactions.ts +101 -0
- package/_vendor/canvas-core/editor/internal.ts +22 -0
- package/_vendor/canvas-core/editor/managers/BindingManager.ts +262 -0
- package/_vendor/canvas-core/editor/managers/CameraManager.ts +147 -0
- package/_vendor/canvas-core/editor/managers/ClipboardManager.ts +32 -0
- package/_vendor/canvas-core/editor/managers/DocumentSnapshotManager.ts +256 -0
- package/_vendor/canvas-core/editor/managers/InputsManager.ts +319 -0
- package/_vendor/canvas-core/editor/managers/InteractionTransientManager.ts +218 -0
- package/_vendor/canvas-core/editor/managers/QueryManager.ts +837 -0
- package/_vendor/canvas-core/editor/managers/RenderWindowManager.ts +347 -0
- package/_vendor/canvas-core/editor/managers/SelectionLayoutManager.ts +196 -0
- package/_vendor/canvas-core/editor/managers/SelectionManager.ts +98 -0
- package/_vendor/canvas-core/editor/managers/SelectionOverlayManager.ts +308 -0
- package/_vendor/canvas-core/editor/managers/ShapeEditingManager.ts +226 -0
- package/_vendor/canvas-core/editor/managers/ShapeGeometryManager.ts +162 -0
- package/_vendor/canvas-core/editor/managers/ShapeHierarchyManager.ts +797 -0
- package/_vendor/canvas-core/editor/managers/SideEffectManager.ts +83 -0
- package/_vendor/canvas-core/editor/managers/SnapManager.ts +433 -0
- package/_vendor/canvas-core/editor/managers/SnapSnapshotBuilder.ts +430 -0
- package/_vendor/canvas-core/editor/managers/SpatialIndexManager.ts +354 -0
- package/_vendor/canvas-core/editor/managers/ToolManager.ts +81 -0
- package/_vendor/canvas-core/editor/managers/queryState.ts +583 -0
- package/_vendor/canvas-core/editor/managers/selectionGeometry.ts +287 -0
- package/_vendor/canvas-core/editor/managers/shapeOrder.ts +350 -0
- package/_vendor/canvas-core/editor/managers/shapeReparenting.ts +160 -0
- package/_vendor/canvas-core/editor/managers/snapCalculations.ts +412 -0
- package/_vendor/canvas-core/editor/managers/snapGeometry.ts +202 -0
- package/_vendor/canvas-core/editor/managers/snapIndicators.ts +403 -0
- package/_vendor/canvas-core/editor/managers/snapTypes.ts +122 -0
- package/_vendor/canvas-core/editor/queries/shapeTransforms.ts +171 -0
- package/_vendor/canvas-core/editor/renderWindow.ts +439 -0
- package/_vendor/canvas-core/editor/resizeGeometry.ts +654 -0
- package/_vendor/canvas-core/editor/selection.ts +489 -0
- package/_vendor/canvas-core/editor/selectionHandleInteractions.ts +43 -0
- package/_vendor/canvas-core/editor/selectionOverlay.ts +567 -0
- package/_vendor/canvas-core/exports/integrity.ts +174 -0
- package/_vendor/canvas-core/exports/svg.ts +494 -0
- package/_vendor/canvas-core/geometry/Box.ts +80 -0
- package/_vendor/canvas-core/geometry/Ellipse2d.ts +67 -0
- package/_vendor/canvas-core/geometry/Geometry2d.ts +52 -0
- package/_vendor/canvas-core/geometry/Mat.ts +92 -0
- package/_vendor/canvas-core/geometry/Polyline2d.ts +63 -0
- package/_vendor/canvas-core/geometry/Rectangle2d.ts +64 -0
- package/_vendor/canvas-core/geometry/Vec.ts +123 -0
- package/_vendor/canvas-core/geometry/index.ts +13 -0
- package/_vendor/canvas-core/geometry/segments.ts +65 -0
- package/_vendor/canvas-core/index.ts +391 -0
- package/_vendor/canvas-core/plugins/index.ts +5 -0
- package/_vendor/canvas-core/plugins/types.ts +53 -0
- package/_vendor/canvas-core/reactivity.ts +16 -0
- package/_vendor/canvas-core/schema/records.ts +221 -0
- package/_vendor/canvas-core/schema/snapshots.ts +527 -0
- package/_vendor/canvas-core/shapes/ShapeUtil.ts +516 -0
- package/_vendor/canvas-core/shapes/ShapeUtilRegistry.ts +26 -0
- package/_vendor/canvas-core/shapes/capabilities.ts +67 -0
- package/_vendor/canvas-core/shapes/types.ts +20 -0
- package/_vendor/canvas-core/toolbar.ts +148 -0
- package/_vendor/canvas-core/tools/hand/HandTool.ts +75 -0
- package/_vendor/canvas-core/tools/index.ts +2 -0
- package/_vendor/canvas-core/tools/select/SelectTool.ts +33 -0
- package/_vendor/canvas-core/tools/select/selectContainment.ts +104 -0
- package/_vendor/canvas-core/tools/select/selectToolHelpers.ts +801 -0
- package/_vendor/canvas-core/tools/select/states/BrushingState.ts +145 -0
- package/_vendor/canvas-core/tools/select/states/DraggingSelectionHandleInteractionState.ts +116 -0
- package/_vendor/canvas-core/tools/select/states/IdleState.ts +159 -0
- package/_vendor/canvas-core/tools/select/states/PointingCanvasState.ts +41 -0
- package/_vendor/canvas-core/tools/select/states/PointingResizeHandleState.ts +78 -0
- package/_vendor/canvas-core/tools/select/states/PointingRotateHandleState.ts +64 -0
- package/_vendor/canvas-core/tools/select/states/PointingSelectionHandleInteractionState.ts +34 -0
- package/_vendor/canvas-core/tools/select/states/PointingShapeState.ts +140 -0
- package/_vendor/canvas-core/tools/select/states/ResizingState.ts +171 -0
- package/_vendor/canvas-core/tools/select/states/RotatingState.ts +124 -0
- package/_vendor/canvas-core/tools/select/states/SelectStateNode.ts +128 -0
- package/_vendor/canvas-core/tools/select/states/TranslatingState.ts +139 -0
- package/_vendor/canvas-core/tools/select/states/index.ts +11 -0
- package/_vendor/canvas-plugin-api/feature.ts +204 -0
- package/_vendor/canvas-plugin-api/index.ts +142 -0
- package/_vendor/canvas-product-react/api/apiRegistry.ts +35 -0
- package/_vendor/canvas-product-react/api/canvasProductApi.ts +422 -0
- package/_vendor/canvas-product-react/api/index.ts +31 -0
- package/_vendor/canvas-product-react/capabilities/clipboard/clipboardService.ts +36 -0
- package/_vendor/canvas-product-react/capabilities/clipboard/systemClipboard.ts +245 -0
- package/_vendor/canvas-product-react/capabilities/context-menu/systemContextMenu.ts +396 -0
- package/_vendor/canvas-product-react/capabilities/context-menu/useContextMenuSurface.ts +59 -0
- package/_vendor/canvas-product-react/capabilities/export/useSvgExport.ts +156 -0
- package/_vendor/canvas-product-react/capabilities/history/useHistoryControls.ts +23 -0
- package/_vendor/canvas-product-react/capabilities/pages/usePageControls.ts +41 -0
- package/_vendor/canvas-product-react/capabilities/toolbar/systemToolbar.ts +490 -0
- package/_vendor/canvas-product-react/capabilities/toolbar/useToolbarSurfaces.ts +92 -0
- package/_vendor/canvas-product-react/capabilities/zoom/useZoomControls.ts +27 -0
- package/_vendor/canvas-product-react/index.ts +84 -0
- package/_vendor/canvas-product-react/persistence/index.ts +18 -0
- package/_vendor/canvas-product-react/persistence/localSnapshotPersistence.ts +294 -0
- package/_vendor/canvas-product-react/runtime/CanvasProductRoot.tsx +250 -0
- package/_vendor/canvas-product-react/runtime/productDefinition.ts +72 -0
- package/_vendor/canvas-product-react/runtime/productServices.ts +96 -0
- package/_vendor/canvas-react/components/CanvasView.tsx +796 -0
- package/_vendor/canvas-react/components/OverlayLayer.tsx +879 -0
- package/_vendor/canvas-react/components/PageSpace.tsx +34 -0
- package/_vendor/canvas-react/components/ShapeLayer.tsx +428 -0
- package/_vendor/canvas-react/components/index.ts +4 -0
- package/_vendor/canvas-react/hooks/useEditor.ts +11 -0
- package/_vendor/canvas-react/hooks/useValue.ts +14 -0
- package/_vendor/canvas-react/host/CanvasRendererRegistryContext.tsx +14 -0
- package/_vendor/canvas-react/host/contextMenuRegistry.ts +61 -0
- package/_vendor/canvas-react/host/domEventGuards.ts +61 -0
- package/_vendor/canvas-react/host/index.ts +38 -0
- package/_vendor/canvas-react/host/pluginHost.ts +635 -0
- package/_vendor/canvas-react/host/rendererRegistry.tsx +58 -0
- package/_vendor/canvas-react/host/toolbarRegistry.ts +100 -0
- package/_vendor/canvas-react/index.ts +3 -0
- package/_vendor/plugin-arrow/ArrowBindingUtil.ts +239 -0
- package/_vendor/plugin-arrow/ArrowShapeUtil.ts +311 -0
- package/_vendor/plugin-arrow/ArrowTool.ts +208 -0
- package/_vendor/plugin-arrow/arrowGeometry.ts +441 -0
- package/_vendor/plugin-arrow/arrowTerminalBindingResolver.ts +253 -0
- package/_vendor/plugin-arrow/arrowTerminalCommands.ts +230 -0
- package/_vendor/plugin-arrow/canvas.tsx +92 -0
- package/_vendor/plugin-arrow/core.ts +409 -0
- package/_vendor/plugin-arrow/feature.ts +20 -0
- package/_vendor/plugin-arrow/index.ts +28 -0
- package/_vendor/plugin-arrow/selectionHandleInteractions.ts +154 -0
- package/_vendor/plugin-arrow/types.ts +63 -0
- package/_vendor/plugin-draw/DrawShapeRenderer.tsx +60 -0
- package/_vendor/plugin-draw/DrawShapeUtil.ts +372 -0
- package/_vendor/plugin-draw/DrawTool.ts +444 -0
- package/_vendor/plugin-draw/canvas.tsx +24 -0
- package/_vendor/plugin-draw/core.ts +515 -0
- package/_vendor/plugin-draw/drawGeometry.ts +491 -0
- package/_vendor/plugin-draw/drawHitTest.ts +271 -0
- package/_vendor/plugin-draw/drawInterpolation.ts +250 -0
- package/_vendor/plugin-draw/drawPath.ts +550 -0
- package/_vendor/plugin-draw/drawSegments.ts +106 -0
- package/_vendor/plugin-draw/drawSelectionTransform.ts +67 -0
- package/_vendor/plugin-draw/drawShapeGeometry.ts +307 -0
- package/_vendor/plugin-draw/drawStrokeGeometry.ts +454 -0
- package/_vendor/plugin-draw/feature.ts +20 -0
- package/_vendor/plugin-draw/index.ts +31 -0
- package/_vendor/plugin-draw/types.ts +36 -0
- package/_vendor/plugin-eraser/EraserTool.ts +297 -0
- package/_vendor/plugin-eraser/canvas.tsx +616 -0
- package/_vendor/plugin-eraser/core.ts +49 -0
- package/_vendor/plugin-eraser/feature.ts +20 -0
- package/_vendor/plugin-eraser/index.ts +4 -0
- package/_vendor/plugin-frame/FrameShapeUtil.ts +173 -0
- package/_vendor/plugin-frame/FrameTool.ts +225 -0
- package/_vendor/plugin-frame/canvas.tsx +75 -0
- package/_vendor/plugin-frame/core.ts +534 -0
- package/_vendor/plugin-frame/feature.ts +20 -0
- package/_vendor/plugin-frame/index.ts +6 -0
- package/_vendor/plugin-geo/GeoShapeUtil.ts +463 -0
- package/_vendor/plugin-geo/GeoTools.ts +247 -0
- package/_vendor/plugin-geo/canvas.tsx +90 -0
- package/_vendor/plugin-geo/core.ts +495 -0
- package/_vendor/plugin-geo/feature.ts +20 -0
- package/_vendor/plugin-geo/index.ts +7 -0
- package/_vendor/plugin-geo/types.ts +27 -0
- package/_vendor/plugin-group/GroupShapeUtil.ts +59 -0
- package/_vendor/plugin-group/canvas.tsx +59 -0
- package/_vendor/plugin-group/core.ts +40 -0
- package/_vendor/plugin-group/feature.ts +20 -0
- package/_vendor/plugin-group/index.ts +5 -0
- package/_vendor/plugin-image/ImageShapeUtil.ts +95 -0
- package/_vendor/plugin-image/canvas.tsx +212 -0
- package/_vendor/plugin-image/core.ts +542 -0
- package/_vendor/plugin-image/crop.ts +770 -0
- package/_vendor/plugin-image/cropInteractions.ts +350 -0
- package/_vendor/plugin-image/cropSession.ts +38 -0
- package/_vendor/plugin-image/feature.ts +20 -0
- package/_vendor/plugin-image/index.ts +29 -0
- package/_vendor/plugin-image/types.ts +55 -0
- package/_vendor/plugin-image-product-react/ImageImportController.tsx +143 -0
- package/_vendor/plugin-image-product-react/imageAssetService.ts +52 -0
- package/_vendor/plugin-image-product-react/imageBlobStore.ts +315 -0
- package/_vendor/plugin-image-product-react/imageHostActions.ts +438 -0
- package/_vendor/plugin-image-product-react/imageProductApi.ts +88 -0
- package/_vendor/plugin-image-product-react/imageToolbarHostBridge.ts +48 -0
- package/_vendor/plugin-image-product-react/imageToolbarRuntime.ts +21 -0
- package/_vendor/plugin-image-product-react/index.ts +60 -0
- package/_vendor/plugin-image-product-react/snapshotGuards.ts +33 -0
- package/_vendor/plugin-image-product-react/svgImageAssetInliner.ts +54 -0
- package/_vendor/plugin-image-product-react/useImageImport.ts +64 -0
- package/_vendor/plugin-laser/LaserTool.ts +154 -0
- package/_vendor/plugin-laser/canvas.tsx +645 -0
- package/_vendor/plugin-laser/core.ts +49 -0
- package/_vendor/plugin-laser/feature.ts +20 -0
- package/_vendor/plugin-laser/index.ts +4 -0
- package/_vendor/plugin-text/ArrowLabelBindingUtil.ts +336 -0
- package/_vendor/plugin-text/GeoLabelBindingUtil.ts +391 -0
- package/_vendor/plugin-text/TextShapeSvgExporter.ts +101 -0
- package/_vendor/plugin-text/TextShapeUtil.ts +284 -0
- package/_vendor/plugin-text/TextTool.ts +228 -0
- package/_vendor/plugin-text/canvas.tsx +115 -0
- package/_vendor/plugin-text/core.ts +422 -0
- package/_vendor/plugin-text/feature.ts +20 -0
- package/_vendor/plugin-text/host/RichTextEditingBox.tsx +144 -0
- package/_vendor/plugin-text/host/RichTextToolbar.tsx +266 -0
- package/_vendor/plugin-text/host/TiptapTextEditor.tsx +191 -0
- package/_vendor/plugin-text/host/richTextRendering.tsx +49 -0
- package/_vendor/plugin-text/host/textDomSelection.ts +93 -0
- package/_vendor/plugin-text/host/textEditingBoxStyle.ts +36 -0
- package/_vendor/plugin-text/host/textEntryFocus.ts +307 -0
- package/_vendor/plugin-text/host/textFontLoader.ts +149 -0
- package/_vendor/plugin-text/host/textMeasurement.ts +258 -0
- package/_vendor/plugin-text/host/textMeasurementSync.ts +77 -0
- package/_vendor/plugin-text/host/textRangeSelection.ts +134 -0
- package/_vendor/plugin-text/host/textShapeRendererRuntime.tsx +89 -0
- package/_vendor/plugin-text/host/textShapeSync.ts +173 -0
- package/_vendor/plugin-text/host/tiptapTextEditorController.ts +148 -0
- package/_vendor/plugin-text/index.ts +18 -0
- package/_vendor/plugin-text/richTextUtils.ts +259 -0
- package/_vendor/plugin-text/textInteractions.ts +366 -0
- package/_vendor/plugin-text/textLabelHelpers.ts +235 -0
- package/_vendor/plugin-text/textShapeHelpers.ts +123 -0
- package/_vendor/plugin-text/textStyle.ts +96 -0
- package/_vendor/plugin-text/textTypes.ts +48 -0
- package/_vendor/signals/atom.ts +74 -0
- package/_vendor/signals/computed.ts +132 -0
- package/_vendor/signals/core.ts +130 -0
- package/_vendor/signals/effect.ts +117 -0
- package/_vendor/signals/index.ts +9 -0
- package/_vendor/signals/types.ts +17 -0
- package/_vendor/store/Store.ts +171 -0
- package/_vendor/store/diff.ts +229 -0
- package/_vendor/store/history.ts +177 -0
- package/_vendor/store/index.ts +32 -0
- package/_vendor/store/types.ts +37 -0
- package/api/canvasKitApi.d.ts +29 -0
- package/index.css +3 -0
- package/index.d.ts +16 -0
- package/index.mjs +1 -0
- package/internal-modules.d.ts +187 -0
- package/package.json +48 -0
- package/product/Shell.d.ts +19 -0
- package/product/chromeContext.d.ts +70 -0
- package/product/createCanvasKitEditor.d.ts +6 -0
- package/product/index.d.ts +7 -0
- package/product/persistence.d.ts +3 -0
- package/product/product.d.ts +22 -0
- package/product/useCanvasClipboardShortcuts.d.ts +6 -0
- package/product-iyBbEPIs.mjs +4 -0
- package/product.mjs +1 -0
- package/standard/extensions.d.ts +1 -0
- package/standard/index.d.ts +3 -0
- package/standard/standardApi.d.ts +91 -0
- package/standard-BmrxwqpJ.mjs +44 -0
- package/standard.mjs +1 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { Vec } from "@land/canvas-core";
|
|
2
|
+
import { drawPointToVec } from "./drawPath";
|
|
3
|
+
import type { DrawShapeGeometry } from "./drawShapeGeometry";
|
|
4
|
+
import type { DrawShapePoint } from "./types";
|
|
5
|
+
|
|
6
|
+
interface DrawShapeHitTestOptions {
|
|
7
|
+
fill?: string;
|
|
8
|
+
isClosed?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const HIT_TEST_MARGIN = 4;
|
|
12
|
+
|
|
13
|
+
function distanceToSegment(point: Vec, start: Vec, end: Vec): number {
|
|
14
|
+
const delta = end.sub(start);
|
|
15
|
+
const lengthSquared = delta.len2();
|
|
16
|
+
if (lengthSquared === 0) {
|
|
17
|
+
return point.dist(start);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const t = Math.max(
|
|
21
|
+
0,
|
|
22
|
+
Math.min(
|
|
23
|
+
1,
|
|
24
|
+
((point.x - start.x) * delta.x + (point.y - start.y) * delta.y) /
|
|
25
|
+
lengthSquared,
|
|
26
|
+
),
|
|
27
|
+
);
|
|
28
|
+
const projection = new Vec(start.x + delta.x * t, start.y + delta.y * t);
|
|
29
|
+
return point.dist(projection);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function distanceBetweenSegments(
|
|
33
|
+
firstStart: Vec,
|
|
34
|
+
firstEnd: Vec,
|
|
35
|
+
secondStart: Vec,
|
|
36
|
+
secondEnd: Vec,
|
|
37
|
+
): number {
|
|
38
|
+
if (doSegmentsIntersect(firstStart, firstEnd, secondStart, secondEnd)) {
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return Math.min(
|
|
43
|
+
distanceToSegment(firstStart, secondStart, secondEnd),
|
|
44
|
+
distanceToSegment(firstEnd, secondStart, secondEnd),
|
|
45
|
+
distanceToSegment(secondStart, firstStart, firstEnd),
|
|
46
|
+
distanceToSegment(secondEnd, firstStart, firstEnd),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function doSegmentsIntersect(
|
|
51
|
+
firstStart: Vec,
|
|
52
|
+
firstEnd: Vec,
|
|
53
|
+
secondStart: Vec,
|
|
54
|
+
secondEnd: Vec,
|
|
55
|
+
): boolean {
|
|
56
|
+
const first = firstEnd.sub(firstStart);
|
|
57
|
+
const second = secondEnd.sub(secondStart);
|
|
58
|
+
const denominator = first.x * second.y - first.y * second.x;
|
|
59
|
+
if (Math.abs(denominator) < 1e-8) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const delta = secondStart.sub(firstStart);
|
|
64
|
+
const t = (delta.x * second.y - delta.y * second.x) / denominator;
|
|
65
|
+
const u = (delta.x * first.y - delta.y * first.x) / denominator;
|
|
66
|
+
|
|
67
|
+
return t >= 0 && t <= 1 && u >= 0 && u <= 1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function isPointInPolygon(point: Vec, polygon: readonly Vec[]): boolean {
|
|
71
|
+
let inside = false;
|
|
72
|
+
|
|
73
|
+
for (
|
|
74
|
+
let index = 0, previousIndex = polygon.length - 1;
|
|
75
|
+
index < polygon.length;
|
|
76
|
+
previousIndex = index, index += 1
|
|
77
|
+
) {
|
|
78
|
+
const current = polygon[index];
|
|
79
|
+
const previous = polygon[previousIndex];
|
|
80
|
+
if (!current || !previous) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const intersects =
|
|
85
|
+
current.y > point.y !== previous.y > point.y &&
|
|
86
|
+
point.x <
|
|
87
|
+
((previous.x - current.x) * (point.y - current.y)) /
|
|
88
|
+
(previous.y - current.y) +
|
|
89
|
+
current.x;
|
|
90
|
+
|
|
91
|
+
if (intersects) {
|
|
92
|
+
inside = !inside;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return inside;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function distanceToClosedPolygon(point: Vec, polygon: readonly Vec[]): number {
|
|
100
|
+
if (polygon.length === 0) {
|
|
101
|
+
return Number.POSITIVE_INFINITY;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let nearestDistance = Number.POSITIVE_INFINITY;
|
|
105
|
+
for (let index = 0; index < polygon.length; index += 1) {
|
|
106
|
+
const start = polygon[index];
|
|
107
|
+
const end = polygon[(index + 1) % polygon.length];
|
|
108
|
+
if (!start || !end) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
nearestDistance = Math.min(
|
|
112
|
+
nearestDistance,
|
|
113
|
+
distanceToSegment(point, start, end),
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return nearestDistance;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function doesSegmentHitClosedPolygon(
|
|
121
|
+
start: Vec,
|
|
122
|
+
end: Vec,
|
|
123
|
+
polygon: readonly Vec[],
|
|
124
|
+
margin: number,
|
|
125
|
+
): boolean {
|
|
126
|
+
if (polygon.length === 0) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
if (isPointInPolygon(start, polygon) || isPointInPolygon(end, polygon)) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
for (let index = 0; index < polygon.length; index += 1) {
|
|
134
|
+
const edgeStart = polygon[index];
|
|
135
|
+
const edgeEnd = polygon[(index + 1) % polygon.length];
|
|
136
|
+
if (!edgeStart || !edgeEnd) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (distanceBetweenSegments(start, end, edgeStart, edgeEnd) <= margin) {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function distanceToDrawCenterline(
|
|
148
|
+
point: Vec,
|
|
149
|
+
points: readonly DrawShapePoint[],
|
|
150
|
+
isClosed: boolean,
|
|
151
|
+
): number {
|
|
152
|
+
if (points.length === 0) {
|
|
153
|
+
return Number.POSITIVE_INFINITY;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (points.length === 1) {
|
|
157
|
+
return point.dist(drawPointToVec(points[0]!));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let nearestDistance = Number.POSITIVE_INFINITY;
|
|
161
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
162
|
+
const start = points[index - 1];
|
|
163
|
+
const end = points[index];
|
|
164
|
+
if (!start || !end) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
nearestDistance = Math.min(
|
|
168
|
+
nearestDistance,
|
|
169
|
+
distanceToSegment(point, drawPointToVec(start), drawPointToVec(end)),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (isClosed && points.length >= 3) {
|
|
174
|
+
nearestDistance = Math.min(
|
|
175
|
+
nearestDistance,
|
|
176
|
+
distanceToSegment(
|
|
177
|
+
point,
|
|
178
|
+
drawPointToVec(points[points.length - 1]!),
|
|
179
|
+
drawPointToVec(points[0]!),
|
|
180
|
+
),
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return nearestDistance;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function hitTestDrawShapeGeometry(
|
|
188
|
+
geometry: Pick<
|
|
189
|
+
DrawShapeGeometry,
|
|
190
|
+
"outlinePolygons" | "outlineVertices" | "normalizedPoints"
|
|
191
|
+
>,
|
|
192
|
+
strokeWidth: number,
|
|
193
|
+
point: Vec,
|
|
194
|
+
options: DrawShapeHitTestOptions = {},
|
|
195
|
+
): boolean {
|
|
196
|
+
if (
|
|
197
|
+
options.isClosed &&
|
|
198
|
+
options.fill !== undefined &&
|
|
199
|
+
options.fill !== "transparent" &&
|
|
200
|
+
geometry.normalizedPoints.length >= 3 &&
|
|
201
|
+
isPointInPolygon(
|
|
202
|
+
point,
|
|
203
|
+
geometry.normalizedPoints.map(drawPointToVec),
|
|
204
|
+
)
|
|
205
|
+
) {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (
|
|
210
|
+
options.isClosed &&
|
|
211
|
+
distanceToDrawCenterline(point, geometry.normalizedPoints, true) <=
|
|
212
|
+
Math.max(HIT_TEST_MARGIN, strokeWidth * 0.5)
|
|
213
|
+
) {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (geometry.outlineVertices.length === 0) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (const polygon of geometry.outlinePolygons) {
|
|
222
|
+
if (isPointInPolygon(point, polygon)) {
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (
|
|
227
|
+
distanceToClosedPolygon(point, polygon) <=
|
|
228
|
+
Math.max(HIT_TEST_MARGIN, strokeWidth * 0.5)
|
|
229
|
+
) {
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function hitTestDrawShapeLineSegmentGeometry(
|
|
238
|
+
geometry: Pick<
|
|
239
|
+
DrawShapeGeometry,
|
|
240
|
+
"outlinePolygons" | "outlineVertices" | "normalizedPoints"
|
|
241
|
+
>,
|
|
242
|
+
strokeWidth: number,
|
|
243
|
+
start: Vec,
|
|
244
|
+
end: Vec,
|
|
245
|
+
options: DrawShapeHitTestOptions & { margin?: number } = {},
|
|
246
|
+
): boolean {
|
|
247
|
+
const margin = Math.max(HIT_TEST_MARGIN, strokeWidth * 0.5, options.margin ?? 0);
|
|
248
|
+
|
|
249
|
+
if (
|
|
250
|
+
options.isClosed &&
|
|
251
|
+
options.fill !== undefined &&
|
|
252
|
+
options.fill !== "transparent" &&
|
|
253
|
+
geometry.normalizedPoints.length >= 3 &&
|
|
254
|
+
doesSegmentHitClosedPolygon(
|
|
255
|
+
start,
|
|
256
|
+
end,
|
|
257
|
+
geometry.normalizedPoints.map(drawPointToVec),
|
|
258
|
+
margin,
|
|
259
|
+
)
|
|
260
|
+
) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (geometry.outlineVertices.length === 0) {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return geometry.outlinePolygons.some((polygon) =>
|
|
269
|
+
doesSegmentHitClosedPolygon(start, end, polygon, margin),
|
|
270
|
+
);
|
|
271
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { Vec } from "@land/canvas-core";
|
|
2
|
+
import { cloneDrawPoint, drawPointToVec } from "./drawPath";
|
|
3
|
+
import { cloneDrawSegments } from "./drawSegments";
|
|
4
|
+
import {
|
|
5
|
+
getDrawShapePagePoints,
|
|
6
|
+
normalizeDrawShapeFromPagePoints,
|
|
7
|
+
} from "./drawGeometry";
|
|
8
|
+
import type { DrawShapePoint, DrawShapeRecord } from "./types";
|
|
9
|
+
|
|
10
|
+
export interface DrawShapeInterpolationOptions {
|
|
11
|
+
pointCount?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface DrawInterpolationSample {
|
|
15
|
+
point: DrawShapePoint;
|
|
16
|
+
segmentWeight: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface DrawInterpolationSegment {
|
|
20
|
+
start: DrawShapePoint;
|
|
21
|
+
end: DrawShapePoint;
|
|
22
|
+
length: number;
|
|
23
|
+
segmentWeight: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function clamp(value: number, min: number, max: number): number {
|
|
27
|
+
return Math.max(min, Math.min(max, value));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function lerp(start: number, end: number, t: number): number {
|
|
31
|
+
return start + (end - start) * t;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function lerpOptionalNumber(
|
|
35
|
+
start: number | undefined,
|
|
36
|
+
end: number | undefined,
|
|
37
|
+
t: number,
|
|
38
|
+
): number | undefined {
|
|
39
|
+
if (start === undefined && end === undefined) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const startValue = start ?? end ?? 0;
|
|
44
|
+
const endValue = end ?? start ?? 0;
|
|
45
|
+
return lerp(startValue, endValue, t);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getDrawInterpolationPointCount(
|
|
49
|
+
fromPointCount: number,
|
|
50
|
+
toPointCount: number,
|
|
51
|
+
requestedPointCount?: number,
|
|
52
|
+
): number {
|
|
53
|
+
if (
|
|
54
|
+
typeof requestedPointCount === "number" &&
|
|
55
|
+
Number.isFinite(requestedPointCount)
|
|
56
|
+
) {
|
|
57
|
+
return Math.max(2, Math.round(requestedPointCount));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return Math.max(2, fromPointCount, toPointCount, 12);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function getDrawInterpolationSegments(
|
|
64
|
+
points: readonly DrawShapePoint[],
|
|
65
|
+
isClosed: boolean,
|
|
66
|
+
): DrawInterpolationSegment[] {
|
|
67
|
+
const segments: DrawInterpolationSegment[] = [];
|
|
68
|
+
|
|
69
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
70
|
+
const start = points[index - 1];
|
|
71
|
+
const end = points[index];
|
|
72
|
+
if (!start || !end) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
segments.push({
|
|
77
|
+
start,
|
|
78
|
+
end,
|
|
79
|
+
length: drawPointToVec(start).dist(drawPointToVec(end)),
|
|
80
|
+
segmentWeight: end.segment === "straight" ? 1 : 0,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (isClosed && points.length >= 3) {
|
|
85
|
+
const start = points[points.length - 1];
|
|
86
|
+
const end = points[0];
|
|
87
|
+
if (start && end) {
|
|
88
|
+
segments.push({
|
|
89
|
+
start,
|
|
90
|
+
end,
|
|
91
|
+
length: drawPointToVec(start).dist(drawPointToVec(end)),
|
|
92
|
+
segmentWeight: 1,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return segments;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function resampleDrawShapePoints(
|
|
101
|
+
points: readonly DrawShapePoint[],
|
|
102
|
+
sampleCount: number,
|
|
103
|
+
isClosed: boolean,
|
|
104
|
+
): DrawInterpolationSample[] {
|
|
105
|
+
const clampedSampleCount = Math.max(2, Math.round(sampleCount));
|
|
106
|
+
|
|
107
|
+
if (points.length === 0) {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (points.length === 1) {
|
|
112
|
+
return Array.from({ length: clampedSampleCount }, () => ({
|
|
113
|
+
point: cloneDrawPoint(points[0]!),
|
|
114
|
+
segmentWeight: 0,
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const segments = getDrawInterpolationSegments(points, isClosed);
|
|
119
|
+
const totalLength = segments.reduce((sum, segment) => sum + segment.length, 0);
|
|
120
|
+
|
|
121
|
+
if (segments.length === 0 || totalLength <= 1e-6) {
|
|
122
|
+
return Array.from({ length: clampedSampleCount }, () => ({
|
|
123
|
+
point: cloneDrawPoint(points[0]!),
|
|
124
|
+
segmentWeight: 0,
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return Array.from({ length: clampedSampleCount }, (_, sampleIndex) => {
|
|
129
|
+
const distance =
|
|
130
|
+
isClosed
|
|
131
|
+
? (sampleIndex / clampedSampleCount) * totalLength
|
|
132
|
+
: (sampleIndex / Math.max(clampedSampleCount - 1, 1)) * totalLength;
|
|
133
|
+
let remainingDistance = distance;
|
|
134
|
+
let segment = segments[segments.length - 1]!;
|
|
135
|
+
|
|
136
|
+
for (const candidate of segments) {
|
|
137
|
+
if (remainingDistance <= candidate.length || candidate === segment) {
|
|
138
|
+
segment = candidate;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
remainingDistance -= candidate.length;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const segmentT =
|
|
145
|
+
segment.length <= 1e-6 ? 0 : clamp(remainingDistance / segment.length, 0, 1);
|
|
146
|
+
const start = drawPointToVec(segment.start);
|
|
147
|
+
const end = drawPointToVec(segment.end);
|
|
148
|
+
const point = Vec.Lrp(start, end, segmentT);
|
|
149
|
+
const pressure = lerpOptionalNumber(
|
|
150
|
+
segment.start.pressure,
|
|
151
|
+
segment.end.pressure,
|
|
152
|
+
segmentT,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
point: {
|
|
157
|
+
x: point.x,
|
|
158
|
+
y: point.y,
|
|
159
|
+
...(pressure === undefined ? null : { pressure }),
|
|
160
|
+
},
|
|
161
|
+
segmentWeight: segment.segmentWeight,
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function interpolateDrawShapeRecords(
|
|
167
|
+
from: DrawShapeRecord,
|
|
168
|
+
to: DrawShapeRecord,
|
|
169
|
+
t: number,
|
|
170
|
+
options: DrawShapeInterpolationOptions = {},
|
|
171
|
+
): DrawShapeRecord {
|
|
172
|
+
const clampedT = clamp(t, 0, 1);
|
|
173
|
+
const interpolateAsClosed = from.props.isClosed && to.props.isClosed;
|
|
174
|
+
const fromPagePoints = getDrawShapePagePoints(from);
|
|
175
|
+
const toPagePoints = getDrawShapePagePoints(to);
|
|
176
|
+
const sampleCount = getDrawInterpolationPointCount(
|
|
177
|
+
fromPagePoints.length,
|
|
178
|
+
toPagePoints.length,
|
|
179
|
+
options.pointCount,
|
|
180
|
+
);
|
|
181
|
+
const fromSamples = resampleDrawShapePoints(
|
|
182
|
+
fromPagePoints,
|
|
183
|
+
sampleCount,
|
|
184
|
+
interpolateAsClosed,
|
|
185
|
+
);
|
|
186
|
+
const toSamples = resampleDrawShapePoints(
|
|
187
|
+
toPagePoints,
|
|
188
|
+
sampleCount,
|
|
189
|
+
interpolateAsClosed,
|
|
190
|
+
);
|
|
191
|
+
const interpolatedPagePoints = fromSamples.map((sample, index) => {
|
|
192
|
+
const target = toSamples[index] ?? toSamples[toSamples.length - 1] ?? sample;
|
|
193
|
+
const point = Vec.Lrp(
|
|
194
|
+
drawPointToVec(sample.point),
|
|
195
|
+
drawPointToVec(target.point),
|
|
196
|
+
clampedT,
|
|
197
|
+
);
|
|
198
|
+
const pressure = lerpOptionalNumber(
|
|
199
|
+
sample.point.pressure,
|
|
200
|
+
target.point.pressure,
|
|
201
|
+
clampedT,
|
|
202
|
+
);
|
|
203
|
+
const segmentWeight = lerp(
|
|
204
|
+
sample.segmentWeight,
|
|
205
|
+
target.segmentWeight,
|
|
206
|
+
clampedT,
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
x: point.x,
|
|
211
|
+
y: point.y,
|
|
212
|
+
...(pressure === undefined ? null : { pressure }),
|
|
213
|
+
...(index > 0 && segmentWeight >= 0.5
|
|
214
|
+
? { segment: "straight" as const }
|
|
215
|
+
: null),
|
|
216
|
+
};
|
|
217
|
+
});
|
|
218
|
+
const nextIsClosed = clampedT < 0.5 ? from.props.isClosed : to.props.isClosed;
|
|
219
|
+
const nextIsPen =
|
|
220
|
+
clampedT < 0.5 ? (from.props.isPen ?? false) : (to.props.isPen ?? false);
|
|
221
|
+
const nextStrokeWidth = Math.max(
|
|
222
|
+
1,
|
|
223
|
+
lerp(from.props.strokeWidth, to.props.strokeWidth, clampedT),
|
|
224
|
+
);
|
|
225
|
+
const normalized = normalizeDrawShapeFromPagePoints(
|
|
226
|
+
interpolatedPagePoints,
|
|
227
|
+
nextStrokeWidth,
|
|
228
|
+
nextIsPen,
|
|
229
|
+
nextIsClosed,
|
|
230
|
+
);
|
|
231
|
+
const styleSource = clampedT < 0.5 ? from : to;
|
|
232
|
+
|
|
233
|
+
return {
|
|
234
|
+
...styleSource,
|
|
235
|
+
id: from.id,
|
|
236
|
+
x: normalized.x,
|
|
237
|
+
y: normalized.y,
|
|
238
|
+
rotation: 0,
|
|
239
|
+
scaleX: 1,
|
|
240
|
+
scaleY: 1,
|
|
241
|
+
opacity: lerp(from.opacity, to.opacity, clampedT),
|
|
242
|
+
props: {
|
|
243
|
+
...styleSource.props,
|
|
244
|
+
segments: cloneDrawSegments(normalized.segments),
|
|
245
|
+
strokeWidth: nextStrokeWidth,
|
|
246
|
+
isClosed: nextIsClosed,
|
|
247
|
+
isPen: nextIsPen,
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|