@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,208 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StateNode,
|
|
3
|
+
Vec,
|
|
4
|
+
getEdgeScrollDelta,
|
|
5
|
+
reparentShapeToContainingParent,
|
|
6
|
+
type Editor,
|
|
7
|
+
type PointerEditorEvent,
|
|
8
|
+
type ShapeId,
|
|
9
|
+
type TickEditorEvent,
|
|
10
|
+
} from "@land/canvas-core";
|
|
11
|
+
import {
|
|
12
|
+
applyResolvedArrowTerminalBinding,
|
|
13
|
+
createArrow,
|
|
14
|
+
} from "./arrowTerminalCommands";
|
|
15
|
+
import {
|
|
16
|
+
resolveArrowTerminalBindingAtPoint,
|
|
17
|
+
resolveArrowTerminalBindingAtStartPoint,
|
|
18
|
+
resolveArrowTerminalBindingForArrow,
|
|
19
|
+
} from "./arrowTerminalBindingResolver";
|
|
20
|
+
import { isArrowShapeRecord, type ArrowShapeProps } from "./types";
|
|
21
|
+
|
|
22
|
+
interface CreatingArrowInfo {
|
|
23
|
+
startPagePoint: Vec;
|
|
24
|
+
currentPagePoint: Vec;
|
|
25
|
+
shapeId: ShapeId | null;
|
|
26
|
+
startBinding: ReturnType<typeof resolveArrowTerminalBindingAtPoint>["binding"];
|
|
27
|
+
props: ArrowShapeProps;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ARROW_CREATE_HISTORY_MARK = "arrow.create";
|
|
31
|
+
const DRAG_CREATE_THRESHOLD = 4;
|
|
32
|
+
|
|
33
|
+
export class ArrowTool extends StateNode {
|
|
34
|
+
constructor(editor: Editor) {
|
|
35
|
+
super(editor, "arrow");
|
|
36
|
+
this.initial = "idle";
|
|
37
|
+
this.addChild(new ArrowIdleState(editor, this));
|
|
38
|
+
this.addChild(new CreatingArrowState(editor, this));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class ArrowIdleState extends StateNode {
|
|
43
|
+
constructor(editor: Editor, parent: StateNode) {
|
|
44
|
+
super(editor, "idle", parent);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected onEnter(): void {
|
|
48
|
+
this.editor.clearSelectionTransients();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected onPointerDown(event: PointerEditorEvent): void {
|
|
52
|
+
const defaults = this.editor.getDefaultShapeProps<ArrowShapeProps>("arrow");
|
|
53
|
+
const startBinding = resolveArrowTerminalBindingAtStartPoint(this.editor, {
|
|
54
|
+
terminal: "start",
|
|
55
|
+
rawPagePoint: event.pagePoint,
|
|
56
|
+
ignoreTargets: event.accelKey,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
this.transition("creating", {
|
|
60
|
+
startPagePoint: startBinding.pagePoint,
|
|
61
|
+
currentPagePoint: startBinding.pagePoint.clone(),
|
|
62
|
+
shapeId: null,
|
|
63
|
+
startBinding: startBinding.binding,
|
|
64
|
+
props: defaults,
|
|
65
|
+
} satisfies CreatingArrowInfo);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
class CreatingArrowState extends StateNode {
|
|
70
|
+
private info: CreatingArrowInfo | null = null;
|
|
71
|
+
|
|
72
|
+
constructor(editor: Editor, parent: StateNode) {
|
|
73
|
+
super(editor, "creating", parent);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
protected onEnter(info?: unknown): void {
|
|
77
|
+
this.info = info as CreatingArrowInfo;
|
|
78
|
+
if (!this.info) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.editor.history.mark(ARROW_CREATE_HISTORY_MARK);
|
|
83
|
+
const created = createArrow(
|
|
84
|
+
this.editor,
|
|
85
|
+
{
|
|
86
|
+
startPagePoint: this.info.startPagePoint,
|
|
87
|
+
endPagePoint: this.info.startPagePoint,
|
|
88
|
+
startBinding: this.info.startBinding,
|
|
89
|
+
props: {
|
|
90
|
+
stroke: this.info.props.stroke,
|
|
91
|
+
strokeWidth: this.info.props.strokeWidth,
|
|
92
|
+
headStart: this.info.props.headStart,
|
|
93
|
+
headEnd: this.info.props.headEnd,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
"user",
|
|
97
|
+
);
|
|
98
|
+
this.info.shapeId = created.id;
|
|
99
|
+
|
|
100
|
+
this.editor.commands.setSelectedShapeIds([created.id], {
|
|
101
|
+
source: "system",
|
|
102
|
+
});
|
|
103
|
+
this.editor.setBindingPreview(null);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
protected onPointerMove(event: PointerEditorEvent): void {
|
|
107
|
+
this.updateShape(event.pagePoint);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
protected onTick(event: TickEditorEvent): void {
|
|
111
|
+
if (!this.info) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const inputs = this.editor.inputs.state.get();
|
|
116
|
+
if (
|
|
117
|
+
inputs.dragPhase !== "dragging" ||
|
|
118
|
+
!inputs.currentPagePoint ||
|
|
119
|
+
!inputs.currentScreenPoint
|
|
120
|
+
) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const edgeScrollDelta = getEdgeScrollDelta(
|
|
125
|
+
inputs.currentScreenPoint,
|
|
126
|
+
this.editor.viewportScreenSizeSignal.get(),
|
|
127
|
+
event.elapsed,
|
|
128
|
+
);
|
|
129
|
+
if (edgeScrollDelta.x === 0 && edgeScrollDelta.y === 0) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this.editor.panCamera(edgeScrollDelta);
|
|
134
|
+
this.updateShape(this.editor.screenToPage(inputs.currentScreenPoint));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
protected onPointerUp(event: PointerEditorEvent): void {
|
|
138
|
+
if (!this.info) {
|
|
139
|
+
this.transition("idle");
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
this.updateShape(event.pagePoint);
|
|
144
|
+
|
|
145
|
+
if (
|
|
146
|
+
!this.info.shapeId ||
|
|
147
|
+
this.info.startPagePoint.dist(this.info.currentPagePoint) <
|
|
148
|
+
DRAG_CREATE_THRESHOLD
|
|
149
|
+
) {
|
|
150
|
+
this.editor.history.bailToMark(ARROW_CREATE_HISTORY_MARK);
|
|
151
|
+
this.transition("idle");
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
reparentShapeToContainingParent(this.editor, this.info.shapeId, {
|
|
156
|
+
source: "user",
|
|
157
|
+
});
|
|
158
|
+
this.editor.history.squashToMark(ARROW_CREATE_HISTORY_MARK);
|
|
159
|
+
|
|
160
|
+
if (this.editor.isToolLocked()) {
|
|
161
|
+
this.transition("idle");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.editor.commands.setActiveTool("select", { source: "system" });
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
protected onCancel(): void {
|
|
169
|
+
this.editor.history.bailToMark(ARROW_CREATE_HISTORY_MARK);
|
|
170
|
+
this.transition("idle");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
protected onExit(): void {
|
|
174
|
+
this.info = null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private updateShape(rawPagePoint: Vec): void {
|
|
178
|
+
if (!this.info) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
this.info.currentPagePoint = rawPagePoint.clone();
|
|
183
|
+
const shapeId = this.info.shapeId;
|
|
184
|
+
const arrow = shapeId ? this.editor.getShape(shapeId) : null;
|
|
185
|
+
const endBinding =
|
|
186
|
+
isArrowShapeRecord(arrow)
|
|
187
|
+
? resolveArrowTerminalBindingForArrow(this.editor, {
|
|
188
|
+
arrow,
|
|
189
|
+
terminal: "end",
|
|
190
|
+
rawPagePoint,
|
|
191
|
+
ignoreTargets: this.editor.inputs.state.get().accelKey,
|
|
192
|
+
})
|
|
193
|
+
: resolveArrowTerminalBindingAtPoint(this.editor, {
|
|
194
|
+
terminal: "end",
|
|
195
|
+
rawPagePoint,
|
|
196
|
+
oppositePagePoint: this.info.startPagePoint,
|
|
197
|
+
ignoreTargets: this.editor.inputs.state.get().accelKey,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
applyResolvedArrowTerminalBinding(
|
|
201
|
+
this.editor,
|
|
202
|
+
shapeId,
|
|
203
|
+
"end",
|
|
204
|
+
endBinding,
|
|
205
|
+
"user",
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
import { Box, Vec } from "@land/canvas-core";
|
|
2
|
+
import type { ArrowShapeProps, ArrowShapeRecord, ArrowTerminal } from "./types";
|
|
3
|
+
|
|
4
|
+
const ARROW_CURVE_SAMPLE_COUNT = 24;
|
|
5
|
+
const ARROW_CURVE_EPSILON = 1e-6;
|
|
6
|
+
const MIN_ARROW_AXIS = 1;
|
|
7
|
+
const PI2 = Math.PI * 2;
|
|
8
|
+
|
|
9
|
+
export interface ArrowInfoTerminal {
|
|
10
|
+
terminal: ArrowTerminal;
|
|
11
|
+
point: Vec;
|
|
12
|
+
handle: Vec;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface StraightArrowInfo {
|
|
16
|
+
type: "straight";
|
|
17
|
+
start: ArrowInfoTerminal;
|
|
18
|
+
end: ArrowInfoTerminal;
|
|
19
|
+
middle: Vec;
|
|
20
|
+
arc: null;
|
|
21
|
+
bend: number;
|
|
22
|
+
points: Vec[];
|
|
23
|
+
bounds: Box;
|
|
24
|
+
path: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface CurvedArrowInfo {
|
|
28
|
+
type: "curved";
|
|
29
|
+
start: ArrowInfoTerminal;
|
|
30
|
+
end: ArrowInfoTerminal;
|
|
31
|
+
middle: Vec;
|
|
32
|
+
arc: ArrowArcInfo;
|
|
33
|
+
bend: number;
|
|
34
|
+
points: Vec[];
|
|
35
|
+
bounds: Box;
|
|
36
|
+
path: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type ArrowInfo = StraightArrowInfo | CurvedArrowInfo;
|
|
40
|
+
|
|
41
|
+
export interface ArrowArcInfo {
|
|
42
|
+
center: Vec;
|
|
43
|
+
radius: number;
|
|
44
|
+
largeArcFlag: 0 | 1;
|
|
45
|
+
sweepFlag: 0 | 1;
|
|
46
|
+
size: number;
|
|
47
|
+
length: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function getArrowTerminalPagePoint(
|
|
51
|
+
shape: ArrowShapeRecord,
|
|
52
|
+
terminal: ArrowTerminal,
|
|
53
|
+
): Vec {
|
|
54
|
+
const point = terminal === "start" ? shape.props.start : shape.props.end;
|
|
55
|
+
return transformArrowPoint(shape, new Vec(shape.x + point.x, shape.y + point.y));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getOppositeArrowTerminal(
|
|
59
|
+
terminal: ArrowTerminal,
|
|
60
|
+
): ArrowTerminal {
|
|
61
|
+
return terminal === "start" ? "end" : "start";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function withArrowPagePoints(
|
|
65
|
+
shape: ArrowShapeRecord,
|
|
66
|
+
start: Vec,
|
|
67
|
+
end: Vec,
|
|
68
|
+
): ArrowShapeRecord {
|
|
69
|
+
const normalized = normalizeArrowShapePoints(start, end);
|
|
70
|
+
return {
|
|
71
|
+
...shape,
|
|
72
|
+
x: normalized.x,
|
|
73
|
+
y: normalized.y,
|
|
74
|
+
rotation: 0,
|
|
75
|
+
scaleX: 1,
|
|
76
|
+
scaleY: 1,
|
|
77
|
+
props: {
|
|
78
|
+
...shape.props,
|
|
79
|
+
w: normalized.w,
|
|
80
|
+
h: normalized.h,
|
|
81
|
+
start: normalized.start,
|
|
82
|
+
end: normalized.end,
|
|
83
|
+
bend: shape.props.bend,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function setArrowTerminalPagePoint(
|
|
89
|
+
shape: ArrowShapeRecord,
|
|
90
|
+
terminal: ArrowTerminal,
|
|
91
|
+
pagePoint: Vec,
|
|
92
|
+
): ArrowShapeRecord {
|
|
93
|
+
const start =
|
|
94
|
+
terminal === "start" ? pagePoint : getArrowTerminalPagePoint(shape, "start");
|
|
95
|
+
const end =
|
|
96
|
+
terminal === "end" ? pagePoint : getArrowTerminalPagePoint(shape, "end");
|
|
97
|
+
return withArrowPagePoints(shape, start, end);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function normalizeArrowShapePoints(
|
|
101
|
+
start: Vec,
|
|
102
|
+
end: Vec,
|
|
103
|
+
): Pick<ArrowShapeProps, "w" | "h" | "start" | "end"> & {
|
|
104
|
+
x: number;
|
|
105
|
+
y: number;
|
|
106
|
+
} {
|
|
107
|
+
const normalizedX = normalizeAxis(start.x, end.x);
|
|
108
|
+
const normalizedY = normalizeAxis(start.y, end.y);
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
x: normalizedX.origin,
|
|
112
|
+
y: normalizedY.origin,
|
|
113
|
+
w: normalizedX.size,
|
|
114
|
+
h: normalizedY.size,
|
|
115
|
+
start: {
|
|
116
|
+
x: normalizedX.startOffset,
|
|
117
|
+
y: normalizedY.startOffset,
|
|
118
|
+
},
|
|
119
|
+
end: {
|
|
120
|
+
x: normalizedX.endOffset,
|
|
121
|
+
y: normalizedY.endOffset,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function getArrowInfo(shape: ArrowShapeRecord): ArrowInfo {
|
|
127
|
+
return getArrowInfoFromPoints(
|
|
128
|
+
new Vec(shape.props.start.x, shape.props.start.y).add(
|
|
129
|
+
new Vec(shape.x, shape.y),
|
|
130
|
+
),
|
|
131
|
+
new Vec(shape.props.end.x, shape.props.end.y).add(
|
|
132
|
+
new Vec(shape.x, shape.y),
|
|
133
|
+
),
|
|
134
|
+
shape.props.bend,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function getArrowLocalInfo(shape: ArrowShapeRecord): ArrowInfo {
|
|
139
|
+
return getArrowInfoFromPoints(
|
|
140
|
+
new Vec(shape.props.start.x, shape.props.start.y),
|
|
141
|
+
new Vec(shape.props.end.x, shape.props.end.y),
|
|
142
|
+
shape.props.bend,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function getArrowBendFromMiddlePagePoint(
|
|
147
|
+
start: Vec,
|
|
148
|
+
end: Vec,
|
|
149
|
+
point: Vec,
|
|
150
|
+
): number {
|
|
151
|
+
const middle = Vec.Med(start, end);
|
|
152
|
+
const direction = end.sub(start);
|
|
153
|
+
if (direction.len() < 1e-6) {
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const perpendicular = direction.per().normalize();
|
|
158
|
+
const offset = point.sub(middle);
|
|
159
|
+
return -dot(offset, perpendicular);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function getArrowHitDistance(info: ArrowInfo, point: Vec): number {
|
|
163
|
+
let minDistance = Infinity;
|
|
164
|
+
for (let index = 0; index < info.points.length - 1; index++) {
|
|
165
|
+
minDistance = Math.min(
|
|
166
|
+
minDistance,
|
|
167
|
+
getDistanceToSegment(point, info.points[index]!, info.points[index + 1]!),
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return Number.isFinite(minDistance)
|
|
172
|
+
? minDistance
|
|
173
|
+
: point.dist(info.start.point);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function clampFinite(value: unknown, fallback: number): number {
|
|
177
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function normalizeAxis(
|
|
181
|
+
start: number,
|
|
182
|
+
end: number,
|
|
183
|
+
): {
|
|
184
|
+
origin: number;
|
|
185
|
+
size: number;
|
|
186
|
+
startOffset: number;
|
|
187
|
+
endOffset: number;
|
|
188
|
+
} {
|
|
189
|
+
const min = Math.min(start, end);
|
|
190
|
+
const max = Math.max(start, end);
|
|
191
|
+
if (Math.abs(max - min) < 1e-6) {
|
|
192
|
+
return {
|
|
193
|
+
origin: min - MIN_ARROW_AXIS / 2,
|
|
194
|
+
size: MIN_ARROW_AXIS,
|
|
195
|
+
startOffset: MIN_ARROW_AXIS / 2,
|
|
196
|
+
endOffset: MIN_ARROW_AXIS / 2,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
origin: min,
|
|
202
|
+
size: max - min,
|
|
203
|
+
startOffset: start - min,
|
|
204
|
+
endOffset: end - min,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function transformArrowPoint(shape: ArrowShapeRecord, point: Vec): Vec {
|
|
209
|
+
if (shape.rotation === 0 && shape.scaleX === 1 && shape.scaleY === 1) {
|
|
210
|
+
return point;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const center = new Vec(
|
|
214
|
+
shape.x + shape.props.w / 2,
|
|
215
|
+
shape.y + shape.props.h / 2,
|
|
216
|
+
);
|
|
217
|
+
const offset = point.sub(center);
|
|
218
|
+
return center.add(
|
|
219
|
+
new Vec(offset.x * shape.scaleX, offset.y * shape.scaleY).rot(
|
|
220
|
+
shape.rotation,
|
|
221
|
+
),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function formatPathNumber(value: number): string {
|
|
226
|
+
if (!Number.isFinite(value)) {
|
|
227
|
+
return "0";
|
|
228
|
+
}
|
|
229
|
+
return Number(value.toFixed(3)).toString();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function normalizeArrowBend(value: unknown, fallback = 0): number {
|
|
233
|
+
return clampFinite(value, fallback);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function getArrowInfoFromPoints(
|
|
237
|
+
start: Vec,
|
|
238
|
+
end: Vec,
|
|
239
|
+
rawBend: unknown,
|
|
240
|
+
): ArrowInfo {
|
|
241
|
+
const bend = normalizeArrowBend(rawBend, 0);
|
|
242
|
+
const middle = getArrowMiddlePagePoint(start, end, bend);
|
|
243
|
+
const arc = getArrowArcInfo(start, middle, end);
|
|
244
|
+
const isStraight =
|
|
245
|
+
Math.abs(bend) < ARROW_CURVE_EPSILON ||
|
|
246
|
+
arc === null ||
|
|
247
|
+
arc.length === 0 ||
|
|
248
|
+
!Number.isFinite(arc.length) ||
|
|
249
|
+
!Number.isFinite(arc.radius);
|
|
250
|
+
const points = isStraight
|
|
251
|
+
? [start, end]
|
|
252
|
+
: sampleArcCurve(start, end, arc, ARROW_CURVE_SAMPLE_COUNT);
|
|
253
|
+
const path =
|
|
254
|
+
isStraight || arc === null
|
|
255
|
+
? `M ${formatPathNumber(start.x)} ${formatPathNumber(
|
|
256
|
+
start.y,
|
|
257
|
+
)} L ${formatPathNumber(end.x)} ${formatPathNumber(end.y)}`
|
|
258
|
+
: `M ${formatPathNumber(start.x)} ${formatPathNumber(
|
|
259
|
+
start.y,
|
|
260
|
+
)} A ${formatPathNumber(arc.radius)} ${formatPathNumber(
|
|
261
|
+
arc.radius,
|
|
262
|
+
)} 0 ${arc.largeArcFlag} ${arc.sweepFlag} ${formatPathNumber(
|
|
263
|
+
end.x,
|
|
264
|
+
)} ${formatPathNumber(end.y)}`;
|
|
265
|
+
|
|
266
|
+
const sharedBase = {
|
|
267
|
+
start: {
|
|
268
|
+
terminal: "start" as const,
|
|
269
|
+
point: start,
|
|
270
|
+
handle: start,
|
|
271
|
+
},
|
|
272
|
+
end: {
|
|
273
|
+
terminal: "end" as const,
|
|
274
|
+
point: end,
|
|
275
|
+
handle: end,
|
|
276
|
+
},
|
|
277
|
+
middle,
|
|
278
|
+
bend,
|
|
279
|
+
points,
|
|
280
|
+
bounds: Box.FromPoints(points),
|
|
281
|
+
path,
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
return isStraight
|
|
285
|
+
? {
|
|
286
|
+
type: "straight",
|
|
287
|
+
...sharedBase,
|
|
288
|
+
arc: null,
|
|
289
|
+
}
|
|
290
|
+
: {
|
|
291
|
+
type: "curved",
|
|
292
|
+
...sharedBase,
|
|
293
|
+
arc: arc!,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function getArrowMiddlePagePoint(
|
|
298
|
+
start: Vec,
|
|
299
|
+
end: Vec,
|
|
300
|
+
bend: number,
|
|
301
|
+
): Vec {
|
|
302
|
+
const middle = Vec.Med(start, end);
|
|
303
|
+
const direction = end.sub(start);
|
|
304
|
+
const perpendicular = direction.len()
|
|
305
|
+
? direction.per().normalize()
|
|
306
|
+
: new Vec(0, -1);
|
|
307
|
+
|
|
308
|
+
return middle.add(perpendicular.mul(-bend));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function sampleArcCurve(
|
|
312
|
+
start: Vec,
|
|
313
|
+
end: Vec,
|
|
314
|
+
arc: ArrowArcInfo,
|
|
315
|
+
sampleCount: number,
|
|
316
|
+
): Vec[] {
|
|
317
|
+
const points: Vec[] = [];
|
|
318
|
+
const startAngle = Vec.Angle(arc.center, start);
|
|
319
|
+
const measure = getArcMeasure(
|
|
320
|
+
startAngle,
|
|
321
|
+
Vec.Angle(arc.center, end),
|
|
322
|
+
arc.sweepFlag,
|
|
323
|
+
arc.largeArcFlag,
|
|
324
|
+
);
|
|
325
|
+
for (let index = 0; index <= sampleCount; index++) {
|
|
326
|
+
const t = index / sampleCount;
|
|
327
|
+
points.push(getPointOnCircle(arc.center, arc.radius, startAngle + measure * t));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return points;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function getArrowArcInfo(start: Vec, middle: Vec, end: Vec): ArrowArcInfo | null {
|
|
334
|
+
const center = getCircleCenterFromThreePoints(start, middle, end);
|
|
335
|
+
if (!center) {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const radius = center.dist(start);
|
|
340
|
+
if (radius < ARROW_CURVE_EPSILON) {
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const sweepFlag = isClockwise(start, middle, end) ? 1 : 0;
|
|
345
|
+
const theta = getInscribedArcMeasure(start, middle, end);
|
|
346
|
+
if (!Number.isFinite(theta)) {
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
const largeArcFlag = Math.PI > theta ? 1 : 0;
|
|
350
|
+
const size = (PI2 - theta) * (sweepFlag ? 1 : -1);
|
|
351
|
+
|
|
352
|
+
return {
|
|
353
|
+
center,
|
|
354
|
+
radius,
|
|
355
|
+
largeArcFlag,
|
|
356
|
+
sweepFlag,
|
|
357
|
+
size,
|
|
358
|
+
length: Math.abs(size * radius),
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function getInscribedArcMeasure(start: Vec, middle: Vec, end: Vec): number {
|
|
363
|
+
const ab = start.dist(end);
|
|
364
|
+
const bc = end.dist(middle);
|
|
365
|
+
const ca = middle.dist(start);
|
|
366
|
+
if (bc < ARROW_CURVE_EPSILON || ca < ARROW_CURVE_EPSILON) {
|
|
367
|
+
return 0;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const ratio = (bc * bc + ca * ca - ab * ab) / (2 * bc * ca);
|
|
371
|
+
return Math.acos(Math.max(-1, Math.min(1, ratio))) * 2;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function getCircleCenterFromThreePoints(
|
|
375
|
+
a: Vec,
|
|
376
|
+
b: Vec,
|
|
377
|
+
c: Vec,
|
|
378
|
+
): Vec | null {
|
|
379
|
+
const denominator =
|
|
380
|
+
-2 *
|
|
381
|
+
(a.x * (b.y - c.y) -
|
|
382
|
+
a.y * (b.x - c.x) +
|
|
383
|
+
b.x * c.y -
|
|
384
|
+
c.x * b.y);
|
|
385
|
+
if (Math.abs(denominator) < ARROW_CURVE_EPSILON) {
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const x =
|
|
390
|
+
((a.x * a.x + a.y * a.y) * (c.y - b.y) +
|
|
391
|
+
(b.x * b.x + b.y * b.y) * (a.y - c.y) +
|
|
392
|
+
(c.x * c.x + c.y * c.y) * (b.y - a.y)) /
|
|
393
|
+
denominator;
|
|
394
|
+
const y =
|
|
395
|
+
((a.x * a.x + a.y * a.y) * (b.x - c.x) +
|
|
396
|
+
(b.x * b.x + b.y * b.y) * (c.x - a.x) +
|
|
397
|
+
(c.x * c.x + c.y * c.y) * (a.x - b.x)) /
|
|
398
|
+
denominator;
|
|
399
|
+
|
|
400
|
+
return Number.isFinite(x) && Number.isFinite(y) ? new Vec(x, y) : null;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function getPointOnCircle(center: Vec, radius: number, angle: number): Vec {
|
|
404
|
+
return center.add(new Vec(Math.cos(angle) * radius, Math.sin(angle) * radius));
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function getArcMeasure(
|
|
408
|
+
startAngle: number,
|
|
409
|
+
endAngle: number,
|
|
410
|
+
sweepFlag: 0 | 1,
|
|
411
|
+
largeArcFlag: 0 | 1,
|
|
412
|
+
): number {
|
|
413
|
+
const delta = (endAngle - startAngle) % PI2;
|
|
414
|
+
const shortMeasure = ((2 * delta) % PI2) - delta;
|
|
415
|
+
if (!largeArcFlag) {
|
|
416
|
+
return shortMeasure;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return (PI2 - Math.abs(shortMeasure)) * (sweepFlag ? 1 : -1);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function isClockwise(a: Vec, b: Vec, c: Vec): boolean {
|
|
423
|
+
return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y) < 0;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function getDistanceToSegment(point: Vec, start: Vec, end: Vec): number {
|
|
427
|
+
const segment = end.sub(start);
|
|
428
|
+
const lengthSquared = segment.x * segment.x + segment.y * segment.y;
|
|
429
|
+
if (lengthSquared === 0) {
|
|
430
|
+
return point.dist(start);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const offset = point.sub(start);
|
|
434
|
+
const t = Math.max(0, Math.min(1, dot(offset, segment) / lengthSquared));
|
|
435
|
+
const projection = start.add(segment.mul(t));
|
|
436
|
+
return point.dist(projection);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function dot(a: Vec, b: Vec): number {
|
|
440
|
+
return a.x * b.x + a.y * b.y;
|
|
441
|
+
}
|