@pixel-point/toolcraft 0.0.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/LICENSE.md +98 -0
- package/README.md +41 -0
- package/bin/create-toolcraft-app.mjs +8 -0
- package/bin/toolcraft.mjs +8 -0
- package/package.json +24 -0
- package/scripts/prepare-pack.mjs +29 -0
- package/src/cli.mjs +392 -0
- package/src/cli.test.mjs +284 -0
- package/src/copy-recursive.mjs +86 -0
- package/src/generate.mjs +212 -0
- package/src/generate.test.mjs +322 -0
- package/src/import-map.mjs +14 -0
- package/src/package-json.mjs +80 -0
- package/src/package-json.test.mjs +67 -0
- package/src/rewrite-imports.mjs +85 -0
- package/src/rewrite-imports.test.mjs +58 -0
- package/templates/runtime/contracts/component-contracts.test.ts +1165 -0
- package/templates/runtime/contracts/component-contracts.ts +1340 -0
- package/templates/runtime/contracts/decision-contracts.test.ts +206 -0
- package/templates/runtime/contracts/decision-contracts.ts +283 -0
- package/templates/runtime/contracts/index.test.ts +14 -0
- package/templates/runtime/contracts/index.ts +3 -0
- package/templates/runtime/contracts/types.ts +56 -0
- package/templates/runtime/export/export.test.ts +203 -0
- package/templates/runtime/export/export.ts +132 -0
- package/templates/runtime/export/index.ts +1 -0
- package/templates/runtime/index.ts +14 -0
- package/templates/runtime/react/canvas-shell.test.tsx +424 -0
- package/templates/runtime/react/canvas-shell.tsx +408 -0
- package/templates/runtime/react/control-renderers.ts +31 -0
- package/templates/runtime/react/controls-panel.test.tsx +3736 -0
- package/templates/runtime/react/controls-panel.tsx +2327 -0
- package/templates/runtime/react/curve-geometry.test.ts +70 -0
- package/templates/runtime/react/index.ts +15 -0
- package/templates/runtime/react/layer-tree.ts +96 -0
- package/templates/runtime/react/layers-panel.test.tsx +487 -0
- package/templates/runtime/react/layers-panel.tsx +1348 -0
- package/templates/runtime/react/media-file.ts +82 -0
- package/templates/runtime/react/panel-host-config.ts +80 -0
- package/templates/runtime/react/panel-host-geometry.test.ts +66 -0
- package/templates/runtime/react/panel-host-geometry.ts +109 -0
- package/templates/runtime/react/panel-host-types.ts +74 -0
- package/templates/runtime/react/panel-host.test.tsx +102 -0
- package/templates/runtime/react/panel-host.tsx +353 -0
- package/templates/runtime/react/runtime-public-api.test.tsx +132 -0
- package/templates/runtime/react/settings-transfer.test.ts +150 -0
- package/templates/runtime/react/settings-transfer.ts +279 -0
- package/templates/runtime/react/storage-key-migration.ts +48 -0
- package/templates/runtime/react/theme-runtime.tsx +177 -0
- package/templates/runtime/react/timeline-panel.test.tsx +668 -0
- package/templates/runtime/react/timeline-panel.tsx +2953 -0
- package/templates/runtime/react/toolbar-panel.test.tsx +212 -0
- package/templates/runtime/react/toolbar-panel.tsx +205 -0
- package/templates/runtime/react/toolcraft-app.integration.test.tsx +350 -0
- package/templates/runtime/react/toolcraft-app.test.tsx +339 -0
- package/templates/runtime/react/toolcraft-app.tsx +81 -0
- package/templates/runtime/react/toolcraft-root.test.tsx +347 -0
- package/templates/runtime/react/toolcraft-root.tsx +203 -0
- package/templates/runtime/react/use-toolcraft.ts +41 -0
- package/templates/runtime/schema/define-toolcraft.test.ts +1524 -0
- package/templates/runtime/schema/define-toolcraft.ts +1442 -0
- package/templates/runtime/schema/keyframe-capability.test.ts +90 -0
- package/templates/runtime/schema/keyframe-capability.ts +51 -0
- package/templates/runtime/schema/runtime-targets.ts +40 -0
- package/templates/runtime/schema/types.ts +370 -0
- package/templates/runtime/state/canvas-zoom.ts +8 -0
- package/templates/runtime/state/create-template-state.test.ts +242 -0
- package/templates/runtime/state/create-template-state.ts +95 -0
- package/templates/runtime/state/keyframe-evaluation.test.ts +141 -0
- package/templates/runtime/state/keyframe-evaluation.ts +203 -0
- package/templates/runtime/state/persistence.test.ts +217 -0
- package/templates/runtime/state/persistence.ts +511 -0
- package/templates/runtime/state/reducer.test.ts +937 -0
- package/templates/runtime/state/reducer.ts +1212 -0
- package/templates/runtime/state/timeline-readiness.ts +43 -0
- package/templates/runtime/state/types.ts +242 -0
- package/templates/runtime/styles.css +125 -0
- package/templates/runtime/testing/performance.test.ts +1058 -0
- package/templates/runtime/testing/performance.ts +1078 -0
- package/templates/starter/AGENTS.md +186 -0
- package/templates/starter/LICENSE.md +98 -0
- package/templates/starter/NOTICE.md +8 -0
- package/templates/starter/docs/toolcraft/README.md +41 -0
- package/templates/starter/docs/toolcraft/acceptance-testing.md +205 -0
- package/templates/starter/docs/toolcraft/agent-worklog.md +81 -0
- package/templates/starter/docs/toolcraft/assembly-workflow.md +206 -0
- package/templates/starter/docs/toolcraft/component-rules.md +299 -0
- package/templates/starter/docs/toolcraft/custom-controls.md +71 -0
- package/templates/starter/docs/toolcraft/decision-contract.md +71 -0
- package/templates/starter/docs/toolcraft/performance.md +112 -0
- package/templates/starter/docs/toolcraft/renderer-technique.md +48 -0
- package/templates/starter/docs/toolcraft/schema-reference.md +265 -0
- package/templates/starter/docs/toolcraft/workflow.md +87 -0
- package/templates/starter/e2e/app-browser-acceptance.spec.ts +785 -0
- package/templates/starter/e2e/app-controls.spec.ts +41 -0
- package/templates/starter/e2e/app-performance.spec.ts +326 -0
- package/templates/starter/e2e/canvas-handle-helpers.ts +244 -0
- package/templates/starter/e2e/performance-helpers.ts +612 -0
- package/templates/starter/e2e/product-observable-helpers.ts +170 -0
- package/templates/starter/index.html +12 -0
- package/templates/starter/package.json +52 -0
- package/templates/starter/playwright.config.ts +43 -0
- package/templates/starter/scripts/check-ai-skills.mjs +95 -0
- package/templates/starter/scripts/check-toolcraft-docs.mjs +159 -0
- package/templates/starter/scripts/check-toolcraft-integrity.mjs +232 -0
- package/templates/starter/scripts/run-vite-on-free-port.mjs +48 -0
- package/templates/starter/scripts/toolcraft-port.mjs +54 -0
- package/templates/starter/scripts/toolcraft-port.test.mjs +73 -0
- package/templates/starter/src/app/starter-acceptance.test.ts +5959 -0
- package/templates/starter/src/app/starter-acceptance.ts +2646 -0
- package/templates/starter/src/app/starter-performance.test.ts +1390 -0
- package/templates/starter/src/app/starter-performance.ts +12 -0
- package/templates/starter/src/app/starter-schema.test.ts +70 -0
- package/templates/starter/src/app/starter-schema.ts +15 -0
- package/templates/starter/src/main.tsx +18 -0
- package/templates/starter/src/router.tsx +16 -0
- package/templates/starter/src/routes/index.tsx +7 -0
- package/templates/starter/src/routes/root.tsx +19 -0
- package/templates/starter/src/styles.css +120 -0
- package/templates/starter/tsconfig.json +11 -0
- package/templates/starter/vite.config.ts +13 -0
- package/templates/ui/components/composites/accordion.tsx +73 -0
- package/templates/ui/components/composites/alert-dialog.tsx +190 -0
- package/templates/ui/components/composites/alert.tsx +74 -0
- package/templates/ui/components/composites/aspect-ratio.tsx +22 -0
- package/templates/ui/components/composites/avatar.tsx +98 -0
- package/templates/ui/components/composites/badge.tsx +69 -0
- package/templates/ui/components/composites/breadcrumb.tsx +106 -0
- package/templates/ui/components/composites/card.tsx +91 -0
- package/templates/ui/components/composites/combobox.tsx +486 -0
- package/templates/ui/components/composites/command.tsx +296 -0
- package/templates/ui/components/composites/context-menu.tsx +247 -0
- package/templates/ui/components/composites/dialog.tsx +282 -0
- package/templates/ui/components/composites/dropdown-menu.tsx +299 -0
- package/templates/ui/components/composites/empty.tsx +110 -0
- package/templates/ui/components/composites/hover-card.tsx +44 -0
- package/templates/ui/components/composites/index.ts +30 -0
- package/templates/ui/components/composites/menubar.tsx +214 -0
- package/templates/ui/components/composites/navigation-menu.tsx +167 -0
- package/templates/ui/components/composites/pagination.tsx +131 -0
- package/templates/ui/components/composites/progress.tsx +72 -0
- package/templates/ui/components/composites/radio-group.tsx +84 -0
- package/templates/ui/components/composites/resizable.tsx +42 -0
- package/templates/ui/components/composites/sheet.tsx +153 -0
- package/templates/ui/components/composites/sidebar-structural.tsx +310 -0
- package/templates/ui/components/composites/sidebar.tsx +431 -0
- package/templates/ui/components/composites/sonner.tsx +35 -0
- package/templates/ui/components/composites/spinner.tsx +43 -0
- package/templates/ui/components/composites/table.tsx +108 -0
- package/templates/ui/components/composites/tabs.tsx +83 -0
- package/templates/ui/components/control-layout/index.tsx +437 -0
- package/templates/ui/components/controls/actions/actions-control.tsx +139 -0
- package/templates/ui/components/controls/actions/index.ts +9 -0
- package/templates/ui/components/controls/anchor-grid/anchor-grid-control.tsx +107 -0
- package/templates/ui/components/controls/anchor-grid/index.ts +4 -0
- package/templates/ui/components/controls/boolean/boolean-controls.tsx +79 -0
- package/templates/ui/components/controls/boolean/index.ts +4 -0
- package/templates/ui/components/controls/channel-mixer/channel-mixer-control.tsx +95 -0
- package/templates/ui/components/controls/channel-mixer/index.ts +4 -0
- package/templates/ui/components/controls/channel-tabs/channel-tabs.tsx +42 -0
- package/templates/ui/components/controls/channel-tabs/index.ts +6 -0
- package/templates/ui/components/controls/code-textarea/code-textarea-control.tsx +90 -0
- package/templates/ui/components/controls/code-textarea/index.ts +4 -0
- package/templates/ui/components/controls/color/color-control.tsx +571 -0
- package/templates/ui/components/controls/color/color-picker-popover.tsx +104 -0
- package/templates/ui/components/controls/color/index.ts +41 -0
- package/templates/ui/components/controls/color/palette-control-data.ts +436 -0
- package/templates/ui/components/controls/color/palette-control.tsx +535 -0
- package/templates/ui/components/controls/color/style-guide-color-picker-channel-utils.ts +162 -0
- package/templates/ui/components/controls/color/style-guide-color-picker-interactions.ts +190 -0
- package/templates/ui/components/controls/color/style-guide-color-picker-logic.ts +485 -0
- package/templates/ui/components/controls/color/style-guide-color-picker-parts.tsx +710 -0
- package/templates/ui/components/controls/color/style-guide-color-picker.tsx +503 -0
- package/templates/ui/components/controls/control-types.ts +43 -0
- package/templates/ui/components/controls/curves/curve-geometry.ts +355 -0
- package/templates/ui/components/controls/curves/curve-graph.tsx +390 -0
- package/templates/ui/components/controls/curves/curves-control.tsx +445 -0
- package/templates/ui/components/controls/curves/index.ts +6 -0
- package/templates/ui/components/controls/file-drop/file-drop-control.tsx +191 -0
- package/templates/ui/components/controls/file-drop/index.ts +5 -0
- package/templates/ui/components/controls/font-picker/font-catalog.json +15360 -0
- package/templates/ui/components/controls/font-picker/font-catalog.ts +116 -0
- package/templates/ui/components/controls/font-picker/font-picker-control.tsx +1202 -0
- package/templates/ui/components/controls/font-picker/font-preview-loader.ts +336 -0
- package/templates/ui/components/controls/font-picker/index.ts +24 -0
- package/templates/ui/components/controls/font-picker/use-hover-intent.ts +46 -0
- package/templates/ui/components/controls/gradient/gradient-control-utils.ts +190 -0
- package/templates/ui/components/controls/gradient/gradient-control.tsx +612 -0
- package/templates/ui/components/controls/gradient/gradient-stop-list.tsx +400 -0
- package/templates/ui/components/controls/gradient/gradient-toolbar.tsx +152 -0
- package/templates/ui/components/controls/gradient/index.ts +4 -0
- package/templates/ui/components/controls/image-picker/image-picker-control.tsx +139 -0
- package/templates/ui/components/controls/image-picker/index.ts +7 -0
- package/templates/ui/components/controls/index.ts +192 -0
- package/templates/ui/components/controls/range-input/index.ts +4 -0
- package/templates/ui/components/controls/range-input/range-input-control.tsx +173 -0
- package/templates/ui/components/controls/range-slider/index.ts +4 -0
- package/templates/ui/components/controls/range-slider/range-slider-control.tsx +122 -0
- package/templates/ui/components/controls/range-slider/range-slider-value.ts +61 -0
- package/templates/ui/components/controls/segmented/index.ts +8 -0
- package/templates/ui/components/controls/segmented/segmented-control.tsx +94 -0
- package/templates/ui/components/controls/select/index.ts +4 -0
- package/templates/ui/components/controls/select/select-control.tsx +223 -0
- package/templates/ui/components/controls/slider/index.ts +4 -0
- package/templates/ui/components/controls/slider/slider-control.tsx +150 -0
- package/templates/ui/components/controls/slider/slider-value.ts +56 -0
- package/templates/ui/components/controls/text-input/index.ts +4 -0
- package/templates/ui/components/controls/text-input/text-input-control.tsx +158 -0
- package/templates/ui/components/controls/use-measured-element-width.ts +42 -0
- package/templates/ui/components/controls/vector/index.ts +8 -0
- package/templates/ui/components/controls/vector/vector-control.tsx +401 -0
- package/templates/ui/components/panel/index.ts +19 -0
- package/templates/ui/components/panel/panel-actions.tsx +165 -0
- package/templates/ui/components/panel/panel-header.tsx +61 -0
- package/templates/ui/components/panel/panel-icon-button.tsx +96 -0
- package/templates/ui/components/panel/panel-section.tsx +168 -0
- package/templates/ui/components/panel/panel-surface.tsx +206 -0
- package/templates/ui/components/panel/panel.tsx +210 -0
- package/templates/ui/components/primitives/animated-loader.tsx +61 -0
- package/templates/ui/components/primitives/button-group.tsx +134 -0
- package/templates/ui/components/primitives/button.tsx +429 -0
- package/templates/ui/components/primitives/checkbox.tsx +62 -0
- package/templates/ui/components/primitives/editable-slider-value-label.tsx +337 -0
- package/templates/ui/components/primitives/field.tsx +225 -0
- package/templates/ui/components/primitives/index.ts +82 -0
- package/templates/ui/components/primitives/input-group.tsx +298 -0
- package/templates/ui/components/primitives/input.tsx +61 -0
- package/templates/ui/components/primitives/internal/button-loading.tsx +178 -0
- package/templates/ui/components/primitives/label.tsx +16 -0
- package/templates/ui/components/primitives/popover.tsx +126 -0
- package/templates/ui/components/primitives/portal-layer-context.tsx +33 -0
- package/templates/ui/components/primitives/primitive-arrow-icon.tsx +38 -0
- package/templates/ui/components/primitives/scroll-fade-logic.ts +441 -0
- package/templates/ui/components/primitives/scroll-fade-render.tsx +75 -0
- package/templates/ui/components/primitives/scroll-fade-types.ts +41 -0
- package/templates/ui/components/primitives/scroll-fade.tsx +72 -0
- package/templates/ui/components/primitives/select.tsx +408 -0
- package/templates/ui/components/primitives/selection-state.ts +31 -0
- package/templates/ui/components/primitives/separator.tsx +21 -0
- package/templates/ui/components/primitives/slider/index.ts +4 -0
- package/templates/ui/components/primitives/slider/slider-interaction.tsx +96 -0
- package/templates/ui/components/primitives/slider/slider-parts.tsx +303 -0
- package/templates/ui/components/primitives/slider/slider-reset.ts +152 -0
- package/templates/ui/components/primitives/slider/slider-value.ts +114 -0
- package/templates/ui/components/primitives/slider/slider.tsx +511 -0
- package/templates/ui/components/primitives/switch.tsx +35 -0
- package/templates/ui/components/primitives/textarea.tsx +49 -0
- package/templates/ui/components/primitives/toggle-group.tsx +114 -0
- package/templates/ui/components/primitives/toggle.tsx +46 -0
- package/templates/ui/components/primitives/tooltip.tsx +100 -0
- package/templates/ui/hooks/use-mobile.ts +21 -0
- package/templates/ui/index.ts +31 -0
- package/templates/ui/lib/control-outline.ts +3 -0
- package/templates/ui/lib/input-control-style.ts +131 -0
- package/templates/ui/lib/style-guide-color-utils.ts +111 -0
- package/templates/ui/lib/utils.ts +6 -0
- package/templates/ui/styles.css +291 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import type { CurvePoint } from "../control-types";
|
|
2
|
+
|
|
3
|
+
export type CurveInterpolation = "monotone" | "smooth";
|
|
4
|
+
|
|
5
|
+
export const curveViewBoxSize = 268;
|
|
6
|
+
export const curveInset = 18;
|
|
7
|
+
export const curveGraphSize = 232;
|
|
8
|
+
export const curveGraphMax = curveInset + curveGraphSize;
|
|
9
|
+
export const curveGridStops = [0.25, 0.5, 0.75] as const;
|
|
10
|
+
export const curveHitThreshold = 10;
|
|
11
|
+
export const defaultCurveInterpolation = "smooth" satisfies CurveInterpolation;
|
|
12
|
+
|
|
13
|
+
export function clamp(value: number): number {
|
|
14
|
+
return Math.min(1, Math.max(0, value));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function normalizeCurvePoints(points: readonly CurvePoint[]): CurvePoint[] {
|
|
18
|
+
return points.map((point) => ({ x: clamp(point.x), y: clamp(point.y) })).sort(sortPointsByX);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function mapPointToSvg(point: CurvePoint): [number, number] {
|
|
22
|
+
return [curveInset + point.x * curveGraphSize, curveGraphMax - point.y * curveGraphSize];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function pointFromSvgEvent(
|
|
26
|
+
event: Pick<PointerEvent, "clientX" | "clientY">,
|
|
27
|
+
svg: SVGSVGElement | null,
|
|
28
|
+
): CurvePoint {
|
|
29
|
+
const rect = svg?.getBoundingClientRect();
|
|
30
|
+
if (!rect) {
|
|
31
|
+
return { x: 0, y: 0 };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const viewBox = svg?.viewBox.baseVal;
|
|
35
|
+
const viewBoxX =
|
|
36
|
+
(viewBox?.x ?? 0) + ((event.clientX - rect.left) / rect.width) * (viewBox?.width ?? rect.width);
|
|
37
|
+
const viewBoxY =
|
|
38
|
+
(viewBox?.y ?? 0) +
|
|
39
|
+
((event.clientY - rect.top) / rect.height) * (viewBox?.height ?? rect.height);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
x: clamp((viewBoxX - curveInset) / curveGraphSize),
|
|
43
|
+
y: clamp(1 - (viewBoxY - curveInset) / curveGraphSize),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function constrainCurvePoint(
|
|
48
|
+
points: readonly CurvePoint[],
|
|
49
|
+
index: number,
|
|
50
|
+
point: CurvePoint,
|
|
51
|
+
): CurvePoint {
|
|
52
|
+
if (index === 0 || index === points.length - 1) {
|
|
53
|
+
return { x: points[index]?.x ?? point.x, y: point.y };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const previousPoint = points[index - 1];
|
|
57
|
+
const nextPoint = points[index + 1];
|
|
58
|
+
const minX = (previousPoint?.x ?? 0) + 0.01;
|
|
59
|
+
const maxX = (nextPoint?.x ?? 1) - 0.01;
|
|
60
|
+
|
|
61
|
+
return { x: Math.min(maxX, Math.max(minX, point.x)), y: point.y };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function replaceCurvePoint(
|
|
65
|
+
points: readonly CurvePoint[],
|
|
66
|
+
index: number,
|
|
67
|
+
point: CurvePoint,
|
|
68
|
+
): CurvePoint[] {
|
|
69
|
+
return normalizeCurvePoints(
|
|
70
|
+
points.map((item, itemIndex) => (itemIndex === index ? point : item)),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function insertCurvePoint(
|
|
75
|
+
points: readonly CurvePoint[],
|
|
76
|
+
point: CurvePoint,
|
|
77
|
+
): { index: number; points: CurvePoint[] } {
|
|
78
|
+
const normalizedPoints = normalizeCurvePoints(points);
|
|
79
|
+
const insertIndex = normalizedPoints.findIndex((item) => item.x > point.x);
|
|
80
|
+
|
|
81
|
+
if (insertIndex <= 0) {
|
|
82
|
+
return {
|
|
83
|
+
index: 0,
|
|
84
|
+
points: replaceCurvePoint(normalizedPoints, 0, { x: 0, y: point.y }),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (insertIndex === -1) {
|
|
89
|
+
const endpointIndex = normalizedPoints.length - 1;
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
index: endpointIndex,
|
|
93
|
+
points: replaceCurvePoint(normalizedPoints, endpointIndex, { x: 1, y: point.y }),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
index: insertIndex,
|
|
99
|
+
points: normalizeCurvePoints([
|
|
100
|
+
...normalizedPoints.slice(0, insertIndex),
|
|
101
|
+
point,
|
|
102
|
+
...normalizedPoints.slice(insertIndex),
|
|
103
|
+
]),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function removeCurvePoint(points: readonly CurvePoint[], index: number): CurvePoint[] {
|
|
108
|
+
return normalizeCurvePoints(points.filter((_, itemIndex) => itemIndex !== index));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function isPointNearCurve(
|
|
112
|
+
points: readonly CurvePoint[],
|
|
113
|
+
point: CurvePoint,
|
|
114
|
+
threshold = curveHitThreshold,
|
|
115
|
+
interpolation: CurveInterpolation = defaultCurveInterpolation,
|
|
116
|
+
): boolean {
|
|
117
|
+
const curvePoint = getCurvePointAtX(points, point.x, interpolation);
|
|
118
|
+
const distance = Math.abs(curvePoint.y - point.y) * curveGraphSize;
|
|
119
|
+
|
|
120
|
+
return distance <= threshold;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function getCurvePointAtX(
|
|
124
|
+
points: readonly CurvePoint[],
|
|
125
|
+
x: number,
|
|
126
|
+
interpolation: CurveInterpolation = defaultCurveInterpolation,
|
|
127
|
+
): CurvePoint {
|
|
128
|
+
const normalizedPoints = normalizeCurvePoints(points);
|
|
129
|
+
const firstPoint = normalizedPoints[0];
|
|
130
|
+
|
|
131
|
+
if (!firstPoint) {
|
|
132
|
+
return { x: clamp(x), y: 0 };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (x <= firstPoint.x) {
|
|
136
|
+
return { x: firstPoint.x, y: firstPoint.y };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (let index = 1; index < normalizedPoints.length; index += 1) {
|
|
140
|
+
const point = normalizedPoints[index];
|
|
141
|
+
|
|
142
|
+
if (point && x <= point.x) {
|
|
143
|
+
return getCurvePointInSegment(normalizedPoints, index, x, interpolation);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const lastPoint = normalizedPoints[normalizedPoints.length - 1] ?? firstPoint;
|
|
148
|
+
|
|
149
|
+
return { x: lastPoint.x, y: lastPoint.y };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function getCurvePath(
|
|
153
|
+
points: readonly CurvePoint[],
|
|
154
|
+
interpolation: CurveInterpolation = defaultCurveInterpolation,
|
|
155
|
+
): string {
|
|
156
|
+
const normalizedPoints = normalizeCurvePoints(points);
|
|
157
|
+
const firstPoint = normalizedPoints[0];
|
|
158
|
+
|
|
159
|
+
if (!firstPoint) {
|
|
160
|
+
return "";
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const tangents = getCurveTangents(normalizedPoints, interpolation);
|
|
164
|
+
const [startX, startY] = mapPointToSvg(firstPoint);
|
|
165
|
+
let path = `M ${startX} ${startY}`;
|
|
166
|
+
|
|
167
|
+
for (let index = 1; index < normalizedPoints.length; index += 1) {
|
|
168
|
+
path += getCurveSegmentPath(normalizedPoints, tangents, index);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return path;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function getCurveSegmentPath(
|
|
175
|
+
points: readonly CurvePoint[],
|
|
176
|
+
tangents: readonly number[],
|
|
177
|
+
index: number,
|
|
178
|
+
): string {
|
|
179
|
+
const previousPoint = points[index - 1];
|
|
180
|
+
const point = points[index];
|
|
181
|
+
const deltaX = point.x - previousPoint.x;
|
|
182
|
+
const controlPointOne = {
|
|
183
|
+
x: previousPoint.x + deltaX / 3,
|
|
184
|
+
y: previousPoint.y + (deltaX * (tangents[index - 1] ?? 0)) / 3,
|
|
185
|
+
};
|
|
186
|
+
const controlPointTwo = {
|
|
187
|
+
x: point.x - deltaX / 3,
|
|
188
|
+
y: point.y - (deltaX * (tangents[index] ?? 0)) / 3,
|
|
189
|
+
};
|
|
190
|
+
const [controlOneX, controlOneY] = mapPointToSvg(controlPointOne);
|
|
191
|
+
const [controlTwoX, controlTwoY] = mapPointToSvg(controlPointTwo);
|
|
192
|
+
const [pointX, pointY] = mapPointToSvg(point);
|
|
193
|
+
|
|
194
|
+
return ` C ${controlOneX} ${controlOneY} ${controlTwoX} ${controlTwoY} ${pointX} ${pointY}`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function getCurvePointInSegment(
|
|
198
|
+
points: readonly CurvePoint[],
|
|
199
|
+
index: number,
|
|
200
|
+
x: number,
|
|
201
|
+
interpolation: CurveInterpolation,
|
|
202
|
+
): CurvePoint {
|
|
203
|
+
const previousPoint = points[index - 1];
|
|
204
|
+
const point = points[index];
|
|
205
|
+
|
|
206
|
+
if (!previousPoint || !point) {
|
|
207
|
+
return { x: clamp(x), y: 0 };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const tangents = getCurveTangents(points, interpolation);
|
|
211
|
+
const deltaX = Math.max(Number.EPSILON, point.x - previousPoint.x);
|
|
212
|
+
const t = clamp((x - previousPoint.x) / deltaX);
|
|
213
|
+
const tSquared = t * t;
|
|
214
|
+
const tCubed = tSquared * t;
|
|
215
|
+
const startTangent = tangents[index - 1] ?? 0;
|
|
216
|
+
const endTangent = tangents[index] ?? 0;
|
|
217
|
+
const y =
|
|
218
|
+
(2 * tCubed - 3 * tSquared + 1) * previousPoint.y +
|
|
219
|
+
(tCubed - 2 * tSquared + t) * deltaX * startTangent +
|
|
220
|
+
(-2 * tCubed + 3 * tSquared) * point.y +
|
|
221
|
+
(tCubed - tSquared) * deltaX * endTangent;
|
|
222
|
+
|
|
223
|
+
return { x: clamp(x), y: clamp(y) };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function getCurveTangents(
|
|
227
|
+
points: readonly CurvePoint[],
|
|
228
|
+
interpolation: CurveInterpolation,
|
|
229
|
+
): number[] {
|
|
230
|
+
return interpolation === "monotone"
|
|
231
|
+
? getMonotoneTangents(points)
|
|
232
|
+
: getSmoothTangents(points);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function getSmoothTangents(points: readonly CurvePoint[]): number[] {
|
|
236
|
+
if (points.length <= 1) {
|
|
237
|
+
return points.map(() => 0);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (points.length === 2) {
|
|
241
|
+
const slope = getSlope(points[0], 0, points);
|
|
242
|
+
|
|
243
|
+
return [slope, slope];
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return points.map((point, index) => {
|
|
247
|
+
if (index === 0) {
|
|
248
|
+
return getSlope(point, 0, points);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (index === points.length - 1) {
|
|
252
|
+
return getSlope(points[index - 1] ?? point, index - 1, points);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const previousPoint = points[index - 1];
|
|
256
|
+
const nextPoint = points[index + 1];
|
|
257
|
+
const deltaX = Math.max(Number.EPSILON, (nextPoint?.x ?? point.x) - previousPoint.x);
|
|
258
|
+
|
|
259
|
+
return ((nextPoint?.y ?? point.y) - previousPoint.y) / deltaX;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function getMonotoneTangents(points: readonly CurvePoint[]): number[] {
|
|
264
|
+
if (points.length <= 1) {
|
|
265
|
+
return points.map(() => 0);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (points.length === 2) {
|
|
269
|
+
const slope = getSlope(points[0], 0, points);
|
|
270
|
+
|
|
271
|
+
return [slope, slope];
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const intervals = points.slice(0, -1).map((point, index) =>
|
|
275
|
+
Math.max(Number.EPSILON, (points[index + 1]?.x ?? point.x) - point.x),
|
|
276
|
+
);
|
|
277
|
+
const slopes = points.slice(0, -1).map((point, index) => getSlope(point, index, points));
|
|
278
|
+
|
|
279
|
+
return points.map((_, index) => {
|
|
280
|
+
if (index === 0) {
|
|
281
|
+
return getEndpointTangent({
|
|
282
|
+
adjacentInterval: intervals[1] ?? intervals[0] ?? 1,
|
|
283
|
+
interval: intervals[0] ?? 1,
|
|
284
|
+
adjacentSlope: slopes[1] ?? slopes[0] ?? 0,
|
|
285
|
+
slope: slopes[0] ?? 0,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (index === points.length - 1) {
|
|
290
|
+
return getEndpointTangent({
|
|
291
|
+
adjacentInterval: intervals[index - 2] ?? intervals[index - 1] ?? 1,
|
|
292
|
+
interval: intervals[index - 1] ?? 1,
|
|
293
|
+
adjacentSlope: slopes[index - 2] ?? slopes[index - 1] ?? 0,
|
|
294
|
+
slope: slopes[index - 1] ?? 0,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const leftSlope = slopes[index - 1] ?? 0;
|
|
299
|
+
const rightSlope = slopes[index] ?? 0;
|
|
300
|
+
|
|
301
|
+
if (leftSlope * rightSlope <= 0) {
|
|
302
|
+
return 0;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const leftInterval = intervals[index - 1] ?? 1;
|
|
306
|
+
const rightInterval = intervals[index] ?? 1;
|
|
307
|
+
const leftWeight = 2 * rightInterval + leftInterval;
|
|
308
|
+
const rightWeight = rightInterval + 2 * leftInterval;
|
|
309
|
+
|
|
310
|
+
return (
|
|
311
|
+
(leftWeight + rightWeight) /
|
|
312
|
+
(leftWeight / leftSlope + rightWeight / rightSlope)
|
|
313
|
+
);
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function getEndpointTangent({
|
|
318
|
+
adjacentInterval,
|
|
319
|
+
adjacentSlope,
|
|
320
|
+
interval,
|
|
321
|
+
slope,
|
|
322
|
+
}: {
|
|
323
|
+
adjacentInterval: number;
|
|
324
|
+
adjacentSlope: number;
|
|
325
|
+
interval: number;
|
|
326
|
+
slope: number;
|
|
327
|
+
}): number {
|
|
328
|
+
const tangent =
|
|
329
|
+
((2 * interval + adjacentInterval) * slope - interval * adjacentSlope) /
|
|
330
|
+
(interval + adjacentInterval);
|
|
331
|
+
|
|
332
|
+
if (Math.sign(tangent) !== Math.sign(slope)) {
|
|
333
|
+
return 0;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (
|
|
337
|
+
Math.sign(slope) !== Math.sign(adjacentSlope) &&
|
|
338
|
+
Math.abs(tangent) > Math.abs(3 * slope)
|
|
339
|
+
) {
|
|
340
|
+
return 3 * slope;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return tangent;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function getSlope(point: CurvePoint, index: number, points: readonly CurvePoint[]): number {
|
|
347
|
+
const nextPoint = points[index + 1];
|
|
348
|
+
const deltaX = Math.max(Number.EPSILON, nextPoint.x - point.x);
|
|
349
|
+
|
|
350
|
+
return (nextPoint.y - point.y) / deltaX;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function sortPointsByX(left: CurvePoint, right: CurvePoint): number {
|
|
354
|
+
return left.x - right.x;
|
|
355
|
+
}
|