@link-foundation/ui-components 0.0.0-stage → 0.0.0
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/CONFIGURATIONS.md +74 -0
- package/ICONS.md +76 -0
- package/LICENSE +24 -0
- package/README.md +1499 -2
- package/bin/lino-network.mjs +71 -0
- package/dist/icons/ATTRIBUTION.md +602 -0
- package/dist/icons/NOTICE.lino +1261 -0
- package/dist/icons/brand.js +31 -0
- package/dist/icons/brand.svg +3 -0
- package/dist/icons/licenses/brand/DISCLAIMER.md +51 -0
- package/dist/icons/licenses/brand/LICENSE.md +30 -0
- package/dist/icons/licenses/lucide/LICENSE +43 -0
- package/dist/icons/licenses/material-symbols/LICENSE +202 -0
- package/dist/icons/licenses/material-symbols/NOTICE +12 -0
- package/dist/icons/licenses/tabler/LICENSE +21 -0
- package/dist/icons/lucide.js +681 -0
- package/dist/icons/lucide.svg +34 -0
- package/dist/icons/material-symbols.js +681 -0
- package/dist/icons/material-symbols.svg +34 -0
- package/dist/icons/own.js +521 -0
- package/dist/icons/own.svg +34 -0
- package/dist/icons/tabler.js +681 -0
- package/dist/icons/tabler.svg +34 -0
- package/dist/network-layout-NOTICE.md +6 -0
- package/dist/network-layout-d3-dispatch-LICENSE.txt +13 -0
- package/dist/network-layout-d3-force-LICENSE.txt +13 -0
- package/dist/network-layout-d3-quadtree-LICENSE.txt +13 -0
- package/dist/network-layout-d3-timer-LICENSE.txt +13 -0
- package/dist/network-layout-dagre-LICENSE.txt +19 -0
- package/dist/network-layout.worker.mjs +2 -0
- package/package.json +246 -4
- package/src/a11y/announce.js +88 -0
- package/src/a11y/bind-machine.js +181 -0
- package/src/a11y/focus.js +169 -0
- package/src/a11y/index.d.ts +89 -0
- package/src/a11y/index.js +19 -0
- package/src/a11y/keys.js +182 -0
- package/src/a11y/roving.js +167 -0
- package/src/a11y/typeahead.js +82 -0
- package/src/behavior/actions.js +235 -0
- package/src/behavior/diagnostics.js +73 -0
- package/src/behavior/dom.d.ts +38 -0
- package/src/behavior/dom.js +420 -0
- package/src/behavior/index.d.ts +211 -0
- package/src/behavior/index.js +29 -0
- package/src/behavior/interpret.js +454 -0
- package/src/behavior/machines/disclosure.lino +46 -0
- package/src/behavior/machines/index.js +91 -0
- package/src/behavior/machines/toggle.lino +25 -0
- package/src/behavior/parse-machine.js +367 -0
- package/src/charts/README.md +78 -0
- package/src/charts/activity-layout.js +350 -0
- package/src/charts/activity-model.js +130 -0
- package/src/charts/activity-validation.js +31 -0
- package/src/charts/chart-model.js +667 -0
- package/src/charts/chart.js +518 -0
- package/src/charts/echarts-adapter.d.ts +23 -0
- package/src/charts/echarts-adapter.js +240 -0
- package/src/charts/element.js +330 -0
- package/src/charts/errors.js +62 -0
- package/src/charts/gauge.js +27 -0
- package/src/charts/index.d.ts +344 -0
- package/src/charts/index.js +59 -0
- package/src/charts/layout.js +542 -0
- package/src/charts/lino.js +158 -0
- package/src/charts/mark-options.js +78 -0
- package/src/charts/read.js +101 -0
- package/src/charts/renderer-features.js +8 -0
- package/src/charts/scales.js +223 -0
- package/src/charts/small-model.js +223 -0
- package/src/charts/small-svg.js +269 -0
- package/src/charts/sparkline.js +27 -0
- package/src/charts/stat-tile.js +37 -0
- package/src/charts/summary.js +193 -0
- package/src/charts/svg-renderer.js +226 -0
- package/src/charts/svg.js +69 -0
- package/src/charts/tag-cloud.js +129 -0
- package/src/charts/tokens.js +171 -0
- package/src/charts/tooltip.js +47 -0
- package/src/charts/values.js +146 -0
- package/src/chat/README.md +142 -0
- package/src/chat/actions.js +75 -0
- package/src/chat/chat-contract.js +95 -0
- package/src/chat/chat-list.js +42 -0
- package/src/chat/chat-model.js +125 -0
- package/src/chat/chat.css +228 -0
- package/src/chat/composer.js +131 -0
- package/src/chat/day-separator.js +12 -0
- package/src/chat/dom.js +33 -0
- package/src/chat/follow.js +49 -0
- package/src/chat/index.d.ts +142 -0
- package/src/chat/index.js +13 -0
- package/src/chat/message-bubble.js +191 -0
- package/src/chat/message-stream.js +331 -0
- package/src/chat/reasoning-trace.js +70 -0
- package/src/chat/selection.js +39 -0
- package/src/chat/skins/bubbles.lino +2 -0
- package/src/chat/skins/cards.lino +4 -0
- package/src/chat/skins/compact.lino +4 -0
- package/src/chat/skins/signal.lino +25 -0
- package/src/chat/skins/t3.lino +25 -0
- package/src/chat/skins/telegram.lino +31 -0
- package/src/chat/skins/whatsapp.lino +25 -0
- package/src/chat/stream-keys.js +57 -0
- package/src/chat/stream-window.js +159 -0
- package/src/chat/vocabulary.js +19 -0
- package/src/controls/accordion.js +160 -0
- package/src/controls/avatar.js +80 -0
- package/src/controls/badge.js +48 -0
- package/src/controls/button-template.js +32 -0
- package/src/controls/button.js +97 -0
- package/src/controls/catalog.lino +84 -0
- package/src/controls/checkbox.js +112 -0
- package/src/controls/chip.js +140 -0
- package/src/controls/choices.js +45 -0
- package/src/controls/color-field.js +217 -0
- package/src/controls/color-popover.js +135 -0
- package/src/controls/color-rgb.js +80 -0
- package/src/controls/color.js +67 -0
- package/src/controls/combobox.js +192 -0
- package/src/controls/dom.js +261 -0
- package/src/controls/icon-button.js +33 -0
- package/src/controls/index.d.ts +163 -0
- package/src/controls/index.js +182 -0
- package/src/controls/kinds-text.js +358 -0
- package/src/controls/kinds.lino +350 -0
- package/src/controls/label.js +52 -0
- package/src/controls/link.js +101 -0
- package/src/controls/listbox.js +144 -0
- package/src/controls/machines/button.lino +20 -0
- package/src/controls/machines/checkbox.lino +17 -0
- package/src/controls/machines/chip.lino +20 -0
- package/src/controls/machines/color-field.lino +23 -0
- package/src/controls/machines/combobox.lino +82 -0
- package/src/controls/machines/icon-button.lino +20 -0
- package/src/controls/machines/index.js +586 -0
- package/src/controls/machines/number-field.lino +71 -0
- package/src/controls/machines/radio-group.lino +17 -0
- package/src/controls/machines/select.lino +122 -0
- package/src/controls/machines/slider.lino +99 -0
- package/src/controls/machines/switch.lino +19 -0
- package/src/controls/machines/text-area.lino +26 -0
- package/src/controls/machines/text-field.lino +26 -0
- package/src/controls/number-field.js +239 -0
- package/src/controls/numeric-keys.js +125 -0
- package/src/controls/option.js +54 -0
- package/src/controls/progress.js +76 -0
- package/src/controls/radio-group.js +234 -0
- package/src/controls/select.js +229 -0
- package/src/controls/slider.js +234 -0
- package/src/controls/stories.js +172 -0
- package/src/controls/styles.css +1060 -0
- package/src/controls/switch.js +95 -0
- package/src/controls/text-area.js +39 -0
- package/src/controls/text-field.js +131 -0
- package/src/controls/vocabulary.js +164 -0
- package/src/data/data.css +95 -0
- package/src/data/index.d.ts +83 -0
- package/src/data/index.js +10 -0
- package/src/data/keyboard.js +189 -0
- package/src/data/list.js +215 -0
- package/src/data/render-data.js +116 -0
- package/src/data/selection-dom.js +39 -0
- package/src/data/selection-model.js +43 -0
- package/src/data/sort-model.js +42 -0
- package/src/data/source.js +45 -0
- package/src/data/table.js +160 -0
- package/src/data/tree-model.js +41 -0
- package/src/data/tree.js +55 -0
- package/src/data/viewport.js +216 -0
- package/src/data/virtual-range.js +92 -0
- package/src/data/vocabulary.js +67 -0
- package/src/diagrams/diagram-model.js +511 -0
- package/src/diagrams/diagram.js +204 -0
- package/src/diagrams/index.d.ts +225 -0
- package/src/diagrams/index.js +48 -0
- package/src/diagrams/layout-dagre.js +164 -0
- package/src/diagrams/layout-plugins.js +131 -0
- package/src/diagrams/mermaid-export.js +232 -0
- package/src/diagrams/mermaid-view.js +229 -0
- package/src/diagrams/svg-diagram.js +215 -0
- package/src/editor/code-editor.js +206 -0
- package/src/editor/documents-model.js +170 -0
- package/src/editor/documents-persistence.js +85 -0
- package/src/editor/editor-engine.js +22 -0
- package/src/editor/editor-ui.js +203 -0
- package/src/editor/editor.css +106 -0
- package/src/editor/engines/codemirror.js +227 -0
- package/src/editor/engines/monaco.js +219 -0
- package/src/editor/index.d.ts +111 -0
- package/src/editor/index.js +14 -0
- package/src/editor/lino-highlight.js +106 -0
- package/src/editor/lino-structure.js +85 -0
- package/src/editor/validate-lino.js +43 -0
- package/src/embed/README.md +161 -0
- package/src/embed/controller.js +223 -0
- package/src/embed/element.js +141 -0
- package/src/embed/index.d.ts +75 -0
- package/src/embed/index.js +9 -0
- package/src/embed/providers/telegram.d.ts +3 -0
- package/src/embed/providers/telegram.js +38 -0
- package/src/embed/providers/vk.d.ts +3 -0
- package/src/embed/providers/vk.js +42 -0
- package/src/embed/providers/x.d.ts +3 -0
- package/src/embed/providers/x.js +31 -0
- package/src/embed/registry.js +35 -0
- package/src/embed/script.js +73 -0
- package/src/embed/view.js +122 -0
- package/src/icons/adapters/lucide.d.ts +15 -0
- package/src/icons/adapters/lucide.js +142 -0
- package/src/icons/adapters/material-symbols.d.ts +13 -0
- package/src/icons/adapters/material-symbols.js +113 -0
- package/src/icons/adapters/shared.d.ts +38 -0
- package/src/icons/adapters/shared.js +157 -0
- package/src/icons/adapters/simple-icons.d.ts +9 -0
- package/src/icons/adapters/simple-icons.js +38 -0
- package/src/icons/adapters/tabler.d.ts +9 -0
- package/src/icons/adapters/tabler.js +86 -0
- package/src/icons/icon-registry.js +215 -0
- package/src/icons/icon.js +356 -0
- package/src/icons/index.d.ts +123 -0
- package/src/icons/index.js +30 -0
- package/src/icons/names.js +51 -0
- package/src/icons/own-icons.js +521 -0
- package/src/icons/presentation.js +55 -0
- package/src/icons/svg.js +136 -0
- package/src/index.d.ts +14 -0
- package/src/index.js +40 -0
- package/src/layout/README.md +102 -0
- package/src/layout/app-shell.js +150 -0
- package/src/layout/catalog.lino +27 -0
- package/src/layout/dock-model.js +576 -0
- package/src/layout/dock-view.js +312 -0
- package/src/layout/dock.js +352 -0
- package/src/layout/dom.js +312 -0
- package/src/layout/grid.js +192 -0
- package/src/layout/index.d.ts +267 -0
- package/src/layout/index.js +60 -0
- package/src/layout/layer-manager.js +120 -0
- package/src/layout/layout.css +760 -0
- package/src/layout/menu.js +141 -0
- package/src/layout/nav-strip.js +174 -0
- package/src/layout/page-links.js +166 -0
- package/src/layout/panel.js +26 -0
- package/src/layout/pref-switches.js +209 -0
- package/src/layout/split-model.js +270 -0
- package/src/layout/split.js +430 -0
- package/src/layout/stack.js +86 -0
- package/src/layout/surfaces.js +119 -0
- package/src/layout/tab-group.js +151 -0
- package/src/layout/tab-strip.js +277 -0
- package/src/layout/vocabulary.js +229 -0
- package/src/layout/window-frame.js +222 -0
- package/src/layout/window.js +146 -0
- package/src/links-view/animator.js +144 -0
- package/src/links-view/configuration-classes.js +150 -0
- package/src/links-view/configuration-model.js +191 -0
- package/src/links-view/configuration-notations.js +230 -0
- package/src/links-view/configurations.d.ts +73 -0
- package/src/links-view/configurations.js +33 -0
- package/src/links-view/create-layout-runner.js +79 -0
- package/src/links-view/doublet-curves.js +239 -0
- package/src/links-view/doublet-layout.js +148 -0
- package/src/links-view/doublet-markers.js +109 -0
- package/src/links-view/doublet-options.js +369 -0
- package/src/links-view/doublet-svg.js +156 -0
- package/src/links-view/doublet.js +230 -0
- package/src/links-view/export.js +146 -0
- package/src/links-view/geometry.js +409 -0
- package/src/links-view/index.d.ts +1029 -0
- package/src/links-view/index.js +140 -0
- package/src/links-view/labels.js +28 -0
- package/src/links-view/layout-worker-entry.mjs +2 -0
- package/src/links-view/layout-worker.js +28 -0
- package/src/links-view/layouts/circle.js +7 -0
- package/src/links-view/layouts/force.js +76 -0
- package/src/links-view/layouts/grid.js +7 -0
- package/src/links-view/layouts/hex.js +8 -0
- package/src/links-view/layouts/index.js +15 -0
- package/src/links-view/layouts/layered.js +29 -0
- package/src/links-view/layouts/regular.js +27 -0
- package/src/links-view/layouts/spiral.js +10 -0
- package/src/links-view/link-view.js +494 -0
- package/src/links-view/links-gallery.js +306 -0
- package/src/links-view/links-network.js +496 -0
- package/src/links-view/lino-network-source.js +122 -0
- package/src/links-view/lino-network.js +233 -0
- package/src/links-view/lino-to-network.js +252 -0
- package/src/links-view/markers.js +192 -0
- package/src/links-view/model.js +233 -0
- package/src/links-view/network-editor/dom.js +36 -0
- package/src/links-view/network-editor/editor.js +202 -0
- package/src/links-view/network-editor/model.js +163 -0
- package/src/links-view/network-editor/options.js +265 -0
- package/src/links-view/network-editor/panels.js +423 -0
- package/src/links-view/network-editor/presets.js +76 -0
- package/src/links-view/network-editor/provider.js +93 -0
- package/src/links-view/network-editor/remap.js +45 -0
- package/src/links-view/network-editor/schema.js +224 -0
- package/src/links-view/network-editor/share.js +232 -0
- package/src/links-view/network-editor/styles.js +19 -0
- package/src/links-view/network-editor/url-sync.js +131 -0
- package/src/links-view/network-interaction.js +228 -0
- package/src/links-view/network-model.js +85 -0
- package/src/links-view/network-renderer.js +259 -0
- package/src/links-view/network-scene.js +245 -0
- package/src/links-view/network-source.js +82 -0
- package/src/links-view/network-vocabulary.js +18 -0
- package/src/links-view/presentation.js +199 -0
- package/src/links-view/render-canvas.js +102 -0
- package/src/links-view/render-svg.js +118 -0
- package/src/links-view/scene.js +233 -0
- package/src/links-view/solver-job.js +106 -0
- package/src/links-view/tokens.js +114 -0
- package/src/links-view/zoom-pan.js +203 -0
- package/src/overlays/catalog.lino +89 -0
- package/src/overlays/context-menu.js +110 -0
- package/src/overlays/dialog.js +117 -0
- package/src/overlays/dom.js +168 -0
- package/src/overlays/drawer.js +8 -0
- package/src/overlays/index.d.ts +80 -0
- package/src/overlays/index.js +25 -0
- package/src/overlays/kinds.lino +137 -0
- package/src/overlays/list-popover.js +129 -0
- package/src/overlays/machines/index.js +71 -0
- package/src/overlays/machines/surface.lino +11 -0
- package/src/overlays/machines/tabs.lino +7 -0
- package/src/overlays/machines/toast.lino +16 -0
- package/src/overlays/machines/tooltip.lino +29 -0
- package/src/overlays/menu.js +240 -0
- package/src/overlays/popover.js +194 -0
- package/src/overlays/position.js +84 -0
- package/src/overlays/radial-geometry.js +38 -0
- package/src/overlays/radial-menu.js +50 -0
- package/src/overlays/styles.css +155 -0
- package/src/overlays/tabs.js +125 -0
- package/src/overlays/toast-queue.js +105 -0
- package/src/overlays/toast.js +163 -0
- package/src/overlays/tooltip.js +110 -0
- package/src/overlays/vocabulary.js +3 -0
- package/src/settings/conditions.js +44 -0
- package/src/settings/control-for.js +62 -0
- package/src/settings/field-control.js +201 -0
- package/src/settings/index.d.ts +201 -0
- package/src/settings/index.js +3 -0
- package/src/settings/messages.js +112 -0
- package/src/settings/miniature.js +26 -0
- package/src/settings/panel-actions.js +234 -0
- package/src/settings/panel-model.js +49 -0
- package/src/settings/panel-refresh.js +56 -0
- package/src/settings/panel-rows.js +200 -0
- package/src/settings/preset-cards.js +43 -0
- package/src/settings/saved-looks.js +227 -0
- package/src/settings/section-layout.js +95 -0
- package/src/settings/section-tabs.js +175 -0
- package/src/settings/settings-panel.js +192 -0
- package/src/settings/settings.css +178 -0
- package/stories/accordion.lino +14 -0
- package/stories/alert-dialog.lino +4 -0
- package/stories/app-bar.lino +10 -0
- package/stories/avatar.lino +7 -0
- package/stories/badge.lino +12 -0
- package/stories/button.lino +17 -0
- package/stories/card.lino +10 -0
- package/stories/checkbox.lino +14 -0
- package/stories/chip.lino +19 -0
- package/stories/color-field.lino +9 -0
- package/stories/combobox.lino +10 -0
- package/stories/container.lino +11 -0
- package/stories/context-menu.lino +15 -0
- package/stories/dialog.lino +16 -0
- package/stories/drawer.lino +12 -0
- package/stories/icon-button.lino +13 -0
- package/stories/label.lino +7 -0
- package/stories/link-cards.lino +9 -0
- package/stories/link.lino +9 -0
- package/stories/menu-item.lino +6 -0
- package/stories/menu-separator.lino +7 -0
- package/stories/menu.lino +30 -0
- package/stories/nav-strip.lino +11 -0
- package/stories/number-field.lino +16 -0
- package/stories/option.lino +10 -0
- package/stories/paper.lino +11 -0
- package/stories/popover.lino +4 -0
- package/stories/pref-switches.lino +4 -0
- package/stories/progress.lino +14 -0
- package/stories/radial-menu.lino +19 -0
- package/stories/radio-group.lino +22 -0
- package/stories/radio.lino +10 -0
- package/stories/section-nav.lino +9 -0
- package/stories/select.lino +16 -0
- package/stories/slider.lino +14 -0
- package/stories/switch.lino +10 -0
- package/stories/tab-panel.lino +7 -0
- package/stories/tab.lino +7 -0
- package/stories/tabs.lino +25 -0
- package/stories/text-area.lino +12 -0
- package/stories/text-field.lino +23 -0
- package/stories/titled-section.lino +7 -0
- package/stories/toast.lino +3 -0
- package/stories/toaster.lino +2 -0
- package/stories/tooltip.lino +3 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The geometry of one link: pure functions from end points to a path of
|
|
3
|
+
* cubic Bézier segments, its arc length and its centre (the point half way
|
|
4
|
+
* along the path). Ported from links-visuals (Unlicense): nine control
|
|
5
|
+
* points from the source to the target, joined by a Catmull-Rom spline whose
|
|
6
|
+
* segments are written as cubic Béziers.
|
|
7
|
+
*
|
|
8
|
+
* Nothing here reads the DOM, the clock or random numbers, so the same input
|
|
9
|
+
* always gives the same numbers.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {{x: number, y: number}} Point
|
|
14
|
+
* @typedef {{p0: Point, c1: Point, c2: Point, p3: Point}} CubicSegment
|
|
15
|
+
* @typedef {object} LinkGeometry
|
|
16
|
+
* @property {CubicSegment[]} path the link from the source to the target
|
|
17
|
+
* @property {Point} center the point at half the arc length (the anchor)
|
|
18
|
+
* @property {number} length the arc length of the path
|
|
19
|
+
* @property {Point[]} points the control points the spline passes through
|
|
20
|
+
* (the blueprint points: start, three, centre, three, end)
|
|
21
|
+
* @property {Point} source
|
|
22
|
+
* @property {Point} target
|
|
23
|
+
* @property {boolean} selfLoop
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** The number of blueprint points (links-visuals: x factors -4..4). */
|
|
27
|
+
export const POINT_COUNT = 9;
|
|
28
|
+
|
|
29
|
+
/** The radius of a self-loop when none is given. */
|
|
30
|
+
export const DEFAULT_LOOP_RADIUS = 24;
|
|
31
|
+
|
|
32
|
+
/** The nodes and weights of 8-point Gauss-Legendre quadrature on [-1, 1]. */
|
|
33
|
+
const GAUSS = Object.freeze([
|
|
34
|
+
Object.freeze([-0.9602898564975363, 0.1012285362903763]),
|
|
35
|
+
Object.freeze([-0.7966664774136267, 0.2223810344533745]),
|
|
36
|
+
Object.freeze([-0.525532409916329, 0.3137066458778873]),
|
|
37
|
+
Object.freeze([-0.1834346424956498, 0.362683783378362]),
|
|
38
|
+
Object.freeze([0.1834346424956498, 0.362683783378362]),
|
|
39
|
+
Object.freeze([0.525532409916329, 0.3137066458778873]),
|
|
40
|
+
Object.freeze([0.7966664774136267, 0.2223810344533745]),
|
|
41
|
+
Object.freeze([0.9602898564975363, 0.1012285362903763]),
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
/** Pieces each segment is cut into before the quadrature. */
|
|
45
|
+
const PIECES = 8;
|
|
46
|
+
|
|
47
|
+
/** Newton steps when a length is turned back into a curve parameter. */
|
|
48
|
+
const NEWTON_STEPS = 24;
|
|
49
|
+
|
|
50
|
+
export const point = (x, y) => ({ x, y });
|
|
51
|
+
export const add = (a, b) => point(a.x + b.x, a.y + b.y);
|
|
52
|
+
export const sub = (a, b) => point(a.x - b.x, a.y - b.y);
|
|
53
|
+
export const scale = (a, k) => point(a.x * k, a.y * k);
|
|
54
|
+
export const lerp = (a, b, t) =>
|
|
55
|
+
point(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t);
|
|
56
|
+
export const distance = (a, b) => Math.hypot(b.x - a.x, b.y - a.y);
|
|
57
|
+
export const samePoint = (a, b) => a.x === b.x && a.y === b.y;
|
|
58
|
+
|
|
59
|
+
/** Whether a value is a point with finite coordinates. */
|
|
60
|
+
export const isPoint = (p) =>
|
|
61
|
+
p !== null &&
|
|
62
|
+
typeof p === 'object' &&
|
|
63
|
+
Number.isFinite(p.x) &&
|
|
64
|
+
Number.isFinite(p.y);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The point of a cubic segment at the curve parameter `t` (0..1).
|
|
68
|
+
* @param {CubicSegment} s
|
|
69
|
+
* @param {number} t
|
|
70
|
+
* @returns {Point}
|
|
71
|
+
*/
|
|
72
|
+
export function cubicPoint(s, t) {
|
|
73
|
+
const u = 1 - t;
|
|
74
|
+
const a = u * u * u;
|
|
75
|
+
const b = 3 * u * u * t;
|
|
76
|
+
const c = 3 * u * t * t;
|
|
77
|
+
const d = t * t * t;
|
|
78
|
+
return point(
|
|
79
|
+
a * s.p0.x + b * s.c1.x + c * s.c2.x + d * s.p3.x,
|
|
80
|
+
a * s.p0.y + b * s.c1.y + c * s.c2.y + d * s.p3.y
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The derivative (the tangent vector) of a cubic segment at `t`.
|
|
86
|
+
* @param {CubicSegment} s
|
|
87
|
+
* @param {number} t
|
|
88
|
+
* @returns {Point}
|
|
89
|
+
*/
|
|
90
|
+
export function cubicDerivative(s, t) {
|
|
91
|
+
const u = 1 - t;
|
|
92
|
+
const a = 3 * u * u;
|
|
93
|
+
const b = 6 * u * t;
|
|
94
|
+
const c = 3 * t * t;
|
|
95
|
+
return point(
|
|
96
|
+
a * (s.c1.x - s.p0.x) + b * (s.c2.x - s.c1.x) + c * (s.p3.x - s.c2.x),
|
|
97
|
+
a * (s.c1.y - s.p0.y) + b * (s.c2.y - s.c1.y) + c * (s.p3.y - s.c2.y)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const speed = (s, t) => {
|
|
102
|
+
const d = cubicDerivative(s, t);
|
|
103
|
+
return Math.hypot(d.x, d.y);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** The arc length of a segment between two curve parameters. */
|
|
107
|
+
function lengthBetween(s, t0, t1) {
|
|
108
|
+
const half = (t1 - t0) / 2;
|
|
109
|
+
const mid = (t0 + t1) / 2;
|
|
110
|
+
return (
|
|
111
|
+
GAUSS.reduce((sum, [x, w]) => sum + w * speed(s, mid + half * x), 0) * half
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The arc length of a cubic segment from 0 to `t` (default 1), by
|
|
117
|
+
* Gauss-Legendre quadrature on fixed pieces: deterministic, and accurate to
|
|
118
|
+
* far below 1e-9 for the curves a link draws.
|
|
119
|
+
* @param {CubicSegment} s
|
|
120
|
+
* @param {number} [t]
|
|
121
|
+
*/
|
|
122
|
+
export function segmentLength(s, t = 1) {
|
|
123
|
+
let sum = 0;
|
|
124
|
+
for (let i = 0; i < PIECES; i += 1) {
|
|
125
|
+
sum += lengthBetween(s, (t * i) / PIECES, (t * (i + 1)) / PIECES);
|
|
126
|
+
}
|
|
127
|
+
return sum;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The arc length of a path.
|
|
132
|
+
* @param {CubicSegment[]} path
|
|
133
|
+
*/
|
|
134
|
+
export const pathLength = (path) =>
|
|
135
|
+
path.reduce((sum, s) => sum + segmentLength(s), 0);
|
|
136
|
+
|
|
137
|
+
/** The curve parameter at which a segment reaches the arc length `target`. */
|
|
138
|
+
function parameterAtLength(s, target, total) {
|
|
139
|
+
if (target <= 0 || total === 0) {
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
if (target >= total) {
|
|
143
|
+
return 1;
|
|
144
|
+
}
|
|
145
|
+
let lo = 0;
|
|
146
|
+
let hi = 1;
|
|
147
|
+
let t = target / total;
|
|
148
|
+
for (let i = 0; i < NEWTON_STEPS; i += 1) {
|
|
149
|
+
const error = segmentLength(s, t) - target;
|
|
150
|
+
if (Math.abs(error) < 1e-12) {
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
if (error > 0) {
|
|
154
|
+
hi = t;
|
|
155
|
+
} else {
|
|
156
|
+
lo = t;
|
|
157
|
+
}
|
|
158
|
+
const v = speed(s, t);
|
|
159
|
+
const next = v > 0 ? t - error / v : (lo + hi) / 2;
|
|
160
|
+
// Newton's step, kept inside the bracket (bisection when it leaves it).
|
|
161
|
+
t = next > lo && next < hi ? next : (lo + hi) / 2;
|
|
162
|
+
}
|
|
163
|
+
return t;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The segment and curve parameter at the arc-length fraction `fraction`. */
|
|
167
|
+
function locate(path, fraction) {
|
|
168
|
+
const lengths = path.map((s) => segmentLength(s));
|
|
169
|
+
const total = lengths.reduce((a, b) => a + b, 0);
|
|
170
|
+
const clamped = Math.min(1, Math.max(0, fraction));
|
|
171
|
+
let rest = clamped * total;
|
|
172
|
+
for (let i = 0; i < path.length; i += 1) {
|
|
173
|
+
if (rest <= lengths[i] || i === path.length - 1) {
|
|
174
|
+
return {
|
|
175
|
+
segment: path[i],
|
|
176
|
+
t: parameterAtLength(path[i], rest, lengths[i]),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
rest -= lengths[i];
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The point at the arc-length fraction `t` (0 = start, 1 = end) of a path.
|
|
186
|
+
* @param {CubicSegment[]} path
|
|
187
|
+
* @param {number} t
|
|
188
|
+
* @returns {Point}
|
|
189
|
+
*/
|
|
190
|
+
export function pointAtLength(path, t) {
|
|
191
|
+
const at = locate(path, t);
|
|
192
|
+
return at === null ? point(0, 0) : cubicPoint(at.segment, at.t);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The direction (radians, 0 = +x, clockwise with y down) of a path at the
|
|
197
|
+
* arc-length fraction `t`.
|
|
198
|
+
* @param {CubicSegment[]} path
|
|
199
|
+
* @param {number} t
|
|
200
|
+
*/
|
|
201
|
+
export function angleAtLength(path, t) {
|
|
202
|
+
const at = locate(path, t);
|
|
203
|
+
if (at === null) {
|
|
204
|
+
return 0;
|
|
205
|
+
}
|
|
206
|
+
const d = cubicDerivative(at.segment, at.t);
|
|
207
|
+
return Math.atan2(d.y, d.x);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The direction of the first non-degenerate handle of a segment. */
|
|
211
|
+
function handleAngle(from, handles) {
|
|
212
|
+
const to = handles.find((p) => !samePoint(p, from)) ?? from;
|
|
213
|
+
return Math.atan2(to.y - from.y, to.x - from.x);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The directions of a path at its two ends, as an SVG `orient="auto"`
|
|
218
|
+
* marker takes them: along the path at the start and at the end.
|
|
219
|
+
* @param {CubicSegment[]} path
|
|
220
|
+
* @returns {{start: number, end: number}}
|
|
221
|
+
*/
|
|
222
|
+
export function endAngles(path) {
|
|
223
|
+
if (path.length === 0) {
|
|
224
|
+
return { start: 0, end: 0 };
|
|
225
|
+
}
|
|
226
|
+
const first = path[0];
|
|
227
|
+
const last = path[path.length - 1];
|
|
228
|
+
const end = handleAngle(last.p3, [last.c2, last.c1, last.p0]);
|
|
229
|
+
return {
|
|
230
|
+
start: handleAngle(first.p0, [first.c1, first.c2, first.p3]),
|
|
231
|
+
end: end + Math.PI,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Catmull-Rom tangents as links-visuals takes them: one-sided at the ends,
|
|
237
|
+
* central differences in between.
|
|
238
|
+
* @param {Point[]} points
|
|
239
|
+
* @returns {Point[]}
|
|
240
|
+
*/
|
|
241
|
+
export function catmullRomTangents(points) {
|
|
242
|
+
const last = points.length - 1;
|
|
243
|
+
return points.map((p, i) => {
|
|
244
|
+
if (i === 0) {
|
|
245
|
+
return sub(points[1], p);
|
|
246
|
+
}
|
|
247
|
+
if (i === last) {
|
|
248
|
+
return sub(p, points[last - 1]);
|
|
249
|
+
}
|
|
250
|
+
return scale(sub(points[i + 1], points[i - 1]), 0.5);
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* A Catmull-Rom spline through `points`, written as cubic Bézier segments
|
|
256
|
+
* (`c1 = p_i + m_i / 3`, `c2 = p_{i+1} - m_{i+1} / 3`).
|
|
257
|
+
* @param {Point[]} points at least two
|
|
258
|
+
* @returns {CubicSegment[]}
|
|
259
|
+
*/
|
|
260
|
+
export function catmullRomToCubics(points) {
|
|
261
|
+
if (points.length < 2) {
|
|
262
|
+
return [];
|
|
263
|
+
}
|
|
264
|
+
const m = catmullRomTangents(points);
|
|
265
|
+
const segments = [];
|
|
266
|
+
for (let i = 0; i < points.length - 1; i += 1) {
|
|
267
|
+
segments.push({
|
|
268
|
+
p0: points[i],
|
|
269
|
+
c1: add(points[i], scale(m[i], 1 / 3)),
|
|
270
|
+
c2: sub(points[i + 1], scale(m[i + 1], 1 / 3)),
|
|
271
|
+
p3: points[i + 1],
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return segments;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* The nine control points of a link from `source` to `target`. With
|
|
279
|
+
* `bend = 0` they lie evenly on the straight line; otherwise they are moved
|
|
280
|
+
* along the left normal (y down) by `4 · bend · distance · f · (1 - f)`, a
|
|
281
|
+
* parabola whose top is `bend · distance` away from the middle of the line.
|
|
282
|
+
* @param {Point} source
|
|
283
|
+
* @param {Point} target
|
|
284
|
+
* @param {number} [bend]
|
|
285
|
+
* @returns {Point[]}
|
|
286
|
+
*/
|
|
287
|
+
export function controlPoints(source, target, bend = 0) {
|
|
288
|
+
const d = distance(source, target);
|
|
289
|
+
const normal =
|
|
290
|
+
d === 0
|
|
291
|
+
? point(0, 0)
|
|
292
|
+
: point((target.y - source.y) / d, (source.x - target.x) / d);
|
|
293
|
+
const points = [];
|
|
294
|
+
for (let i = 0; i < POINT_COUNT; i += 1) {
|
|
295
|
+
const f = i / (POINT_COUNT - 1);
|
|
296
|
+
const offset = 4 * bend * d * f * (1 - f);
|
|
297
|
+
points.push(add(lerp(source, target, f), scale(normal, offset)));
|
|
298
|
+
}
|
|
299
|
+
return points;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* The control points of a self-loop at `anchor`: a teardrop above it that
|
|
304
|
+
* leaves to the upper left and comes back from the upper right, with its
|
|
305
|
+
* top (the centre of the link) at `anchor.y - 2.6 · radius`.
|
|
306
|
+
* @param {Point} anchor
|
|
307
|
+
* @param {number} [radius]
|
|
308
|
+
* @returns {Point[]}
|
|
309
|
+
*/
|
|
310
|
+
export function selfLoopPoints(anchor, radius = DEFAULT_LOOP_RADIUS) {
|
|
311
|
+
const center = point(anchor.x, anchor.y - 1.6 * radius);
|
|
312
|
+
const points = [anchor];
|
|
313
|
+
const inner = POINT_COUNT - 2;
|
|
314
|
+
for (let i = 0; i < inner; i += 1) {
|
|
315
|
+
// From the lower left (150°) over the top (270°) to the lower right.
|
|
316
|
+
const angle = (5 * Math.PI) / 6 + (i * (4 * Math.PI)) / 3 / (inner - 1);
|
|
317
|
+
points.push(
|
|
318
|
+
point(
|
|
319
|
+
center.x + radius * Math.cos(angle),
|
|
320
|
+
center.y + radius * Math.sin(angle)
|
|
321
|
+
)
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
points.push(anchor);
|
|
325
|
+
return points;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* The geometry of one link.
|
|
330
|
+
* @param {{source: Point, target?: Point, bend?: number,
|
|
331
|
+
* selfLoop?: boolean, loopRadius?: number}} options a link without a
|
|
332
|
+
* target, or with `selfLoop: true`, or whose ends are the same point, is
|
|
333
|
+
* drawn as a self-loop at the source
|
|
334
|
+
* @returns {LinkGeometry}
|
|
335
|
+
*/
|
|
336
|
+
export function linkGeometry({
|
|
337
|
+
source,
|
|
338
|
+
target,
|
|
339
|
+
bend = 0,
|
|
340
|
+
selfLoop,
|
|
341
|
+
loopRadius,
|
|
342
|
+
}) {
|
|
343
|
+
if (!isPoint(source)) {
|
|
344
|
+
throw new TypeError('linkGeometry: source must be a point {x, y}');
|
|
345
|
+
}
|
|
346
|
+
if (target !== undefined && !isPoint(target)) {
|
|
347
|
+
throw new TypeError('linkGeometry: target must be a point {x, y}');
|
|
348
|
+
}
|
|
349
|
+
if (!Number.isFinite(bend)) {
|
|
350
|
+
throw new TypeError('linkGeometry: bend must be a finite number');
|
|
351
|
+
}
|
|
352
|
+
const loop =
|
|
353
|
+
selfLoop === true || target === undefined || samePoint(source, target);
|
|
354
|
+
const points = loop
|
|
355
|
+
? selfLoopPoints(source, loopRadius)
|
|
356
|
+
: controlPoints(source, target, bend);
|
|
357
|
+
const path = catmullRomToCubics(points);
|
|
358
|
+
return {
|
|
359
|
+
path,
|
|
360
|
+
center: pointAtLength(path, 0.5),
|
|
361
|
+
length: pathLength(path),
|
|
362
|
+
points,
|
|
363
|
+
source,
|
|
364
|
+
target: loop ? source : target,
|
|
365
|
+
selfLoop: loop,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* The bounding box of the points and control points of a path (a cubic
|
|
371
|
+
* stays inside the hull of its four points).
|
|
372
|
+
* @param {CubicSegment[]} path
|
|
373
|
+
* @returns {{minX: number, minY: number, maxX: number, maxY: number}}
|
|
374
|
+
*/
|
|
375
|
+
export function pathBounds(path) {
|
|
376
|
+
const all = path.flatMap((s) => [s.p0, s.c1, s.c2, s.p3]);
|
|
377
|
+
return {
|
|
378
|
+
minX: Math.min(...all.map((p) => p.x)),
|
|
379
|
+
minY: Math.min(...all.map((p) => p.y)),
|
|
380
|
+
maxX: Math.max(...all.map((p) => p.x)),
|
|
381
|
+
maxY: Math.max(...all.map((p) => p.y)),
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* A number as short, stable text: rounded to `digits` decimals, without
|
|
387
|
+
* trailing zeros and without `-0`.
|
|
388
|
+
* @param {number} value
|
|
389
|
+
* @param {number} [digits]
|
|
390
|
+
*/
|
|
391
|
+
export function fmt(value, digits = 3) {
|
|
392
|
+
const rounded = Number(value.toFixed(digits));
|
|
393
|
+
return String(Object.is(rounded, -0) ? 0 : rounded);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* The SVG path data of a path (`M x y C ...`), with rounded numbers so that
|
|
398
|
+
* the output is the same on every engine.
|
|
399
|
+
* @param {CubicSegment[]} path
|
|
400
|
+
* @returns {string}
|
|
401
|
+
*/
|
|
402
|
+
export function pathToSvgD(path) {
|
|
403
|
+
if (path.length === 0) {
|
|
404
|
+
return '';
|
|
405
|
+
}
|
|
406
|
+
const p = (q) => `${fmt(q.x)} ${fmt(q.y)}`;
|
|
407
|
+
const curves = path.map((s) => `C ${p(s.c1)} ${p(s.c2)} ${p(s.p3)}`);
|
|
408
|
+
return [`M ${p(path[0].p0)}`, ...curves].join(' ');
|
|
409
|
+
}
|