@jcsoftdev/ui 0.2.3 → 0.3.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/CHANGELOG.md +288 -7
- package/README.md +2 -1
- package/bin/ui-theme.mjs +1151 -25
- package/dist/ai/attachment/attachment.js +1 -1
- package/dist/ai/chat-list/group.js +3 -1
- package/dist/ai/conversation/conversation.js +1 -1
- package/dist/ai/internal/use-copy.d.ts +1 -5
- package/dist/ai/internal/use-copy.js +1 -32
- package/dist/ai/markdown/parse.js +59 -17
- package/dist/ai/source/source.js +1 -1
- package/dist/atoms/avatar/avatar.d.ts +13 -3
- package/dist/atoms/avatar/avatar.js +40 -2
- package/dist/atoms/avatar/index.d.ts +1 -1
- package/dist/atoms/avatar/index.js +3 -1
- package/dist/atoms/button/button.d.ts +4 -1
- package/dist/atoms/button/button.js +4 -2
- package/dist/atoms/checkbox/checkbox.d.ts +8 -3
- package/dist/atoms/checkbox/checkbox.js +23 -2
- package/dist/atoms/chip/chip.d.ts +9 -3
- package/dist/atoms/chip/chip.js +26 -4
- package/dist/atoms/chip/index.d.ts +1 -1
- package/dist/atoms/chip/index.js +4 -1
- package/dist/atoms/status-dot/index.d.ts +1 -1
- package/dist/atoms/status-dot/status-dot.d.ts +4 -2
- package/dist/atoms/status-dot/status-dot.js +5 -1
- package/dist/charts/bar-chart/bar-race-chart.js +1 -0
- package/dist/charts/beeswarm-chart/beeswarm-chart.js +1 -1
- package/dist/charts/chord-chart/chord-chart.js +13 -5
- package/dist/charts/core/cartesian/cartesian-chart.d.ts +1 -1
- package/dist/charts/core/cartesian/cartesian-chart.js +48 -11
- package/dist/charts/core/cartesian/layout.d.ts +6 -1
- package/dist/charts/core/cartesian/layout.js +14 -5
- package/dist/charts/core/cartesian/marks.d.ts +2 -2
- package/dist/charts/core/cartesian/marks.js +1 -1
- package/dist/charts/core/cartesian/types.d.ts +28 -2
- package/dist/charts/core/category-explorer.d.ts +2 -2
- package/dist/charts/core/category-explorer.js +8 -4
- package/dist/charts/core/chart-dock.d.ts +26 -0
- package/dist/charts/core/chart-dock.js +45 -0
- package/dist/charts/core/chart-frame.d.ts +9 -2
- package/dist/charts/core/chart-frame.js +105 -25
- package/dist/charts/core/distribution/explorer.d.ts +5 -2
- package/dist/charts/core/distribution/explorer.js +12 -5
- package/dist/charts/core/distribution/xy-points.js +1 -1
- package/dist/charts/core/index.d.ts +3 -0
- package/dist/charts/core/index.js +3 -0
- package/dist/charts/core/leave-grace.d.ts +11 -0
- package/dist/charts/core/leave-grace.js +26 -0
- package/dist/charts/core/nearest.d.ts +14 -0
- package/dist/charts/core/nearest.js +22 -0
- package/dist/charts/core/plot-overlay.js +9 -3
- package/dist/charts/core/pointer.test-utils.d.ts +38 -0
- package/dist/charts/core/pointer.test-utils.js +123 -0
- package/dist/charts/core/tooltip.d.ts +25 -2
- package/dist/charts/core/tooltip.js +115 -40
- package/dist/charts/core/touch-select.d.ts +20 -0
- package/dist/charts/core/touch-select.js +184 -0
- package/dist/charts/core/types.d.ts +8 -4
- package/dist/charts/core/types.js +5 -0
- package/dist/charts/core/use-index-navigation.d.ts +17 -6
- package/dist/charts/core/use-index-navigation.js +47 -10
- package/dist/charts/donut-chart/donut-chart.d.ts +5 -1
- package/dist/charts/donut-chart/donut-chart.js +40 -1
- package/dist/charts/donut-chart/slice-labels.d.ts +20 -0
- package/dist/charts/donut-chart/slice-labels.js +20 -0
- package/dist/charts/dumbbell-chart/dumbbell-chart.js +6 -3
- package/dist/charts/fill-gauge-chart/fill-gauge-chart.js +1 -0
- package/dist/charts/funnel-chart/funnel-chart.js +2 -1
- package/dist/charts/gauge-chart/gauge-chart.js +2 -1
- package/dist/charts/heatmap-chart/heatmap-chart.d.ts +4 -1
- package/dist/charts/heatmap-chart/heatmap-chart.js +3 -1
- package/dist/charts/heatmap-chart/heatmap-grid.d.ts +4 -1
- package/dist/charts/heatmap-chart/heatmap-grid.js +3 -1
- package/dist/charts/hex-tile-map/hex-tile-map.js +124 -115
- package/dist/charts/hierarchy-morph/hierarchy-morph.js +2 -1
- package/dist/charts/icicle-chart/icicle-chart.js +7 -9
- package/dist/charts/lollipop-chart/lollipop-chart.js +6 -3
- package/dist/charts/maps/geo/map-viewport.js +1 -1
- package/dist/charts/polar-area-chart/polar-area-chart.js +42 -12
- package/dist/charts/progress-units/progress-units.js +1 -0
- package/dist/charts/radar-chart/radar-chart.js +1 -1
- package/dist/charts/radial-bar-chart/radial-bar-chart.js +2 -1
- package/dist/charts/range-area-chart/range-area-chart.js +3 -2
- package/dist/charts/sankey-chart/sankey-chart.js +12 -4
- package/dist/charts/tree-chart/tree-chart.js +1 -0
- package/dist/charts/treemap-chart/treemap-chart.js +2 -7
- package/dist/charts/waterfall-chart/waterfall-chart.js +6 -3
- package/dist/containers/action-bar/action-bar.d.ts +11 -2
- package/dist/containers/action-bar/action-bar.js +4 -1
- package/dist/containers/card/card.d.ts +21 -3
- package/dist/containers/card/card.js +20 -5
- package/dist/containers/grid/grid.d.ts +15 -0
- package/dist/containers/grid/grid.js +24 -0
- package/dist/containers/grid/index.d.ts +1 -0
- package/dist/containers/grid/index.js +4 -0
- package/dist/containers/index.d.ts +3 -0
- package/dist/containers/index.js +3 -0
- package/dist/containers/kpi/kpi.d.ts +13 -2
- package/dist/containers/kpi/kpi.js +86 -24
- package/dist/containers/kpi-group/index.d.ts +1 -1
- package/dist/containers/kpi-group/index.js +3 -1
- package/dist/containers/kpi-group/kpi-group.d.ts +9 -1
- package/dist/containers/kpi-group/kpi-group.js +11 -1
- package/dist/containers/page/index.d.ts +1 -0
- package/dist/containers/page/index.js +4 -0
- package/dist/containers/page/page.d.ts +7 -0
- package/dist/containers/page/page.js +14 -0
- package/dist/containers/page-header/page-header.d.ts +8 -3
- package/dist/containers/page-header/page-header.js +35 -8
- package/dist/containers/responsive-list/responsive-list.d.ts +1 -1
- package/dist/containers/responsive-list/responsive-list.js +6 -3
- package/dist/containers/stack/index.d.ts +1 -0
- package/dist/containers/stack/index.js +6 -0
- package/dist/containers/stack/stack.d.ts +15 -0
- package/dist/containers/stack/stack.js +43 -0
- package/dist/forms/copy-field/copy-field.d.ts +17 -0
- package/dist/forms/copy-field/copy-field.js +72 -0
- package/dist/forms/copy-field/index.d.ts +1 -0
- package/dist/forms/copy-field/index.js +4 -0
- package/dist/forms/emoji-picker/emoji-picker.js +1 -1
- package/dist/forms/form/form.d.ts +15 -0
- package/dist/forms/form/form.js +29 -0
- package/dist/forms/form/index.d.ts +1 -0
- package/dist/forms/form/index.js +10 -0
- package/dist/forms/index.d.ts +2 -0
- package/dist/forms/index.js +2 -0
- package/dist/forms/input/index.d.ts +1 -1
- package/dist/forms/input/index.js +3 -2
- package/dist/forms/input/input.d.ts +6 -2
- package/dist/forms/input/input.js +9 -3
- package/dist/glass/crystal-map.d.ts +70 -0
- package/dist/glass/crystal-map.js +88 -0
- package/dist/glass/displacement-map.d.ts +117 -0
- package/dist/glass/displacement-map.js +201 -0
- package/dist/glass/index.d.ts +8 -0
- package/dist/glass/index.js +63 -0
- package/dist/glass/lens-crystal.d.ts +29 -0
- package/dist/glass/lens-crystal.js +161 -0
- package/dist/glass/lens-dispersion.d.ts +11 -0
- package/dist/glass/lens-dispersion.js +19 -0
- package/dist/glass/liquid-glass.d.ts +41 -0
- package/dist/glass/liquid-glass.js +256 -0
- package/dist/glass/liquid-motion.d.ts +53 -0
- package/dist/glass/liquid-motion.js +302 -0
- package/dist/glass/liquid-options.d.ts +162 -0
- package/dist/glass/liquid-options.js +179 -0
- package/dist/glass/refraction.d.ts +100 -0
- package/dist/glass/refraction.js +714 -0
- package/dist/glass/specular-map.d.ts +26 -0
- package/dist/glass/specular-map.js +75 -0
- package/dist/glass/spring.d.ts +79 -0
- package/dist/glass/spring.js +58 -0
- package/dist/glass/support.d.ts +13 -0
- package/dist/glass/support.js +14 -0
- package/dist/glass/use-glass-refraction.d.ts +8 -0
- package/dist/glass/use-glass-refraction.js +16 -0
- package/dist/i18n/strings.d.ts +24 -0
- package/dist/i18n/strings.js +18 -2
- package/dist/icons/index.d.ts +2 -0
- package/dist/icons/index.js +4 -0
- package/dist/icons/key.d.ts +2 -0
- package/dist/icons/key.js +12 -0
- package/dist/icons/webhook.d.ts +2 -0
- package/dist/icons/webhook.js +12 -0
- package/dist/liquid/index.d.ts +3 -0
- package/dist/liquid/index.js +29 -0
- package/dist/navigation/breadcrumbs/breadcrumbs.d.ts +29 -0
- package/dist/navigation/breadcrumbs/breadcrumbs.js +70 -0
- package/dist/navigation/breadcrumbs/index.d.ts +1 -0
- package/dist/navigation/breadcrumbs/index.js +6 -0
- package/dist/navigation/index.d.ts +1 -0
- package/dist/navigation/index.js +1 -0
- package/dist/navigation/segmented/index.d.ts +1 -1
- package/dist/navigation/segmented/model.d.ts +5 -0
- package/dist/navigation/segmented/model.js +6 -0
- package/dist/navigation/segmented/segmented.d.ts +17 -3
- package/dist/navigation/segmented/segmented.js +20 -4
- package/dist/overlays/context-menu/context-menu.d.ts +9 -1
- package/dist/overlays/context-menu/context-menu.js +4 -1
- package/dist/overlays/dialog/dialog.d.ts +9 -1
- package/dist/overlays/dialog/dialog.js +3 -0
- package/dist/overlays/drawer/drawer.d.ts +9 -1
- package/dist/overlays/drawer/drawer.js +3 -0
- package/dist/overlays/dropdown-menu/dropdown-menu.d.ts +9 -1
- package/dist/overlays/dropdown-menu/dropdown-menu.js +3 -1
- package/dist/overlays/hover-card/hover-card.d.ts +10 -1
- package/dist/overlays/hover-card/hover-card.js +4 -1
- package/dist/overlays/popover/popover.d.ts +9 -1
- package/dist/overlays/popover/popover.js +3 -0
- package/dist/palette/command-palette/command-palette.d.ts +12 -2
- package/dist/palette/command-palette/command-palette.js +5 -0
- package/dist/primitives/command/command.d.ts +3 -2
- package/dist/primitives/command/command.js +2 -0
- package/dist/primitives/menu/menu.js +1 -1
- package/dist/primitives/popper/popper.js +5 -0
- package/dist/rich-text/rich-text-editor.js +94 -8
- package/dist/rich-text/sanitize.js +70 -20
- package/dist/shell/app-shell/app-shell.d.ts +17 -0
- package/dist/shell/app-shell/app-shell.js +44 -4
- package/dist/shell/bottom-nav/bottom-nav.d.ts +34 -0
- package/dist/shell/bottom-nav/bottom-nav.js +124 -0
- package/dist/shell/bottom-nav/index.d.ts +2 -0
- package/dist/shell/bottom-nav/index.js +9 -0
- package/dist/shell/bottom-nav/split.d.ts +11 -0
- package/dist/shell/bottom-nav/split.js +10 -0
- package/dist/shell/sidebar/sidebar.d.ts +16 -2
- package/dist/shell/sidebar/sidebar.js +46 -4
- package/dist/shell/topbar/topbar.d.ts +20 -9
- package/dist/shell/topbar/topbar.js +16 -9
- package/dist/states/empty-state/empty-state.d.ts +3 -1
- package/dist/states/empty-state/empty-state.js +9 -1
- package/dist/states/error-state/error-state.d.ts +3 -1
- package/dist/states/error-state/error-state.js +2 -0
- package/dist/states/index.d.ts +1 -0
- package/dist/states/index.js +1 -0
- package/dist/states/live-status-bar/index.d.ts +1 -0
- package/dist/states/live-status-bar/index.js +6 -0
- package/dist/states/live-status-bar/live-status-bar.d.ts +17 -0
- package/dist/states/live-status-bar/live-status-bar.js +49 -0
- package/dist/states/offline-banner/offline-banner.d.ts +3 -1
- package/dist/states/offline-banner/offline-banner.js +8 -4
- package/dist/utils/class-groups.js +17 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/material.d.ts +13 -0
- package/dist/utils/material.js +6 -0
- package/dist/utils/tone.d.ts +8 -0
- package/dist/utils/tone.js +17 -0
- package/dist/utils/use-copy.d.ts +5 -0
- package/dist/utils/use-copy.js +35 -0
- package/icons/svg/key.svg +5 -0
- package/icons/svg/webhook.svg +5 -0
- package/package.json +31 -21
- package/scripts/build-theme.d.mts +22 -0
- package/scripts/build-theme.mjs +236 -4
- package/src/ai/attachment/attachment.tsx +1 -1
- package/src/ai/chat-list/group.ts +3 -1
- package/src/ai/conversation/conversation.tsx +1 -1
- package/src/ai/internal/use-copy.ts +1 -38
- package/src/ai/markdown/parse.ts +74 -17
- package/src/ai/source/source.tsx +1 -1
- package/src/atoms/avatar/avatar.tsx +57 -4
- package/src/atoms/avatar/index.ts +8 -1
- package/src/atoms/button/button.tsx +8 -3
- package/src/atoms/checkbox/checkbox.tsx +30 -3
- package/src/atoms/chip/chip.tsx +37 -5
- package/src/atoms/chip/index.ts +7 -1
- package/src/atoms/status-dot/index.ts +1 -0
- package/src/atoms/status-dot/status-dot.tsx +15 -3
- package/src/charts/README.md +21 -0
- package/src/charts/bar-chart/bar-race-chart.tsx +1 -0
- package/src/charts/beeswarm-chart/beeswarm-chart.tsx +1 -1
- package/src/charts/chord-chart/chord-chart.tsx +13 -5
- package/src/charts/core/cartesian/cartesian-chart.tsx +55 -10
- package/src/charts/core/cartesian/layout.ts +29 -10
- package/src/charts/core/cartesian/marks.tsx +3 -3
- package/src/charts/core/cartesian/types.ts +30 -2
- package/src/charts/core/category-explorer.tsx +12 -6
- package/src/charts/core/chart-dock.tsx +68 -0
- package/src/charts/core/chart-frame.tsx +116 -27
- package/src/charts/core/distribution/explorer.tsx +17 -7
- package/src/charts/core/distribution/xy-points.tsx +1 -1
- package/src/charts/core/index.ts +3 -0
- package/src/charts/core/leave-grace.ts +35 -0
- package/src/charts/core/nearest.ts +38 -0
- package/src/charts/core/plot-overlay.tsx +9 -3
- package/src/charts/core/pointer.test-utils.ts +146 -0
- package/src/charts/core/tooltip.tsx +129 -15
- package/src/charts/core/touch-select.ts +250 -0
- package/src/charts/core/types.ts +13 -4
- package/src/charts/core/use-index-navigation.ts +70 -11
- package/src/charts/donut-chart/donut-chart.tsx +53 -3
- package/src/charts/donut-chart/slice-labels.ts +39 -0
- package/src/charts/dumbbell-chart/dumbbell-chart.tsx +8 -3
- package/src/charts/fill-gauge-chart/fill-gauge-chart.tsx +1 -0
- package/src/charts/funnel-chart/funnel-chart.tsx +2 -1
- package/src/charts/gauge-chart/gauge-chart.tsx +2 -1
- package/src/charts/heatmap-chart/heatmap-chart.tsx +5 -0
- package/src/charts/heatmap-chart/heatmap-grid.tsx +5 -1
- package/src/charts/hex-tile-map/hex-tile-map.tsx +6 -1
- package/src/charts/hierarchy-morph/hierarchy-morph.tsx +2 -1
- package/src/charts/icicle-chart/icicle-chart.tsx +8 -9
- package/src/charts/lollipop-chart/lollipop-chart.tsx +8 -3
- package/src/charts/maps/geo/map-viewport.tsx +1 -1
- package/src/charts/polar-area-chart/polar-area-chart.tsx +37 -12
- package/src/charts/progress-units/progress-units.tsx +1 -0
- package/src/charts/radar-chart/radar-chart.tsx +1 -1
- package/src/charts/radial-bar-chart/radial-bar-chart.tsx +2 -1
- package/src/charts/range-area-chart/range-area-chart.tsx +3 -2
- package/src/charts/sankey-chart/sankey-chart.tsx +12 -4
- package/src/charts/tree-chart/tree-chart.tsx +1 -0
- package/src/charts/treemap-chart/treemap-chart.tsx +2 -7
- package/src/charts/waterfall-chart/waterfall-chart.tsx +8 -3
- package/src/containers/action-bar/action-bar.tsx +13 -2
- package/src/containers/card/card.tsx +43 -4
- package/src/containers/grid/grid.tsx +37 -0
- package/src/containers/grid/index.ts +1 -0
- package/src/containers/index.ts +3 -0
- package/src/containers/kpi/kpi.tsx +116 -22
- package/src/containers/kpi-group/index.ts +7 -1
- package/src/containers/kpi-group/kpi-group.tsx +26 -1
- package/src/containers/page/index.ts +1 -0
- package/src/containers/page/page.tsx +17 -0
- package/src/containers/page-header/page-header.tsx +38 -8
- package/src/containers/responsive-list/responsive-list.tsx +7 -4
- package/src/containers/stack/index.ts +7 -0
- package/src/containers/stack/stack.tsx +55 -0
- package/src/forms/copy-field/copy-field.tsx +88 -0
- package/src/forms/copy-field/index.ts +1 -0
- package/src/forms/emoji-picker/emoji-picker.tsx +1 -1
- package/src/forms/form/form.tsx +46 -0
- package/src/forms/form/index.ts +8 -0
- package/src/forms/index.ts +2 -0
- package/src/forms/input/index.ts +1 -1
- package/src/forms/input/input.tsx +14 -3
- package/src/glass/crystal-map.ts +150 -0
- package/src/glass/displacement-map.ts +317 -0
- package/src/glass/index.ts +43 -0
- package/src/glass/lens-crystal.ts +226 -0
- package/src/glass/lens-dispersion.ts +34 -0
- package/src/glass/liquid-glass.tsx +371 -0
- package/src/glass/liquid-motion.ts +395 -0
- package/src/glass/liquid-options.ts +306 -0
- package/src/glass/refraction.ts +1010 -0
- package/src/glass/specular-map.ts +95 -0
- package/src/glass/spring.ts +98 -0
- package/src/glass/support.ts +27 -0
- package/src/glass/use-glass-refraction.ts +21 -0
- package/src/i18n/strings.ts +24 -0
- package/src/icons/index.ts +2 -0
- package/src/icons/key.tsx +13 -0
- package/src/icons/webhook.tsx +13 -0
- package/src/liquid/index.ts +24 -0
- package/src/navigation/breadcrumbs/breadcrumbs.tsx +103 -0
- package/src/navigation/breadcrumbs/index.ts +8 -0
- package/src/navigation/index.ts +1 -0
- package/src/navigation/segmented/index.ts +1 -0
- package/src/navigation/segmented/model.ts +11 -0
- package/src/navigation/segmented/segmented.tsx +45 -6
- package/src/overlays/context-menu/context-menu.tsx +11 -1
- package/src/overlays/dialog/dialog.tsx +10 -0
- package/src/overlays/drawer/drawer.tsx +10 -0
- package/src/overlays/dropdown-menu/dropdown-menu.tsx +10 -1
- package/src/overlays/hover-card/hover-card.tsx +12 -1
- package/src/overlays/popover/popover.tsx +10 -0
- package/src/palette/command-palette/command-palette.tsx +15 -1
- package/src/primitives/command/command.tsx +4 -0
- package/src/primitives/menu/menu.tsx +1 -1
- package/src/primitives/popper/popper.tsx +10 -0
- package/src/rich-text/rich-text-editor.tsx +116 -13
- package/src/rich-text/sanitize.ts +94 -23
- package/src/shell/app-shell/app-shell.tsx +60 -4
- package/src/shell/bottom-nav/bottom-nav.tsx +187 -0
- package/src/shell/bottom-nav/index.ts +8 -0
- package/src/shell/bottom-nav/split.ts +20 -0
- package/src/shell/sidebar/sidebar.tsx +51 -5
- package/src/shell/topbar/topbar.tsx +81 -58
- package/src/states/empty-state/empty-state.tsx +11 -1
- package/src/states/error-state/error-state.tsx +4 -0
- package/src/states/index.ts +1 -0
- package/src/states/live-status-bar/index.ts +6 -0
- package/src/states/live-status-bar/live-status-bar.tsx +62 -0
- package/src/states/offline-banner/offline-banner.tsx +13 -5
- package/src/utils/class-groups.ts +17 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/material.ts +16 -0
- package/src/utils/tone.ts +29 -0
- package/src/utils/use-copy.ts +38 -0
- package/styles/Theme.kt +64 -3
- package/styles/base.css +51 -0
- package/styles/tailwind.css +833 -0
- package/styles/theme.css +546 -4
- package/styles/tokens.css +788 -15
- package/tokens/default.json +918 -29
- package/scripts/build-theme.d.ts +0 -18
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { cachedMap, clamp, type DisplacementField } from "./displacement-map.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The crystal lens: a thick, clear, convex lens that minifies the backdrop. Every output pixel at
|
|
5
|
+
* `p` (measured from the centre of the surface) shows the backdrop at `p + (g(t) - 1) p` with `g(t)
|
|
6
|
+
* = 1 + (gMax - 1) s(t)`, `s(t) = 0.3 + 0.3 t^2 + 0.4 t^6` and `t` the superellipse radial coordinate (1 at
|
|
7
|
+
* the silhouette). The pull is mild in the middle and grows steeply towards the rim, like the
|
|
8
|
+
* side of a ball lens. A radial map `p -> p g(|p|)` with `g >= 1` and `g' >= 0` has the Jacobian
|
|
9
|
+
* determinant `g (g + t g')`, which is at least 1, so it cannot fold back on itself however large
|
|
10
|
+
* `gMax` is: this is what lets the crystal minify by a factor of three at the rim where the other
|
|
11
|
+
* profiles must stay under a slope of one.
|
|
12
|
+
*
|
|
13
|
+
* The samples lie outside the surface (a minifying lens sees a wider area than it covers), so the
|
|
14
|
+
* map is rasterised over the surface plus a margin on every side; the engine lays the filter on a
|
|
15
|
+
* layer that large and clips it back to the silhouette.
|
|
16
|
+
*/
|
|
17
|
+
export const CRYSTAL_EXPONENT = 2.5;
|
|
18
|
+
/** Weights of the constant (even minification), quadratic (whole-surface) and sixth-power (rim) parts of the crystal lens. */
|
|
19
|
+
export const CRYSTAL_WEIGHTS = { flat: 0.3, whole: 0.3, rim: 0.4 } as const;
|
|
20
|
+
/** `gMax - 1` per unit of `refraction`: how much more the rim minifies than the middle. */
|
|
21
|
+
export const CRYSTAL_GAIN = 1.3;
|
|
22
|
+
/** Largest source offset of the crystal lens, in px (bounds the margin and the filter area). */
|
|
23
|
+
export const CRYSTAL_MAX_OFFSET = 260;
|
|
24
|
+
|
|
25
|
+
/** `s(t)` of the crystal lens. */
|
|
26
|
+
export const crystalShape = (t: number) =>
|
|
27
|
+
CRYSTAL_WEIGHTS.flat + CRYSTAL_WEIGHTS.whole * t ** 2 + CRYSTAL_WEIGHTS.rim * t ** 6;
|
|
28
|
+
|
|
29
|
+
/** Radial coordinate of a point of a surface: 1 on the superellipse touching the middle of each side, held there beyond it. */
|
|
30
|
+
export function crystalRadius(px: number, py: number, hx: number, hy: number): number {
|
|
31
|
+
const u = Math.abs(px) / hx;
|
|
32
|
+
const v = Math.abs(py) / hy;
|
|
33
|
+
return Math.min((u ** CRYSTAL_EXPONENT + v ** CRYSTAL_EXPONENT) ** (1 / CRYSTAL_EXPONENT), 1);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Largest source offset of the crystal lens, in px, for a surface and a `refraction` of 0 to 2
|
|
38
|
+
* (1.67 is a thick lens, 2 the thickest the margin allows).
|
|
39
|
+
*/
|
|
40
|
+
export function crystalAmplitude(width: number, height: number, refraction: number): number {
|
|
41
|
+
const half = Math.max(width, height) / 2;
|
|
42
|
+
return Math.min(clamp(refraction, 0, 2) * CRYSTAL_GAIN * half, CRYSTAL_MAX_OFFSET);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Distance, in px, the crystal lens keeps from the edge of its backdrop (see {@link computeCrystalField}). */
|
|
46
|
+
const EDGE_SLACK = 3;
|
|
47
|
+
|
|
48
|
+
export interface CrystalMapOptions {
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
/** Source offset at the silhouette, in px, from {@link crystalAmplitude}; sets the margin. */
|
|
52
|
+
amplitude: number;
|
|
53
|
+
/**
|
|
54
|
+
* Map pixels per CSS pixel. Default 1, between 0.5 and 2: the field is smooth and most of it lies
|
|
55
|
+
* in the margin the layer clips away, so a large lens can go below one.
|
|
56
|
+
*/
|
|
57
|
+
dpr?: number;
|
|
58
|
+
/** Margin past the surface, in px. Default {@link crystalMargin} of `amplitude`. */
|
|
59
|
+
margin?: number;
|
|
60
|
+
/**
|
|
61
|
+
* Margin past each side of the surface, in px (default: `margin` on all four). A surface inside
|
|
62
|
+
* a clipping ancestor (an `overflow` or `clip-path` stage) only has the backdrop within it: a
|
|
63
|
+
* layer that reaches past the clip is cut by the browser and its filter is shifted, so the map
|
|
64
|
+
* covers only the backdrop that exists, and the offsets are clamped so no sample lands beyond it.
|
|
65
|
+
*/
|
|
66
|
+
room?: Partial<Record<"left" | "right" | "top" | "bottom", number>>;
|
|
67
|
+
/** Largest factor the offset is multiplied by (chromatic dispersion pulls red this much further). Default 1. */
|
|
68
|
+
reach?: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Margin, in px, the crystal layer extends past the surface on every side: the largest offset,
|
|
73
|
+
* plus what chromatic dispersion adds to it.
|
|
74
|
+
*/
|
|
75
|
+
export const crystalMargin = (amplitude: number, dispersion = 0) =>
|
|
76
|
+
Math.ceil(amplitude * (1 + Math.max(dispersion, 0))) + 2;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Displacement field of the crystal lens over the surface and its margin: red and green carry
|
|
80
|
+
* `s(t) p / half` (so `scale = 2 amplitude 255/254` restores the offset), blue is 255 (no soft
|
|
81
|
+
* band: a crystal is not frosted). The size of `data` is the surface plus twice the margin.
|
|
82
|
+
*/
|
|
83
|
+
export function computeCrystalField({
|
|
84
|
+
width,
|
|
85
|
+
height,
|
|
86
|
+
amplitude,
|
|
87
|
+
dpr = 1,
|
|
88
|
+
margin,
|
|
89
|
+
room,
|
|
90
|
+
reach = 1,
|
|
91
|
+
}: CrystalMapOptions): DisplacementField {
|
|
92
|
+
const ratio = clamp(dpr, 0.5, 2);
|
|
93
|
+
const w = Math.max(1, width);
|
|
94
|
+
const h = Math.max(1, height);
|
|
95
|
+
const m = margin ?? crystalMargin(amplitude);
|
|
96
|
+
const free = (side: "left" | "right" | "top" | "bottom") => Math.max(room?.[side] ?? m, 0);
|
|
97
|
+
const [left, right, top, bottom] = [free("left"), free("right"), free("top"), free("bottom")];
|
|
98
|
+
const pw = Math.max(1, Math.round((w + left + right) * ratio));
|
|
99
|
+
const ph = Math.max(1, Math.round((h + top + bottom) * ratio));
|
|
100
|
+
const rx = pw / (w + left + right);
|
|
101
|
+
const ry = ph / (h + top + bottom);
|
|
102
|
+
const data = new Uint8ClampedArray(pw * ph * 4);
|
|
103
|
+
const hx = w / 2;
|
|
104
|
+
const hy = h / 2;
|
|
105
|
+
const half = Math.max(hx, hy);
|
|
106
|
+
// A few px short of the edge: the pre-blur fades the last columns of the backdrop to transparent,
|
|
107
|
+
// and a channel pulled further than the others (dispersion) would read that and drop out.
|
|
108
|
+
// Without `room` the map keeps the plain lens: its margin is the whole reach, nothing to clamp.
|
|
109
|
+
const slack = room ? EDGE_SLACK : 0;
|
|
110
|
+
const bounded = room !== undefined;
|
|
111
|
+
const [minX, maxX] = [-hx - left + slack, hx + right - slack];
|
|
112
|
+
const [minY, maxY] = [-hy - top + slack, hy + bottom - slack];
|
|
113
|
+
const unit = Math.max(amplitude, 1e-6);
|
|
114
|
+
const f = Math.max(reach, 1);
|
|
115
|
+
// Offset (as a fraction of `amplitude`, what the channel encodes) that keeps the source in the room.
|
|
116
|
+
const within = (p: number, offset: number, lo: number, hi: number) =>
|
|
117
|
+
clamp((bounded ? clamp(offset, (lo - p) / f, (hi - p) / f) : offset) / unit, -1, 1);
|
|
118
|
+
for (let j = 0; j < ph; j++) {
|
|
119
|
+
const py = (j + 0.5) / ry - top - hy;
|
|
120
|
+
for (let i = 0; i < pw; i++) {
|
|
121
|
+
const px = (i + 0.5) / rx - left - hx;
|
|
122
|
+
const s = crystalShape(crystalRadius(px, py, hx, hy));
|
|
123
|
+
const o = (j * pw + i) * 4;
|
|
124
|
+
data[o] = 128 + within(px, (s * px * amplitude) / half, minX, maxX) * 127;
|
|
125
|
+
data[o + 1] = 128 + within(py, (s * py * amplitude) / half, minY, maxY) * 127;
|
|
126
|
+
data[o + 2] = 255;
|
|
127
|
+
data[o + 3] = 255;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return { width: pw, height: ph, data };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Crystal map as a PNG `data:` URI for `feImage` (browser only, cached). */
|
|
134
|
+
export function createCrystalMap(options: CrystalMapOptions): string {
|
|
135
|
+
const key = [
|
|
136
|
+
options.width,
|
|
137
|
+
options.height,
|
|
138
|
+
Math.round(options.amplitude),
|
|
139
|
+
options.dpr ?? 1,
|
|
140
|
+
options.margin ?? 0,
|
|
141
|
+
options.reach ?? 1,
|
|
142
|
+
options.room?.left ?? -1,
|
|
143
|
+
options.room?.right ?? -1,
|
|
144
|
+
options.room?.top ?? -1,
|
|
145
|
+
options.room?.bottom ?? -1,
|
|
146
|
+
]
|
|
147
|
+
.map((n) => Math.round(n * 100) / 100)
|
|
148
|
+
.join("x");
|
|
149
|
+
return cachedMap(`c${key}`, () => computeCrystalField(options));
|
|
150
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
export interface DisplacementMapOptions {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
/** Corner radius of the glass shape, in px. */
|
|
5
|
+
radius: number;
|
|
6
|
+
/** Width of the refracting bevel, in px, for `coverage: "rim"`; the centre stays undistorted. */
|
|
7
|
+
bezel: number;
|
|
8
|
+
/**
|
|
9
|
+
* `"full"` (default) bends the backdrop over the whole surface, strongest at the rim and
|
|
10
|
+
* gentle but never zero towards the centre (a squarish pillow); `"dome"` does the same with
|
|
11
|
+
* round contours, a drop that is flatter in the middle; `"rim"` only across the bevel.
|
|
12
|
+
*/
|
|
13
|
+
coverage?: LensCoverage;
|
|
14
|
+
/** Width of the rim, in px, over which the veil and the blur ease off (blue channel). Default: `bezel`. */
|
|
15
|
+
soft?: number;
|
|
16
|
+
/** Largest offset of the whole-surface profiles, in px. Default {@link FULL_CAP}. */
|
|
17
|
+
cap?: number;
|
|
18
|
+
/** Device pixel ratio the map is rasterised at. Default 1, capped at 2. */
|
|
19
|
+
dpr?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type LensCoverage = "full" | "rim" | "dome" | "crystal";
|
|
23
|
+
/** The shape of the lens; `coverage` is the older name of the same option. */
|
|
24
|
+
export type LensProfile = LensCoverage;
|
|
25
|
+
|
|
26
|
+
/** Whether a profile bends the whole surface (everything but the rim band). */
|
|
27
|
+
export const isWhole = (coverage: LensCoverage) => coverage !== "rim" && coverage !== "crystal";
|
|
28
|
+
|
|
29
|
+
export interface DisplacementField {
|
|
30
|
+
/** Width and height of `data` in pixels (CSS size times the capped ratio). */
|
|
31
|
+
width: number;
|
|
32
|
+
height: number;
|
|
33
|
+
/**
|
|
34
|
+
* RGBA, row major. Red and green carry the horizontal and vertical source offset (128 is zero);
|
|
35
|
+
* blue carries how soft the backdrop is there: 255 in the middle, easing down to 0 at the edge,
|
|
36
|
+
* so the lens shows the backdrop sharp and lively along the rim and frosted under the text.
|
|
37
|
+
*/
|
|
38
|
+
data: Uint8ClampedArray;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const clamp = (value: number, min: number, max: number, ceil = Number.POSITIVE_INFINITY) =>
|
|
42
|
+
Math.min(Math.max(value, min), max, ceil);
|
|
43
|
+
|
|
44
|
+
/** How much of the legibility veil is left at the very edge of the rim. */
|
|
45
|
+
export const VEIL_FLOOR = 0.5;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Bevel width of a surface, in px. `"rim"`: a little more than its corner radius, so the bend
|
|
49
|
+
* reads as a drop. `"full"`: half the short side, the whole surface.
|
|
50
|
+
*/
|
|
51
|
+
export function bezelFor(
|
|
52
|
+
radius: number,
|
|
53
|
+
width: number,
|
|
54
|
+
height: number,
|
|
55
|
+
coverage: LensCoverage = "full",
|
|
56
|
+
): number {
|
|
57
|
+
const short = Math.min(width, height);
|
|
58
|
+
if (coverage === "full" || coverage === "dome") return Math.max(1, short / 2);
|
|
59
|
+
return clamp(radius * 1.2, 14, Math.max(8, short * 0.45), 56);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Largest source offset of the lens, as a fraction of the bevel width (`"rim"`). The profile falls
|
|
64
|
+
* at most 1.5 times its amplitude per bevel width, so 0.58 keeps the mapping `depth +
|
|
65
|
+
* offset(depth)` strictly increasing (slope 0.87 < 1). Above 1 it folds back on itself: several
|
|
66
|
+
* output pixels then sample the same source pixels in reverse order, and straight stripes break
|
|
67
|
+
* into a repeating sawtooth along the walls. That fold, not the map resolution, was the serrated
|
|
68
|
+
* edge of the first lenses.
|
|
69
|
+
*/
|
|
70
|
+
export const MAX_OFFSET = 0.58;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Full coverage is a dome over a superellipse radial coordinate: with `u = x / hx`, `v = y / hy`
|
|
74
|
+
* and `r = (|u|^N + |v|^N)^(1/N)`, a pixel is pulled towards the centre along the gradient of `r` in
|
|
75
|
+
* the normalised (u, v) plane (perpendicular to the contours of the superellipse) by `A (r / r_corner)^1.5`. The gradient is
|
|
76
|
+
* continuous everywhere except the exact centre, where the pull is zero, so there is no crease:
|
|
77
|
+
* a field built from the distance to the nearest edge, or from each axis on its own, has its
|
|
78
|
+
* pull flip direction along the diagonals of the shape and the glass reads as a four-faced
|
|
79
|
+
* pyramid. `N = 3` keeps the contours between an ellipse (a round drop) and a rounded rectangle
|
|
80
|
+
* (a slab). The slope of the pull is at most 2A / (r_corner^2 h) along an axis, so A is at most
|
|
81
|
+
* 0.55 of the smaller half size (fold-free), and at most {@link FULL_CAP} px so a large surface
|
|
82
|
+
* does not smear.
|
|
83
|
+
*/
|
|
84
|
+
export const FULL_FRACTION = 0.55;
|
|
85
|
+
/** Exponent of the superellipse radial coordinate of the full lens. */
|
|
86
|
+
export const FULL_EXPONENT = 3;
|
|
87
|
+
/** Largest offset of the full lens, in px. */
|
|
88
|
+
export const FULL_CAP = 24;
|
|
89
|
+
/**
|
|
90
|
+
* Shape of the two whole-surface profiles: the exponent `N` of the radial coordinate
|
|
91
|
+
* `(|u|^N + |v|^N)^(1/N)` (3: squarish, 2: round) and the power `p` of the pull `A t^p`, with
|
|
92
|
+
* `t` the radial coordinate over its value at the corners.
|
|
93
|
+
*/
|
|
94
|
+
export const WHOLE_SHAPES = {
|
|
95
|
+
full: { exponent: FULL_EXPONENT, power: 1.5 },
|
|
96
|
+
dome: { exponent: 2, power: 2 },
|
|
97
|
+
} as const;
|
|
98
|
+
|
|
99
|
+
/** Offset amplitude of the lens along each axis, in px. */
|
|
100
|
+
export function lensAmplitude(
|
|
101
|
+
width: number,
|
|
102
|
+
height: number,
|
|
103
|
+
bezel: number,
|
|
104
|
+
coverage: LensCoverage = "full",
|
|
105
|
+
cap: number = FULL_CAP,
|
|
106
|
+
): { x: number; y: number } {
|
|
107
|
+
if (coverage === "rim" || coverage === "crystal") {
|
|
108
|
+
const a = MAX_OFFSET * bezel;
|
|
109
|
+
return { x: a, y: a };
|
|
110
|
+
}
|
|
111
|
+
const a = Math.min((Math.min(width, height) / 2) * FULL_FRACTION, Math.max(cap, 1));
|
|
112
|
+
return { x: a, y: a };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** `scale` of the displacement filter: the offset is half of it (128 is neutral). */
|
|
116
|
+
export function scaleFor(
|
|
117
|
+
width: number,
|
|
118
|
+
height: number,
|
|
119
|
+
bezel: number,
|
|
120
|
+
coverage: LensCoverage = "full",
|
|
121
|
+
cap: number = FULL_CAP,
|
|
122
|
+
): number {
|
|
123
|
+
const { x, y } = lensAmplitude(width, height, bezel, coverage, cap);
|
|
124
|
+
return (2 * Math.max(x, y, 1) * 255) / 254;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const PROFILE_SAMPLES = 64;
|
|
128
|
+
|
|
129
|
+
let profile: Float32Array | undefined;
|
|
130
|
+
let quadratic: Float32Array | undefined;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* `"full"`: the table of the dome, `(x / half size)^2` from the centre (0) to the edge (1).
|
|
134
|
+
*
|
|
135
|
+
* `"rim"`: lateral displacement across the bevel, 1 at the outer edge and 0 at the inner one: `1 -
|
|
136
|
+
* smoothstep(x)`, with x running from the outer edge inwards. It starts flat (the outermost
|
|
137
|
+
* pixels show one clean shifted copy of the backdrop, like the rim of a drop), bends most at the
|
|
138
|
+
* middle of the bevel and eases to zero, so the lens meets the clear centre without a seam. Its
|
|
139
|
+
* slope never exceeds 1.5, which is what keeps {@link MAX_OFFSET} fold-free.
|
|
140
|
+
*/
|
|
141
|
+
export function lensProfile(coverage: LensCoverage = "rim"): Float32Array {
|
|
142
|
+
if (coverage !== "rim") {
|
|
143
|
+
if (quadratic) return quadratic;
|
|
144
|
+
quadratic = new Float32Array(PROFILE_SAMPLES).map((_, i) => (i / (PROFILE_SAMPLES - 1)) ** 2);
|
|
145
|
+
return quadratic;
|
|
146
|
+
}
|
|
147
|
+
if (profile) return profile;
|
|
148
|
+
const out = new Float32Array(PROFILE_SAMPLES);
|
|
149
|
+
for (let i = 0; i < PROFILE_SAMPLES; i++) {
|
|
150
|
+
const x = i / (PROFILE_SAMPLES - 1);
|
|
151
|
+
out[i] = 1 - x * x * (3 - 2 * x);
|
|
152
|
+
}
|
|
153
|
+
profile = out;
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Displacement field of a rounded rectangle, from its signed distance function: zero in the
|
|
159
|
+
* middle, and across the rim a source offset pointing into the shape whose length follows
|
|
160
|
+
* {@link lensProfile} and whose direction is the inward surface normal (straight edges pull
|
|
161
|
+
* along the axis, corners along the radius). Pure, so it runs anywhere.
|
|
162
|
+
*/
|
|
163
|
+
export function computeDisplacementField({
|
|
164
|
+
width,
|
|
165
|
+
height,
|
|
166
|
+
radius,
|
|
167
|
+
bezel,
|
|
168
|
+
coverage = "rim",
|
|
169
|
+
soft,
|
|
170
|
+
cap,
|
|
171
|
+
dpr = 1,
|
|
172
|
+
}: DisplacementMapOptions): DisplacementField {
|
|
173
|
+
const ratio = clamp(dpr, 1, 2);
|
|
174
|
+
const w = Math.max(1, width);
|
|
175
|
+
const h = Math.max(1, height);
|
|
176
|
+
const full = isWhole(coverage);
|
|
177
|
+
const shape = WHOLE_SHAPES[coverage === "dome" ? "dome" : "full"];
|
|
178
|
+
const corner = 2 ** (1 / shape.exponent);
|
|
179
|
+
const rim = clamp(full ? (soft ?? bezelFor(radius, w, h, "rim")) : bezel, 1, Math.min(w, h) / 2);
|
|
180
|
+
const r = clamp(radius, 0, Math.min(w, h) / 2);
|
|
181
|
+
const pw = Math.max(1, Math.round(w * ratio));
|
|
182
|
+
const ph = Math.max(1, Math.round(h * ratio));
|
|
183
|
+
// Rounding makes the real pixels-per-px a hair off `ratio`: positions use the effective ones.
|
|
184
|
+
const rx = pw / w;
|
|
185
|
+
const ry = ph / h;
|
|
186
|
+
const data = new Uint8ClampedArray(pw * ph * 4);
|
|
187
|
+
// Neutral everywhere (128, 128, 255, 255), then only the rim is computed: rows between the
|
|
188
|
+
// rims are visited at their two ends. Little-endian packing of R, G, B, A.
|
|
189
|
+
new Uint32Array(data.buffer).fill(0xffff8080);
|
|
190
|
+
const band = Math.min(pw, Math.ceil(rim * ratio) + 1);
|
|
191
|
+
const table = lensProfile("rim");
|
|
192
|
+
const last = table.length - 1;
|
|
193
|
+
const hx = w / 2;
|
|
194
|
+
const hy = h / 2;
|
|
195
|
+
const amp = lensAmplitude(w, h, bezel, coverage, cap);
|
|
196
|
+
const norm = Math.max(amp.x, amp.y, 1);
|
|
197
|
+
// The whole-surface terms of a column are the same on every row: computed once per column.
|
|
198
|
+
const columnPull = new Float64Array(full ? pw : 0);
|
|
199
|
+
const columnShape = new Float64Array(full ? pw : 0);
|
|
200
|
+
for (let i = 0; full && i < pw; i++) {
|
|
201
|
+
const px = (i + 0.5) / rx - hx;
|
|
202
|
+
const au = Math.abs(px) / hx;
|
|
203
|
+
columnPull[i] = Math.sign(px) * au ** (shape.exponent - 1);
|
|
204
|
+
columnShape[i] = au ** shape.exponent;
|
|
205
|
+
}
|
|
206
|
+
for (let j = 0; j < ph; j++) {
|
|
207
|
+
const py = (j + 0.5) / ry - hy;
|
|
208
|
+
const inner = Math.abs(py) < hy - rim;
|
|
209
|
+
const av = Math.abs(py) / hy;
|
|
210
|
+
const gv = full ? Math.sign(py) * av ** (shape.exponent - 1) : 0;
|
|
211
|
+
const vn = av ** shape.exponent;
|
|
212
|
+
for (let i = 0; i < pw; i++) {
|
|
213
|
+
if (!full && inner && i === band) i = Math.max(band, pw - band);
|
|
214
|
+
const px = (i + 0.5) / rx - hx;
|
|
215
|
+
const o = (j * pw + i) * 4;
|
|
216
|
+
if (full) {
|
|
217
|
+
const gx = columnPull[i] as number;
|
|
218
|
+
const g = Math.hypot(gx, gv);
|
|
219
|
+
const t = Math.min(((columnShape[i] as number) + vn) ** (1 / shape.exponent) / corner, 1);
|
|
220
|
+
const pull = g > 0 ? t ** shape.power / g : 0;
|
|
221
|
+
data[o] = 128 - gx * pull * 127 * (amp.x / norm);
|
|
222
|
+
data[o + 1] = 128 - gv * pull * 127 * (amp.y / norm);
|
|
223
|
+
data[o + 3] = 255;
|
|
224
|
+
if (inner && Math.abs(px) < hx - rim) {
|
|
225
|
+
data[o + 2] = 255;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const qx = Math.abs(px) - (hx - r);
|
|
230
|
+
const qy = Math.abs(py) - (hy - r);
|
|
231
|
+
const out = Math.hypot(Math.max(qx, 0), Math.max(qy, 0));
|
|
232
|
+
const depth = r - (out + Math.min(Math.max(qx, qy), 0));
|
|
233
|
+
let dx = 0;
|
|
234
|
+
let dy = 0;
|
|
235
|
+
let veil = 1;
|
|
236
|
+
if (full) {
|
|
237
|
+
if (depth > 0 && depth < rim) {
|
|
238
|
+
const x = depth / rim;
|
|
239
|
+
veil = x * x * (3 - 2 * x);
|
|
240
|
+
}
|
|
241
|
+
data[o + 2] = veil * 255;
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (depth > 0 && depth < rim) {
|
|
245
|
+
const x = depth / rim;
|
|
246
|
+
veil = x * x * (3 - 2 * x);
|
|
247
|
+
const at = (depth / rim) * last;
|
|
248
|
+
const lo = Math.floor(at);
|
|
249
|
+
const m =
|
|
250
|
+
(table[lo] as number) * (1 - (at - lo)) +
|
|
251
|
+
(table[Math.min(lo + 1, last)] as number) * (at - lo);
|
|
252
|
+
const sx = px < 0 ? -1 : 1;
|
|
253
|
+
const sy = py < 0 ? -1 : 1;
|
|
254
|
+
let nx = 0;
|
|
255
|
+
let ny = 0;
|
|
256
|
+
if (qx > 0 && qy > 0) {
|
|
257
|
+
nx = (sx * qx) / out;
|
|
258
|
+
ny = (sy * qy) / out;
|
|
259
|
+
} else if (qx > qy) nx = sx;
|
|
260
|
+
else ny = sy;
|
|
261
|
+
dx = -nx * m;
|
|
262
|
+
dy = -ny * m;
|
|
263
|
+
}
|
|
264
|
+
data[o] = 128 + dx * 127 * (amp.x / norm);
|
|
265
|
+
data[o + 1] = 128 + dy * 127 * (amp.y / norm);
|
|
266
|
+
data[o + 2] = veil * 255;
|
|
267
|
+
data[o + 3] = 255;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return { width: pw, height: ph, data };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const cache = new Map<string, string>();
|
|
274
|
+
const CACHE_SIZE = 16;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Displacement map for `feDisplacementMap`, as a PNG `data:` URI for `feImage`. Browser only
|
|
278
|
+
* (it rasterises on a canvas); results are kept in a small cache, so reopening a dialog of the
|
|
279
|
+
* same size costs nothing.
|
|
280
|
+
*/
|
|
281
|
+
export function createDisplacementMap(options: DisplacementMapOptions): string {
|
|
282
|
+
const key = [
|
|
283
|
+
options.width,
|
|
284
|
+
options.height,
|
|
285
|
+
options.radius,
|
|
286
|
+
options.bezel,
|
|
287
|
+
options.soft ?? 0,
|
|
288
|
+
// The offset is encoded relative to the largest one, so it only shows in the map below 1 px.
|
|
289
|
+
Math.min(
|
|
290
|
+
lensAmplitude(options.width, options.height, options.bezel, options.coverage, options.cap).x,
|
|
291
|
+
1,
|
|
292
|
+
),
|
|
293
|
+
options.dpr ?? 1,
|
|
294
|
+
]
|
|
295
|
+
.map((n) => Math.round(n * 100) / 100)
|
|
296
|
+
.join("x");
|
|
297
|
+
return cachedMap(`d${options.coverage ?? "rim"}${key}`, () => computeDisplacementField(options));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Rasterises a field to a PNG `data:` URI, kept in a small cache shared by every map. */
|
|
301
|
+
export function cachedMap(key: string, compute: () => DisplacementField): string {
|
|
302
|
+
const hit = cache.get(key);
|
|
303
|
+
if (hit) return hit;
|
|
304
|
+
const field = compute();
|
|
305
|
+
const canvas = document.createElement("canvas");
|
|
306
|
+
canvas.width = field.width;
|
|
307
|
+
canvas.height = field.height;
|
|
308
|
+
const context = canvas.getContext("2d");
|
|
309
|
+
if (!context) return "";
|
|
310
|
+
const image = context.createImageData(field.width, field.height);
|
|
311
|
+
image.data.set(field.data);
|
|
312
|
+
context.putImageData(image, 0, 0);
|
|
313
|
+
const uri = canvas.toDataURL("image/png");
|
|
314
|
+
cache.set(key, uri);
|
|
315
|
+
if (cache.size > CACHE_SIZE) cache.delete(cache.keys().next().value as string);
|
|
316
|
+
return uri;
|
|
317
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export {
|
|
2
|
+
CRYSTAL_GAIN,
|
|
3
|
+
CRYSTAL_MAX_OFFSET,
|
|
4
|
+
computeCrystalField,
|
|
5
|
+
crystalAmplitude,
|
|
6
|
+
crystalMargin,
|
|
7
|
+
crystalRadius,
|
|
8
|
+
crystalShape,
|
|
9
|
+
} from "./crystal-map.js";
|
|
10
|
+
export {
|
|
11
|
+
computeDisplacementField,
|
|
12
|
+
createDisplacementMap,
|
|
13
|
+
type DisplacementField,
|
|
14
|
+
type DisplacementMapOptions,
|
|
15
|
+
type LensCoverage,
|
|
16
|
+
lensProfile,
|
|
17
|
+
} from "./displacement-map.js";
|
|
18
|
+
export { LiquidGlass, startLiquidGlass } from "./liquid-glass.js";
|
|
19
|
+
export {
|
|
20
|
+
LIQUID_DEFAULTS,
|
|
21
|
+
LIQUID_MOTIONS,
|
|
22
|
+
LIQUID_PRESET_CLEAR,
|
|
23
|
+
LIQUID_PRESET_CRYSTAL,
|
|
24
|
+
LIQUID_PRESET_REGULAR,
|
|
25
|
+
LIQUID_PRESET_THICK,
|
|
26
|
+
LIQUID_PRESETS,
|
|
27
|
+
type LiquidGlassOptions,
|
|
28
|
+
type LiquidInteractive,
|
|
29
|
+
type LiquidMotion,
|
|
30
|
+
type LiquidPresetName,
|
|
31
|
+
type LiquidProfile,
|
|
32
|
+
liquidStyle,
|
|
33
|
+
liquidVars,
|
|
34
|
+
mergeLiquid,
|
|
35
|
+
} from "./liquid-options.js";
|
|
36
|
+
export { applyGlassRefraction, type GlassRefractionOptions } from "./refraction.js";
|
|
37
|
+
export {
|
|
38
|
+
computeSpecularField,
|
|
39
|
+
createSpecularMap,
|
|
40
|
+
type SpecularMapOptions,
|
|
41
|
+
} from "./specular-map.js";
|
|
42
|
+
export { supportsBackdropRefraction } from "./support.js";
|
|
43
|
+
export { useGlassRefraction } from "./use-glass-refraction.js";
|