@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,454 @@
|
|
|
1
|
+
import { Vec } from "@land/canvas-core";
|
|
2
|
+
import { drawPointToVec } from "./drawPath";
|
|
3
|
+
import type { DrawShapePoint } from "./types";
|
|
4
|
+
|
|
5
|
+
const HALF_CAP_SEGMENTS = 8;
|
|
6
|
+
const DOT_SEGMENTS = 16;
|
|
7
|
+
const MIN_RADIUS = 0.6;
|
|
8
|
+
const SIMULATED_PRESSURE_CHANGE_RATE = 0.275;
|
|
9
|
+
const SIMULATED_PRESSURE_INITIAL = 0.5;
|
|
10
|
+
const SIMULATED_PRESSURE_MIN = 0.08;
|
|
11
|
+
const SIMULATED_PRESSURE_DISTANCE_SCALE = 1.5;
|
|
12
|
+
const VISUAL_RADIUS_OFFSET = 1;
|
|
13
|
+
const OUTLINE_VERTEX_MIN_DISTANCE_FACTOR = 0.38;
|
|
14
|
+
|
|
15
|
+
export interface DrawStrokeDerivedPoint {
|
|
16
|
+
point: Vec;
|
|
17
|
+
direction: Vec;
|
|
18
|
+
pressure: number;
|
|
19
|
+
radius: number;
|
|
20
|
+
runningLength: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface DrawStrokeStyleOptions {
|
|
24
|
+
baseRadius: number;
|
|
25
|
+
minRadius: number;
|
|
26
|
+
minRadiusScale: number;
|
|
27
|
+
maxRadiusScale: number;
|
|
28
|
+
startTaper: number;
|
|
29
|
+
endTaper: number;
|
|
30
|
+
simulatePressure: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function clamp(value: number, min: number, max: number): number {
|
|
34
|
+
return Math.max(min, Math.min(max, value));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function lerp(start: number, end: number, t: number): number {
|
|
38
|
+
return start + (end - start) * t;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getDrawStrokeStyleOptions(
|
|
42
|
+
strokeWidth: number,
|
|
43
|
+
isPen: boolean,
|
|
44
|
+
): DrawStrokeStyleOptions {
|
|
45
|
+
const baseRadius = Math.max(strokeWidth + VISUAL_RADIUS_OFFSET, 1.6);
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
baseRadius,
|
|
49
|
+
minRadius: Math.max(MIN_RADIUS, baseRadius * 0.32),
|
|
50
|
+
minRadiusScale: isPen ? 0.5 : 0.48,
|
|
51
|
+
maxRadiusScale: isPen ? 1.22 : 1.24,
|
|
52
|
+
startTaper: Math.max(strokeWidth * 1.8, 2.5),
|
|
53
|
+
endTaper: Math.max(strokeWidth * 2.3, 3.5),
|
|
54
|
+
simulatePressure: !isPen,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getPointDirection(
|
|
59
|
+
points: readonly Vec[],
|
|
60
|
+
index: number,
|
|
61
|
+
isClosed: boolean,
|
|
62
|
+
): Vec {
|
|
63
|
+
if (points.length <= 1) {
|
|
64
|
+
return new Vec(1, 0);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (isClosed && points.length >= 3) {
|
|
68
|
+
const current = points[index]!;
|
|
69
|
+
const previous = points[(index - 1 + points.length) % points.length]!;
|
|
70
|
+
const next = points[(index + 1) % points.length]!;
|
|
71
|
+
const incoming = current.sub(previous).uni();
|
|
72
|
+
const outgoing = next.sub(current).uni();
|
|
73
|
+
const combined = incoming.add(outgoing);
|
|
74
|
+
|
|
75
|
+
if (combined.len2() > 1e-6) {
|
|
76
|
+
return combined.uni();
|
|
77
|
+
}
|
|
78
|
+
if (outgoing.len2() > 0) {
|
|
79
|
+
return outgoing;
|
|
80
|
+
}
|
|
81
|
+
if (incoming.len2() > 0) {
|
|
82
|
+
return incoming;
|
|
83
|
+
}
|
|
84
|
+
return new Vec(1, 0);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const current = points[index]!;
|
|
88
|
+
const previous = points[index - 1] ?? current;
|
|
89
|
+
const next = points[index + 1] ?? current;
|
|
90
|
+
const incoming = current.sub(previous).uni();
|
|
91
|
+
const outgoing = next.sub(current).uni();
|
|
92
|
+
|
|
93
|
+
if (index === 0) {
|
|
94
|
+
return outgoing.len2() > 0 ? outgoing : new Vec(1, 0);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (index === points.length - 1) {
|
|
98
|
+
return incoming.len2() > 0 ? incoming : new Vec(1, 0);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const combined = incoming.add(outgoing);
|
|
102
|
+
if (combined.len2() > 1e-6) {
|
|
103
|
+
return combined.uni();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (outgoing.len2() > 0) {
|
|
107
|
+
return outgoing;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (incoming.len2() > 0) {
|
|
111
|
+
return incoming;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return new Vec(1, 0);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function dot(a: Vec, b: Vec): number {
|
|
118
|
+
return a.x * b.x + a.y * b.y;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function getSimulatedPressure({
|
|
122
|
+
distanceFromPrevious,
|
|
123
|
+
previousPressure,
|
|
124
|
+
runningLength,
|
|
125
|
+
totalLength,
|
|
126
|
+
strokeWidth,
|
|
127
|
+
isClosed,
|
|
128
|
+
}: {
|
|
129
|
+
distanceFromPrevious: number;
|
|
130
|
+
previousPressure: number | null;
|
|
131
|
+
runningLength: number;
|
|
132
|
+
totalLength: number;
|
|
133
|
+
strokeWidth: number;
|
|
134
|
+
isClosed: boolean;
|
|
135
|
+
}): number {
|
|
136
|
+
const distanceRatio = clamp(
|
|
137
|
+
distanceFromPrevious /
|
|
138
|
+
Math.max(strokeWidth * SIMULATED_PRESSURE_DISTANCE_SCALE, 1),
|
|
139
|
+
0,
|
|
140
|
+
1,
|
|
141
|
+
);
|
|
142
|
+
const targetPressure = clamp(
|
|
143
|
+
1 - distanceRatio,
|
|
144
|
+
SIMULATED_PRESSURE_MIN,
|
|
145
|
+
1,
|
|
146
|
+
);
|
|
147
|
+
const basePressure =
|
|
148
|
+
previousPressure === null
|
|
149
|
+
? lerp(
|
|
150
|
+
SIMULATED_PRESSURE_INITIAL,
|
|
151
|
+
targetPressure,
|
|
152
|
+
SIMULATED_PRESSURE_CHANGE_RATE,
|
|
153
|
+
)
|
|
154
|
+
: lerp(
|
|
155
|
+
previousPressure,
|
|
156
|
+
targetPressure,
|
|
157
|
+
Math.max(distanceRatio, 0.36) * SIMULATED_PRESSURE_CHANGE_RATE,
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
if (isClosed) {
|
|
161
|
+
return clamp(basePressure, SIMULATED_PRESSURE_MIN, 1);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const taperIn = clamp(runningLength / Math.max(strokeWidth * 1.8, 1), 0, 1);
|
|
165
|
+
const taperOut = clamp(
|
|
166
|
+
(totalLength - runningLength) / Math.max(strokeWidth * 2.6, 1),
|
|
167
|
+
0,
|
|
168
|
+
1,
|
|
169
|
+
);
|
|
170
|
+
const taper = Math.min(taperIn, taperOut);
|
|
171
|
+
|
|
172
|
+
return clamp(
|
|
173
|
+
basePressure * lerp(0.72, 1, taper),
|
|
174
|
+
SIMULATED_PRESSURE_MIN,
|
|
175
|
+
1,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function getStrokeSamplePressure(
|
|
180
|
+
point: DrawShapePoint,
|
|
181
|
+
previousPressure: number | null,
|
|
182
|
+
style: DrawStrokeStyleOptions,
|
|
183
|
+
strokeWidth: number,
|
|
184
|
+
distanceFromPrevious: number,
|
|
185
|
+
runningLength: number,
|
|
186
|
+
totalLength: number,
|
|
187
|
+
isClosed: boolean,
|
|
188
|
+
): number {
|
|
189
|
+
if (style.simulatePressure) {
|
|
190
|
+
return getSimulatedPressure({
|
|
191
|
+
distanceFromPrevious,
|
|
192
|
+
previousPressure,
|
|
193
|
+
runningLength,
|
|
194
|
+
totalLength,
|
|
195
|
+
strokeWidth,
|
|
196
|
+
isClosed,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const inputPressure =
|
|
201
|
+
typeof point.pressure === "number" && Number.isFinite(point.pressure)
|
|
202
|
+
? clamp(point.pressure, 0.05, 1)
|
|
203
|
+
: previousPressure ?? 0.55;
|
|
204
|
+
|
|
205
|
+
return previousPressure === null
|
|
206
|
+
? inputPressure
|
|
207
|
+
: lerp(previousPressure, inputPressure, 0.68);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function createStrokeDerivedPoints(
|
|
211
|
+
points: readonly DrawShapePoint[],
|
|
212
|
+
strokeWidth: number,
|
|
213
|
+
isPen: boolean,
|
|
214
|
+
isClosed: boolean,
|
|
215
|
+
options: { taperStart?: boolean; taperEnd?: boolean } = {},
|
|
216
|
+
): DrawStrokeDerivedPoint[] {
|
|
217
|
+
if (points.length === 0) {
|
|
218
|
+
return [];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const style = getDrawStrokeStyleOptions(strokeWidth, isPen);
|
|
222
|
+
const pointVecs = points.map(drawPointToVec);
|
|
223
|
+
const runningLengths: number[] = [0];
|
|
224
|
+
let totalLength = 0;
|
|
225
|
+
|
|
226
|
+
for (let index = 1; index < pointVecs.length; index += 1) {
|
|
227
|
+
totalLength += pointVecs[index]!.dist(pointVecs[index - 1]!);
|
|
228
|
+
runningLengths.push(totalLength);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let previousPressure: number | null = null;
|
|
232
|
+
const derived = points.map((point, index) => {
|
|
233
|
+
const runningLength = runningLengths[index] ?? 0;
|
|
234
|
+
const distanceFromPrevious =
|
|
235
|
+
index === 0 ? 0 : pointVecs[index]!.dist(pointVecs[index - 1]!);
|
|
236
|
+
const pressure = getStrokeSamplePressure(
|
|
237
|
+
point,
|
|
238
|
+
previousPressure,
|
|
239
|
+
style,
|
|
240
|
+
strokeWidth,
|
|
241
|
+
distanceFromPrevious,
|
|
242
|
+
runningLength,
|
|
243
|
+
totalLength,
|
|
244
|
+
isClosed,
|
|
245
|
+
);
|
|
246
|
+
previousPressure = pressure;
|
|
247
|
+
|
|
248
|
+
const taperStart = options.taperStart ?? true;
|
|
249
|
+
const taperEnd = options.taperEnd ?? true;
|
|
250
|
+
const taper =
|
|
251
|
+
isClosed || pointVecs.length <= 1
|
|
252
|
+
? 1
|
|
253
|
+
: Math.min(
|
|
254
|
+
taperStart ? clamp(runningLength / style.startTaper, 0, 1) : 1,
|
|
255
|
+
taperEnd
|
|
256
|
+
? clamp((totalLength - runningLength) / style.endTaper, 0, 1)
|
|
257
|
+
: 1,
|
|
258
|
+
);
|
|
259
|
+
const radiusScale = lerp(
|
|
260
|
+
style.minRadiusScale,
|
|
261
|
+
style.maxRadiusScale,
|
|
262
|
+
pressure,
|
|
263
|
+
);
|
|
264
|
+
const radius = Math.max(
|
|
265
|
+
style.minRadius,
|
|
266
|
+
style.baseRadius * radiusScale * lerp(0.7, 1, taper),
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
point: pointVecs[index]!,
|
|
271
|
+
direction: getPointDirection(pointVecs, index, isClosed),
|
|
272
|
+
pressure,
|
|
273
|
+
radius,
|
|
274
|
+
runningLength,
|
|
275
|
+
};
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
if (totalLength < style.baseRadius * 0.5) {
|
|
279
|
+
return derived.map((sample) => ({
|
|
280
|
+
...sample,
|
|
281
|
+
radius: Math.max(sample.radius, style.baseRadius * 0.92),
|
|
282
|
+
}));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return derived;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function createCircleOutline(center: Vec, radius: number, segments: number): Vec[] {
|
|
289
|
+
const outline: Vec[] = [];
|
|
290
|
+
for (let index = 0; index < segments; index += 1) {
|
|
291
|
+
const angle = (Math.PI * 2 * index) / segments;
|
|
292
|
+
outline.push(
|
|
293
|
+
new Vec(
|
|
294
|
+
center.x + Math.cos(angle) * radius,
|
|
295
|
+
center.y + Math.sin(angle) * radius,
|
|
296
|
+
),
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
return outline;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function appendDistinctPoint(points: Vec[], point: Vec): void {
|
|
303
|
+
const previous = points[points.length - 1];
|
|
304
|
+
if (!previous) {
|
|
305
|
+
points.push(point);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (previous.dist2(point) < 0.04) {
|
|
310
|
+
points[points.length - 1] = point;
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
points.push(point);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function createClockwiseHalfArc(
|
|
318
|
+
center: Vec,
|
|
319
|
+
fromPoint: Vec,
|
|
320
|
+
steps: number,
|
|
321
|
+
): Vec[] {
|
|
322
|
+
const radius = center.dist(fromPoint);
|
|
323
|
+
if (radius === 0) {
|
|
324
|
+
return [];
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const startAngle = Vec.Angle(center, fromPoint);
|
|
328
|
+
const points: Vec[] = [];
|
|
329
|
+
|
|
330
|
+
for (let index = 1; index < steps; index += 1) {
|
|
331
|
+
const angle = startAngle - Math.PI * (index / steps);
|
|
332
|
+
points.push(
|
|
333
|
+
new Vec(
|
|
334
|
+
center.x + Math.cos(angle) * radius,
|
|
335
|
+
center.y + Math.sin(angle) * radius,
|
|
336
|
+
),
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return points;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export function getStrokeOutlineVertices(
|
|
344
|
+
points: readonly DrawStrokeDerivedPoint[],
|
|
345
|
+
isClosed: boolean,
|
|
346
|
+
options: { capStart?: boolean; capEnd?: boolean } = {},
|
|
347
|
+
): Vec[] {
|
|
348
|
+
if (points.length === 0) {
|
|
349
|
+
return [];
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (points.length === 1) {
|
|
353
|
+
return createCircleOutline(points[0]!.point, points[0]!.radius, DOT_SEGMENTS);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const leftPoints: Vec[] = [];
|
|
357
|
+
const rightPoints: Vec[] = [];
|
|
358
|
+
let minOutlineVertexDistance = 0.12;
|
|
359
|
+
for (const sample of points) {
|
|
360
|
+
minOutlineVertexDistance = Math.max(
|
|
361
|
+
minOutlineVertexDistance,
|
|
362
|
+
sample.radius * OUTLINE_VERTEX_MIN_DISTANCE_FACTOR,
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
const minDistanceSquared = minOutlineVertexDistance ** 2;
|
|
366
|
+
let previousNormal: Vec | null = null;
|
|
367
|
+
|
|
368
|
+
for (let index = 0; index < points.length; index += 1) {
|
|
369
|
+
const sample = points[index]!;
|
|
370
|
+
const nextSample = points[index + 1] ?? sample;
|
|
371
|
+
const direction = sample.direction;
|
|
372
|
+
const nextDirection = nextSample.direction;
|
|
373
|
+
const directionBlend = dot(direction, nextDirection);
|
|
374
|
+
let normal =
|
|
375
|
+
index === 0 || index === points.length - 1
|
|
376
|
+
? direction.per().uni()
|
|
377
|
+
: Vec.Lrp(nextDirection, direction, directionBlend).per().uni();
|
|
378
|
+
if (normal.len2() < 1e-6) {
|
|
379
|
+
normal = previousNormal ?? new Vec(0, 1);
|
|
380
|
+
}
|
|
381
|
+
previousNormal = normal;
|
|
382
|
+
|
|
383
|
+
appendDistinctPointWithThreshold(
|
|
384
|
+
leftPoints,
|
|
385
|
+
sample.point.add(normal.mul(sample.radius)),
|
|
386
|
+
minDistanceSquared,
|
|
387
|
+
);
|
|
388
|
+
appendDistinctPointWithThreshold(
|
|
389
|
+
rightPoints,
|
|
390
|
+
sample.point.sub(normal.mul(sample.radius)),
|
|
391
|
+
minDistanceSquared,
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (isClosed && points.length >= 3) {
|
|
396
|
+
const outline: Vec[] = [];
|
|
397
|
+
for (const point of leftPoints) {
|
|
398
|
+
appendDistinctPoint(outline, point);
|
|
399
|
+
}
|
|
400
|
+
for (let index = rightPoints.length - 1; index >= 0; index -= 1) {
|
|
401
|
+
const point = rightPoints[index]!;
|
|
402
|
+
appendDistinctPoint(outline, point);
|
|
403
|
+
}
|
|
404
|
+
return outline;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const outline: Vec[] = [];
|
|
408
|
+
for (const point of leftPoints) {
|
|
409
|
+
appendDistinctPoint(outline, point);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (options.capEnd ?? true) {
|
|
413
|
+
const endSample = points[points.length - 1]!;
|
|
414
|
+
for (const point of createClockwiseHalfArc(
|
|
415
|
+
endSample.point,
|
|
416
|
+
leftPoints[leftPoints.length - 1]!,
|
|
417
|
+
HALF_CAP_SEGMENTS,
|
|
418
|
+
)) {
|
|
419
|
+
appendDistinctPoint(outline, point);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
for (let index = rightPoints.length - 1; index >= 0; index -= 1) {
|
|
424
|
+
const point = rightPoints[index]!;
|
|
425
|
+
appendDistinctPoint(outline, point);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (options.capStart ?? true) {
|
|
429
|
+
const startSample = points[0]!;
|
|
430
|
+
for (const point of createClockwiseHalfArc(
|
|
431
|
+
startSample.point,
|
|
432
|
+
rightPoints[0]!,
|
|
433
|
+
HALF_CAP_SEGMENTS,
|
|
434
|
+
)) {
|
|
435
|
+
appendDistinctPoint(outline, point);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return outline;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function appendDistinctPointWithThreshold(
|
|
443
|
+
points: Vec[],
|
|
444
|
+
point: Vec,
|
|
445
|
+
minDistanceSquared: number,
|
|
446
|
+
): void {
|
|
447
|
+
const previous = points[points.length - 1];
|
|
448
|
+
if (!previous || previous.dist2(point) > minDistanceSquared) {
|
|
449
|
+
appendDistinctPoint(points, point);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
points[points.length - 1] = point;
|
|
454
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineLandExtension,
|
|
3
|
+
type LandFeatureBundle,
|
|
4
|
+
} from "@land/canvas-plugin-api";
|
|
5
|
+
import { createDrawCanvasPlugin } from "./canvas";
|
|
6
|
+
import { createDrawCorePlugin } from "./core";
|
|
7
|
+
|
|
8
|
+
export function createDrawFeatureBundle(): LandFeatureBundle {
|
|
9
|
+
return {
|
|
10
|
+
id: "land.draw.bundle",
|
|
11
|
+
corePlugins: [createDrawCorePlugin()],
|
|
12
|
+
canvasPlugins: [createDrawCanvasPlugin()],
|
|
13
|
+
shapeTypes: ["draw"],
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const Draw = defineLandExtension({
|
|
18
|
+
name: "draw",
|
|
19
|
+
createFeature: createDrawFeatureBundle,
|
|
20
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export { DrawTool } from "./DrawTool";
|
|
2
|
+
export { DrawShapeUtil } from "./DrawShapeUtil";
|
|
3
|
+
export { DrawShapeSvgExporter, createDrawCorePlugin } from "./core";
|
|
4
|
+
export { DrawShapeSvg } from "./DrawShapeRenderer";
|
|
5
|
+
export { createDrawCanvasPlugin } from "./canvas";
|
|
6
|
+
export { Draw, createDrawFeatureBundle } from "./feature";
|
|
7
|
+
export {
|
|
8
|
+
flipDrawShapeInSelectionBounds,
|
|
9
|
+
getDrawShapeDiagnostics,
|
|
10
|
+
getDrawShapePageSegments,
|
|
11
|
+
getDrawShapePagePoints,
|
|
12
|
+
normalizeTransformedDrawShape,
|
|
13
|
+
} from "./drawGeometry";
|
|
14
|
+
export { interpolateDrawShapeRecords } from "./drawInterpolation";
|
|
15
|
+
export type {
|
|
16
|
+
DrawShapePoint,
|
|
17
|
+
DrawShapeProps,
|
|
18
|
+
DrawShapeRecord,
|
|
19
|
+
DrawShapeSegment,
|
|
20
|
+
} from "./types";
|
|
21
|
+
export { isDrawShapeRecord } from "./types";
|
|
22
|
+
export type {
|
|
23
|
+
DrawShapeDiagnostics,
|
|
24
|
+
DrawShapeSamplingStats,
|
|
25
|
+
} from "./drawGeometry";
|
|
26
|
+
export type { DrawShapeInterpolationOptions } from "./drawInterpolation";
|
|
27
|
+
export type {
|
|
28
|
+
DrawSelectionFlipInput,
|
|
29
|
+
DrawSelectionResizeInput,
|
|
30
|
+
DrawSelectionRotationTransform,
|
|
31
|
+
} from "./drawSelectionTransform";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isShapeRecord,
|
|
3
|
+
type BaseRecord,
|
|
4
|
+
type ShapeRecordBase,
|
|
5
|
+
} from "@land/canvas-core";
|
|
6
|
+
|
|
7
|
+
export interface DrawShapePoint {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
pressure?: number;
|
|
11
|
+
segment?: "straight";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface DrawShapeSegment {
|
|
15
|
+
points: DrawShapePoint[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface DrawShapeProps {
|
|
19
|
+
segments: DrawShapeSegment[];
|
|
20
|
+
stroke: string;
|
|
21
|
+
strokeWidth: number;
|
|
22
|
+
fill: string;
|
|
23
|
+
isClosed: boolean;
|
|
24
|
+
isPen?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface DrawShapeRecord extends ShapeRecordBase<"draw", DrawShapeProps> {
|
|
28
|
+
type: "draw";
|
|
29
|
+
props: DrawShapeProps;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function isDrawShapeRecord(
|
|
33
|
+
record: BaseRecord | null | undefined,
|
|
34
|
+
): record is DrawShapeRecord {
|
|
35
|
+
return isShapeRecord(record) && record.type === "draw";
|
|
36
|
+
}
|