@bloxwap/hig 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/chart/README.md +13 -2
- package/chart/dist/altimeter-burst.d.ts +4 -0
- package/chart/dist/altimeter-burst.js +43 -0
- package/chart/dist/altimeter-chart.d.ts +52 -0
- package/chart/dist/altimeter-chart.js +275 -0
- package/chart/dist/altimeter-model.d.ts +43 -0
- package/chart/dist/altimeter-model.js +148 -0
- package/chart/dist/altimeter-targets.d.ts +33 -0
- package/chart/dist/altimeter-targets.js +88 -0
- package/chart/dist/bitcoin-price.d.ts +8 -0
- package/chart/dist/bitcoin-price.js +41 -0
- package/chart/dist/champions-league.d.ts +19 -0
- package/chart/dist/champions-league.js +104 -0
- package/chart/dist/index.d.ts +6 -0
- package/chart/dist/index.js +5 -0
- package/chart/dist/outcome-chart.d.ts +37 -0
- package/chart/dist/outcome-chart.js +212 -0
- package/chart/dist/outcome-model.d.ts +43 -0
- package/chart/dist/outcome-model.js +71 -0
- package/chart/dist/plus-grid.d.ts +6 -0
- package/chart/dist/plus-grid.js +29 -0
- package/contracts/README.md +1 -1
- package/contracts/dist/avatar.d.ts +68 -0
- package/contracts/dist/avatar.js +93 -0
- package/contracts/dist/confetti.d.ts +174 -0
- package/contracts/dist/confetti.js +138 -0
- package/contracts/dist/decimal.d.ts +7 -0
- package/contracts/dist/decimal.js +35 -0
- package/contracts/dist/index.d.ts +29 -2
- package/contracts/dist/index.js +123 -4
- package/contracts/dist/invariants.d.ts +17 -0
- package/contracts/dist/invariants.js +24 -0
- package/contracts/dist/messages.d.ts +56 -2
- package/contracts/dist/messages.js +20 -2
- package/contracts/dist/number-flow.d.ts +90 -0
- package/contracts/dist/number-flow.js +150 -0
- package/contracts/dist/qr.d.ts +151 -0
- package/contracts/dist/qr.js +581 -0
- package/contracts/dist/toast.d.ts +168 -0
- package/contracts/dist/toast.js +171 -0
- package/feedback/README.md +1 -1
- package/feedback/dist/contract.js +1 -1
- package/feedback/dist/react.js +2 -4
- package/native/README.md +6 -1
- package/native/dist/account-block.d.ts +10 -1
- package/native/dist/account-block.js +17 -7
- package/native/dist/animated-number-model.d.ts +41 -1
- package/native/dist/animated-number-model.js +201 -23
- package/native/dist/animated-number.d.ts +39 -1
- package/native/dist/animated-number.js +161 -16
- package/native/dist/announce.d.ts +2 -0
- package/native/dist/announce.js +10 -0
- package/native/dist/asset-balance-row.d.ts +3 -1
- package/native/dist/asset-balance-row.js +15 -4
- package/native/dist/assets-block.d.ts +115 -0
- package/native/dist/assets-block.js +87 -0
- package/native/dist/async-result.d.ts +2 -0
- package/native/dist/async-result.js +11 -0
- package/native/dist/avatar-frame.d.ts +29 -0
- package/native/dist/avatar-frame.js +56 -0
- package/native/dist/avatar.d.ts +21 -0
- package/native/dist/avatar.js +31 -0
- package/native/dist/bar-mark.d.ts +2 -0
- package/native/dist/bar-mark.js +4 -2
- package/native/dist/bar-pnl.js +2 -1
- package/native/dist/blocks.d.ts +6 -1
- package/native/dist/blocks.js +5 -0
- package/native/dist/brand.js +2 -1
- package/native/dist/button.d.ts +7 -2
- package/native/dist/button.js +13 -3
- package/native/dist/checkbox.js +2 -1
- package/native/dist/confetti.d.ts +33 -0
- package/native/dist/confetti.js +143 -0
- package/native/dist/confirm-block.d.ts +98 -0
- package/native/dist/confirm-block.js +48 -0
- package/native/dist/data.js +2 -0
- package/native/dist/explainer-block.d.ts +91 -0
- package/native/dist/explainer-block.js +34 -0
- package/native/dist/field.d.ts +5 -1
- package/native/dist/field.js +27 -6
- package/native/dist/gesture-sheet-model.d.ts +50 -0
- package/native/dist/gesture-sheet-model.js +105 -0
- package/native/dist/gesture-sheet-next.d.ts +82 -0
- package/native/dist/gesture-sheet-next.js +315 -0
- package/native/dist/gesture-sheet.d.ts +5 -1
- package/native/dist/gesture-sheet.js +13 -6
- package/native/dist/icons.d.ts +9 -6
- package/native/dist/icons.js +25 -24
- package/native/dist/language-block.d.ts +10 -1
- package/native/dist/language-block.js +11 -4
- package/native/dist/list-group.d.ts +3 -1
- package/native/dist/list-group.js +26 -13
- package/native/dist/manage-position-block.d.ts +8 -1
- package/native/dist/manage-position-block.js +7 -2
- package/native/dist/market-row.js +19 -8
- package/native/dist/navigation.js +5 -3
- package/native/dist/number-format.d.ts +6 -0
- package/native/dist/number-format.js +9 -0
- package/native/dist/pickers.d.ts +8 -0
- package/native/dist/pickers.js +3 -3
- package/native/dist/position-block.d.ts +5 -5
- package/native/dist/preset-group.d.ts +31 -0
- package/native/dist/preset-group.js +39 -0
- package/native/dist/preset-picker-block.d.ts +107 -0
- package/native/dist/preset-picker-block.js +43 -0
- package/native/dist/profile-block.d.ts +10 -1
- package/native/dist/profile-block.js +8 -3
- package/native/dist/provider.d.ts +5 -2
- package/native/dist/provider.js +27 -5
- package/native/dist/qr-code.d.ts +33 -0
- package/native/dist/qr-code.js +29 -0
- package/native/dist/radio-group.d.ts +19 -7
- package/native/dist/radio-group.js +39 -10
- package/native/dist/reward-block.d.ts +10 -7
- package/native/dist/reward-block.js +9 -4
- package/native/dist/screen-header.d.ts +13 -3
- package/native/dist/screen-header.js +12 -4
- package/native/dist/select.js +4 -2
- package/native/dist/selection.d.ts +2 -1
- package/native/dist/selection.js +1 -0
- package/native/dist/setting-switch.d.ts +1 -1
- package/native/dist/setting-switch.js +7 -3
- package/native/dist/slider.js +5 -1
- package/native/dist/sound-haptics-block.js +1 -1
- package/native/dist/status.d.ts +41 -9
- package/native/dist/status.js +34 -17
- package/native/dist/sticky-header.js +9 -1
- package/native/dist/swap-amount-card.d.ts +57 -0
- package/native/dist/swap-amount-card.js +31 -0
- package/native/dist/swap-panel-block.d.ts +9 -3
- package/native/dist/swap-panel-block.js +59 -33
- package/native/dist/swap-parts.d.ts +3 -10
- package/native/dist/swap-parts.js +22 -24
- package/native/dist/swap-shared.d.ts +2 -2
- package/native/dist/swap-shared.js +10 -6
- package/native/dist/swap-views.js +1 -1
- package/native/dist/target-block.d.ts +7 -1
- package/native/dist/target-block.js +13 -6
- package/native/dist/toast.d.ts +47 -0
- package/native/dist/toast.js +386 -0
- package/native/dist/token-action-block.d.ts +10 -1
- package/native/dist/token-action-block.js +9 -4
- package/native/dist/trade-history-list-block.js +7 -4
- package/native/dist/wallet-address-core.js +6 -3
- package/native-fonts/README.md +1 -1
- package/og-fonts/README.md +1 -1
- package/package.json +111 -2
- package/react/README.md +3 -1
- package/react/dist/animated-number-flow.d.ts +72 -0
- package/react/dist/animated-number-flow.js +316 -0
- package/react/dist/animated-number-model.d.ts +18 -0
- package/react/dist/animated-number-model.js +75 -5
- package/react/dist/animated-number.d.ts +45 -1
- package/react/dist/animated-number.js +137 -6
- package/react/dist/asset-balance-row.d.ts +3 -1
- package/react/dist/asset-balance-row.js +10 -4
- package/react/dist/async-result.d.ts +2 -0
- package/react/dist/async-result.js +11 -0
- package/react/dist/avatar.d.ts +25 -0
- package/react/dist/avatar.js +50 -0
- package/react/dist/blocks/account-block.d.ts +5 -1
- package/react/dist/blocks/account-block.js +11 -6
- package/react/dist/blocks/assets-block.d.ts +106 -0
- package/react/dist/blocks/assets-block.js +135 -0
- package/react/dist/blocks/confirm-block.d.ts +91 -0
- package/react/dist/blocks/confirm-block.js +31 -0
- package/react/dist/blocks/explainer-block.d.ts +80 -0
- package/react/dist/blocks/explainer-block.js +20 -0
- package/react/dist/blocks/install-command.js +3 -1
- package/react/dist/blocks/language-block.d.ts +5 -1
- package/react/dist/blocks/language-block.js +4 -3
- package/react/dist/blocks/manage-position-block.d.ts +3 -1
- package/react/dist/blocks/manage-position-block.js +2 -2
- package/react/dist/blocks/market-row.js +8 -3
- package/react/dist/blocks/notifications-block.d.ts +3 -1
- package/react/dist/blocks/notifications-block.js +2 -2
- package/react/dist/blocks/pickers.d.ts +9 -0
- package/react/dist/blocks/pickers.js +4 -2
- package/react/dist/blocks/position-block.d.ts +3 -3
- package/react/dist/blocks/preset-picker-block.d.ts +85 -0
- package/react/dist/blocks/preset-picker-block.js +19 -0
- package/react/dist/blocks/profile-block.d.ts +5 -1
- package/react/dist/blocks/profile-block.js +12 -13
- package/react/dist/blocks/reward-block.d.ts +13 -8
- package/react/dist/blocks/reward-block.js +7 -4
- package/react/dist/blocks/screen-header.d.ts +10 -2
- package/react/dist/blocks/screen-header.js +7 -4
- package/react/dist/blocks/setting-switch.js +6 -3
- package/react/dist/blocks/signals-block.d.ts +3 -1
- package/react/dist/blocks/signals-block.js +4 -3
- package/react/dist/blocks/sound-haptics-block.d.ts +3 -1
- package/react/dist/blocks/sound-haptics-block.js +2 -2
- package/react/dist/blocks/sticky-header.d.ts +14 -4
- package/react/dist/blocks/sticky-header.js +68 -8
- package/react/dist/blocks/swap/settings.js +2 -1
- package/react/dist/blocks/swap/shared.d.ts +22 -22
- package/react/dist/blocks/swap/shared.js +30 -28
- package/react/dist/blocks/swap-panel-block.d.ts +10 -3
- package/react/dist/blocks/swap-panel-block.js +71 -36
- package/react/dist/blocks/target-block.d.ts +7 -1
- package/react/dist/blocks/target-block.js +10 -4
- package/react/dist/blocks/token-action-block.d.ts +5 -1
- package/react/dist/blocks/token-action-block.js +2 -2
- package/react/dist/blocks/trade-history-detail-block.d.ts +3 -1
- package/react/dist/blocks/trade-history-detail-block.js +2 -2
- package/react/dist/blocks/trade-history-list-block.d.ts +3 -1
- package/react/dist/blocks/trade-history-list-block.js +7 -4
- package/react/dist/blocks.d.ts +5 -1
- package/react/dist/blocks.js +4 -0
- package/react/dist/brand.js +2 -1
- package/react/dist/button.d.ts +9 -3
- package/react/dist/button.js +8 -3
- package/react/dist/confetti.d.ts +27 -0
- package/react/dist/confetti.js +240 -0
- package/react/dist/data.js +8 -1
- package/react/dist/field.d.ts +6 -1
- package/react/dist/field.js +18 -9
- package/react/dist/gesture-sheet.d.ts +15 -1
- package/react/dist/gesture-sheet.js +53 -8
- package/react/dist/list-group.js +11 -7
- package/react/dist/navigation.js +11 -3
- package/react/dist/overlay.d.ts +28 -3
- package/react/dist/overlay.js +29 -3
- package/react/dist/preset-group.d.ts +30 -0
- package/react/dist/preset-group.js +13 -0
- package/react/dist/provider.d.ts +2 -2
- package/react/dist/provider.js +23 -6
- package/react/dist/qr-code.d.ts +32 -0
- package/react/dist/qr-code.js +28 -0
- package/react/dist/select.js +10 -3
- package/react/dist/selection.d.ts +13 -5
- package/react/dist/selection.js +13 -8
- package/react/dist/slider.js +5 -1
- package/react/dist/status.d.ts +14 -10
- package/react/dist/status.js +17 -9
- package/react/dist/styles.css +238 -163
- package/react/dist/toast.d.ts +19 -12
- package/react/dist/toast.js +29 -7
- package/react/dist/wallet-address-core.d.ts +8 -4
- package/react/dist/wallet-address-core.js +10 -5
- package/tokens/README.md +2 -2
- package/tokens/dist/index.d.ts +16 -0
- package/tokens/dist/index.js +16 -0
- package/tokens/dist/manifest.json +8 -0
- package/tokens/dist/tokens.css +6 -0
package/README.md
CHANGED
|
@@ -20,10 +20,10 @@ import {Button} from '@bloxwap/hig/react/button';
|
|
|
20
20
|
| Entry | Contents |
|
|
21
21
|
| --- | --- |
|
|
22
22
|
| `@bloxwap/hig/tokens` | Tokens, `resolvePrimaryColor`, `./tokens/css`, `./tokens/manifest` ([README](tokens/README.md)) |
|
|
23
|
-
| `@bloxwap/hig/contracts` | Types, formatting, feedback, announcements and
|
|
23
|
+
| `@bloxwap/hig/contracts` | Types, formatting, feedback, announcements, messages, the generated avatar face and the QR encoder ([README](contracts/README.md)) |
|
|
24
24
|
| `@bloxwap/hig/react/*` | Web components, blocks, `./react/css` and `./react/css/base` ([README](react/README.md)) |
|
|
25
25
|
| `@bloxwap/hig/native/*` | React Native components ([README](native/README.md)) |
|
|
26
|
-
| `@bloxwap/hig/chart` | `PriceChart` and `chartTheme` for `@bloxwap/chart` ([README](chart/README.md)) |
|
|
26
|
+
| `@bloxwap/hig/chart` | `AltimeterChart` live price chart and `OutcomeChart`; `PriceChart` and `chartTheme` for `@bloxwap/chart` ([README](chart/README.md)) |
|
|
27
27
|
| `@bloxwap/hig/feedback/*` | Opt-in web and native sound/haptic adapters ([README](feedback/README.md)) |
|
|
28
28
|
| `@bloxwap/hig/native-fonts` | Static TTFs for React Native and Expo ([README](native-fonts/README.md)) |
|
|
29
29
|
| `@bloxwap/hig/og-fonts` | Satori / `next/og` faces and the Node loader ([README](og-fonts/README.md)) |
|
package/chart/README.md
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
# @bloxwap/hig/chart
|
|
2
2
|
|
|
3
|
-
Version 0.
|
|
3
|
+
Version 0.8.1, an entry of `@bloxwap/hig`. `AltimeterChart` is the Bloxwap live chart on its
|
|
4
|
+
own: a plus grid and price trail that flow left at 50px per second, a price ladder centered on the current price,
|
|
5
|
+
and the current price at the trail's tip. Pass `price` or a `feed`; with neither it shows live Bitcoin through
|
|
6
|
+
`bitcoinPrice`. Optional `targets` (`AltimeterTarget`: `{id, price, label?, color?}`) add full-width horizontal dotted lines
|
|
7
|
+
and colored text labels at the chart's trailing edge that glide to the side of each line facing the current price.
|
|
8
|
+
Labels pass behind the current-price pill where they overlap. Streaming price updates retarget from the current position and forward velocity without overshoot; the canvas reuses its font and measured ladder widths between frames.
|
|
9
|
+
Each label shows its title on the left and formatted target price on the right, such as `Take Profit $100.02`.
|
|
10
|
+
Offscreen labels pin and stack at the top or bottom in price order while their lines stay hidden. Labels follow their lines on the same frame and move aside only for other labels or the chart edges. Only the above/below offset animates (260ms), reversing from its current offset; collision constraints apply afterward so labels stay separated throughout.
|
|
11
|
+
Set `dgen` to shake the price pill and burst particles outward from it when its price crosses a target;
|
|
12
|
+
it defaults to off and respects reduced motion. `OutcomeChart` shows a question with two to ten
|
|
13
|
+
outcomes live, as one flowing trail each on a 0 to 100% ladder, ending in a stroked capsule with the label and chance; with no `outcomes` or `feed` it shows the
|
|
14
|
+
UEFA Champions League winner market through `championsLeagueWinner`. The entry is also the HIG adapter for [`@bloxwap/chart`](https://github.com/bloxwap/chart): `PriceChart` (a
|
|
4
15
|
`<figure>` with a required text `summary`, lazy-loading `@bloxwap/chart/react` inside `Suspense`) and
|
|
5
16
|
`chartTheme` (HIG token colors in sRGB for the canvas). Web only; React Native uses labeled readouts and lists
|
|
6
17
|
from `@bloxwap/hig/native/data`.
|
|
@@ -10,7 +21,7 @@ npm install @bloxwap/hig @bloxwap/chart@^0.5.0
|
|
|
10
21
|
```
|
|
11
22
|
|
|
12
23
|
```tsx
|
|
13
|
-
import {PriceChart, chartTheme} from '@bloxwap/hig/chart';
|
|
24
|
+
import {AltimeterChart, OutcomeChart, PriceChart, chartTheme} from '@bloxwap/hig/chart';
|
|
14
25
|
```
|
|
15
26
|
|
|
16
27
|
Needs `react` and `@bloxwap/chart` `^0.3.0 || ^0.4.0 || ^0.5.0` (0.5.0 is the tested baseline); `HIGProvider` from `@bloxwap/hig/react/provider` supplies the
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Coalesce noisy crossings into at most one burst per cooldown, with no queued effects. */
|
|
2
|
+
export declare const ALTIMETER_BURST_COOLDOWN_MS = 600;
|
|
3
|
+
/** A bounded, compositor-driven burst radiating from the price pill. No frame loop or motion dependency. */
|
|
4
|
+
export declare function burstAtPill(pill: HTMLElement, layer: HTMLElement, color: string): () => void;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { tokens } from '@bloxwap/hig/tokens';
|
|
2
|
+
/** Coalesce noisy crossings into at most one burst per cooldown, with no queued effects. */
|
|
3
|
+
export const ALTIMETER_BURST_COOLDOWN_MS = 600;
|
|
4
|
+
const easing = `cubic-bezier(${tokens.easing.standard.join(',')})`;
|
|
5
|
+
/** A bounded, compositor-driven burst radiating from the price pill. No frame loop or motion dependency. */
|
|
6
|
+
export function burstAtPill(pill, layer, color) {
|
|
7
|
+
if (typeof pill.animate !== 'function')
|
|
8
|
+
return () => { };
|
|
9
|
+
// Measure once, before the shake, so the origin follows the pill's actual size and position.
|
|
10
|
+
const width = pill.offsetWidth, height = pill.offsetHeight;
|
|
11
|
+
const x = pill.offsetLeft + width / 2, y = pill.offsetTop + height / 2;
|
|
12
|
+
const burst = document.createElement('span');
|
|
13
|
+
burst.dataset.higAltimeterBurst = '';
|
|
14
|
+
Object.assign(burst.style, { position: 'absolute', left: `${x}px`, top: `${y}px`, color });
|
|
15
|
+
layer.append(burst);
|
|
16
|
+
const animations = [];
|
|
17
|
+
animations.push(pill.animate([
|
|
18
|
+
{ transform: 'translate(0, 0)' }, { transform: 'translate(-4px, -2px)', offset: .18 },
|
|
19
|
+
{ transform: 'translate(3px, 1px)', offset: .36 }, { transform: 'translate(-2px, 1px)', offset: .58 },
|
|
20
|
+
{ transform: 'translate(1px, -1px)', offset: .78 }, { transform: 'translate(0, 0)' },
|
|
21
|
+
], { duration: tokens.motion.base, easing: 'linear' }));
|
|
22
|
+
const ring = document.createElement('span');
|
|
23
|
+
Object.assign(ring.style, { position: 'absolute', left: `${-width / 2}px`, top: `${-height / 2}px`, width: `${width}px`, height: `${height}px`, border: '2px solid currentColor', borderRadius: `${tokens.radius.pill}px`, boxSizing: 'border-box' });
|
|
24
|
+
burst.append(ring);
|
|
25
|
+
animations.push(ring.animate([{ transform: 'scale(.95)', opacity: .65 }, { transform: 'scale(1.6)', opacity: 0 }], { duration: tokens.motion.base, easing, fill: 'forwards' }));
|
|
26
|
+
for (let i = 0; i < 24; i++) {
|
|
27
|
+
const spark = document.createElement('span'), angle = i * Math.PI * 2 / 24, distance = 36 + (i % 5) * 13, size = 3 + i % 3;
|
|
28
|
+
const dx = Math.cos(angle), dy = Math.sin(angle);
|
|
29
|
+
// Begin just inside the capsule, then travel outward from its perimeter.
|
|
30
|
+
const startX = dx * Math.max(0, width / 2 - 3), startY = dy * Math.max(0, height / 2 - 3);
|
|
31
|
+
Object.assign(spark.style, { position: 'absolute', left: `${-size / 2}px`, top: `${-size / 2}px`, width: `${size}px`, height: `${size}px`, background: 'currentColor', borderRadius: '50%' });
|
|
32
|
+
burst.append(spark);
|
|
33
|
+
animations.push(spark.animate([
|
|
34
|
+
{ transform: `translate(${startX}px, ${startY}px)`, opacity: 1 },
|
|
35
|
+
{ opacity: 1, offset: .65 },
|
|
36
|
+
{ transform: `translate(${startX + dx * distance}px, ${startY + dy * distance}px)`, opacity: 0 },
|
|
37
|
+
], { duration: tokens.motion.base * 2, easing, fill: 'forwards' }));
|
|
38
|
+
}
|
|
39
|
+
const clear = () => { for (const animation of animations)
|
|
40
|
+
animation.cancel(); burst.remove(); };
|
|
41
|
+
void Promise.all(animations.map(animation => animation.finished)).then(clear, clear);
|
|
42
|
+
return clear;
|
|
43
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type AltimeterTick } from './altimeter-model.js';
|
|
3
|
+
import { type AltimeterFeed } from './bitcoin-price.js';
|
|
4
|
+
import { type AltimeterTarget } from './altimeter-targets.js';
|
|
5
|
+
export interface AltimeterChartProps extends Omit<React.HTMLAttributes<HTMLElement>, 'children'> {
|
|
6
|
+
/** Text alternative shown as the figure's caption, for example what the chart tracks and where the price comes from. */
|
|
7
|
+
summary: string;
|
|
8
|
+
/** Latest price, owned by the host. `null` reads as waiting for a price. Omit it to use `feed`. */
|
|
9
|
+
price?: number | null;
|
|
10
|
+
/** Live price source, used only while `price` is omitted. Defaults to `bitcoinPrice`. Keep its identity stable. */
|
|
11
|
+
feed?: AltimeterFeed;
|
|
12
|
+
/** Chart height in pixels. Default 320. */
|
|
13
|
+
height?: number;
|
|
14
|
+
/** Format of the current price and the ladder. Defaults to US dollars with the digits the ladder needs. */
|
|
15
|
+
priceOptions?: Intl.NumberFormatOptions;
|
|
16
|
+
/** Horizontal dotted levels with optional right-aligned labels and CSS colors. Offscreen labels pin and stack at the edges. Use stable, unique IDs. */
|
|
17
|
+
targets?: readonly AltimeterTarget[];
|
|
18
|
+
/** DGEN: shake the live pill and burst particles on target crossings. Off by default; suppressed by reduced motion. */
|
|
19
|
+
dgen?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface AltimeterFrame {
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
now: number;
|
|
25
|
+
/** Grid offset in (-50, 0]. */
|
|
26
|
+
phase: number;
|
|
27
|
+
ticks: readonly AltimeterTick[];
|
|
28
|
+
/** Eased price held at the vertical center; `NaN` while waiting. */
|
|
29
|
+
display: number;
|
|
30
|
+
anchor: number;
|
|
31
|
+
spacing: number;
|
|
32
|
+
colors: {
|
|
33
|
+
grid: string;
|
|
34
|
+
ladder: string;
|
|
35
|
+
line: string;
|
|
36
|
+
};
|
|
37
|
+
label: (price: number) => string;
|
|
38
|
+
/** Resolved font stack for the ladder labels. Defaults to the mono family by name. */
|
|
39
|
+
font?: string;
|
|
40
|
+
/** Optional text-width cache for a live renderer; invalidated when the resolved font changes. */
|
|
41
|
+
labelWidth?: (text: string) => number;
|
|
42
|
+
/** Vertical text bounds of target labels; colliding ladder numbers yield to them. */
|
|
43
|
+
targetLabelBounds?: readonly (readonly [number, number])[];
|
|
44
|
+
}
|
|
45
|
+
/** Paints one frame (grid, ladder, trail) and returns the x where the trail ends and the price pill begins. */
|
|
46
|
+
export declare function drawAltimeter(ctx: CanvasRenderingContext2D, frame: AltimeterFrame): number;
|
|
47
|
+
/**
|
|
48
|
+
* The Bloxwap live chart on its own: a grid of plus marks and a price trail that flow left at 50px per second, a
|
|
49
|
+
* price ladder that keeps the current price at the vertical center, and the current price at the trail's tip. It
|
|
50
|
+
* accepts optional target lines and opt-in DGEN crossing feedback. Web only.
|
|
51
|
+
*/
|
|
52
|
+
export declare function AltimeterChart({ summary, price, feed, height, priceOptions, targets, dgen, style, ...props }: AltimeterChartProps): React.JSX.Element;
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { tokens } from '@bloxwap/hig/tokens';
|
|
5
|
+
import * as en from '@bloxwap/hig/contracts/messages';
|
|
6
|
+
import { useHIG, useColors } from '@bloxwap/hig/react/provider';
|
|
7
|
+
import { AnimatedNumberReadout } from '@bloxwap/hig/react/animated-number';
|
|
8
|
+
import { ALTIMETER_FLOW_PX, ALTIMETER_SAMPLE_MS, ALTIMETER_TICK_PX, gridPhase, labelDecimalsFor, ladderPrices, priceTween, smoothTrail, tickSpacingFor, trailPoints } from './altimeter-model.js';
|
|
9
|
+
import { drawPlusGrid, monoFallback, monoFont } from './plus-grid.js';
|
|
10
|
+
import { bitcoinPrice } from './bitcoin-price.js';
|
|
11
|
+
import { targetLayout, targetCrossings, validTargets } from './altimeter-targets.js';
|
|
12
|
+
import { ALTIMETER_BURST_COOLDOWN_MS, burstAtPill } from './altimeter-burst.js';
|
|
13
|
+
/** Inset of the ladder's tick marks from the chart edges. */
|
|
14
|
+
const PAD = 18;
|
|
15
|
+
/** Length of a ladder tick mark, and the gap between the pill and the trailing edge. */
|
|
16
|
+
const EDGE = 8;
|
|
17
|
+
const NO_TARGETS = [];
|
|
18
|
+
const transparent = (color) => /^#[0-9a-f]{6}$/i.test(color) ? `${color}00` : 'transparent';
|
|
19
|
+
const usable = (price) => price != null && Number.isFinite(price) && price > 0;
|
|
20
|
+
const formatter = (locale, options, decimals) => new Intl.NumberFormat(locale, { style: 'currency', currency: 'USD', minimumFractionDigits: decimals, maximumFractionDigits: decimals, ...options });
|
|
21
|
+
/** Paints one frame (grid, ladder, trail) and returns the x where the trail ends and the price pill begins. */
|
|
22
|
+
export function drawAltimeter(ctx, frame) {
|
|
23
|
+
const { width, height, display, spacing } = frame;
|
|
24
|
+
ctx.clearRect(0, 0, width, height);
|
|
25
|
+
ctx.lineJoin = 'round';
|
|
26
|
+
drawPlusGrid(ctx, width, height, frame.phase, frame.colors.grid);
|
|
27
|
+
const right = width - PAD;
|
|
28
|
+
if (!(display > 0) || !(spacing > 0))
|
|
29
|
+
return right - 100;
|
|
30
|
+
const center = height / 2, yFor = (price) => center - ((price - display) / spacing) * ALTIMETER_TICK_PX;
|
|
31
|
+
const reach = (center / ALTIMETER_TICK_PX + 1) * spacing, labels = ladderPrices(frame.anchor, spacing, display - reach, display + reach).map(price => ({ y: yFor(price), text: frame.label(price) }));
|
|
32
|
+
const font = `13px ${frame.font ?? monoFallback}`;
|
|
33
|
+
// Reassigning the canvas font can synchronously resolve document styles, even if the font is unchanged.
|
|
34
|
+
if (ctx.font !== font)
|
|
35
|
+
ctx.font = font;
|
|
36
|
+
ctx.textAlign = 'right';
|
|
37
|
+
ctx.textBaseline = 'middle';
|
|
38
|
+
ctx.globalAlpha = .75;
|
|
39
|
+
ctx.strokeStyle = frame.colors.ladder;
|
|
40
|
+
ctx.fillStyle = frame.colors.ladder;
|
|
41
|
+
let widest = 0;
|
|
42
|
+
ctx.beginPath();
|
|
43
|
+
for (const { y, text } of labels) {
|
|
44
|
+
ctx.moveTo(PAD - EDGE, y);
|
|
45
|
+
ctx.lineTo(PAD, y);
|
|
46
|
+
ctx.moveTo(right, y);
|
|
47
|
+
ctx.lineTo(right + EDGE, y);
|
|
48
|
+
if (!frame.targetLabelBounds?.some(([top, bottom]) => y + 7 > top - 2 && y - 7 < bottom + 2))
|
|
49
|
+
ctx.fillText(text, right - 14, y);
|
|
50
|
+
widest = Math.max(widest, frame.labelWidth?.(text) ?? ctx.measureText(text).width);
|
|
51
|
+
}
|
|
52
|
+
ctx.stroke();
|
|
53
|
+
ctx.globalAlpha = 1;
|
|
54
|
+
// The trail stops short of the ladder labels; the pill covers the rest of the row.
|
|
55
|
+
const trailEnd = Math.max(PAD + 40, right - Math.max(100, Math.ceil(widest) + 22));
|
|
56
|
+
const points = trailPoints(frame.ticks, frame.now, PAD, trailEnd, yFor), last = points.at(-1);
|
|
57
|
+
if (!last)
|
|
58
|
+
return trailEnd;
|
|
59
|
+
const fade = ctx.createLinearGradient(PAD, 0, trailEnd, 0);
|
|
60
|
+
fade.addColorStop(0, transparent(frame.colors.line));
|
|
61
|
+
fade.addColorStop(1, frame.colors.line);
|
|
62
|
+
ctx.strokeStyle = fade;
|
|
63
|
+
ctx.lineWidth = 2.5;
|
|
64
|
+
ctx.beginPath();
|
|
65
|
+
smoothTrail(points, ctx);
|
|
66
|
+
// From the newest sample to the live price; level end tangents keep the tail inside both prices.
|
|
67
|
+
const dx = trailEnd - last[0];
|
|
68
|
+
if (dx > 0)
|
|
69
|
+
ctx.bezierCurveTo(last[0] + dx / 3, last[1], trailEnd - dx / 3, center, trailEnd, center);
|
|
70
|
+
else
|
|
71
|
+
ctx.lineTo(trailEnd, center);
|
|
72
|
+
ctx.stroke();
|
|
73
|
+
return trailEnd;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The Bloxwap live chart on its own: a grid of plus marks and a price trail that flow left at 50px per second, a
|
|
77
|
+
* price ladder that keeps the current price at the vertical center, and the current price at the trail's tip. It
|
|
78
|
+
* accepts optional target lines and opt-in DGEN crossing feedback. Web only.
|
|
79
|
+
*/
|
|
80
|
+
export function AltimeterChart({ summary, price, feed = bitcoinPrice, height = 320, priceOptions, targets = NO_TARGETS, dgen = false, style, ...props }) {
|
|
81
|
+
const { locale, messages, reducedMotion } = useHIG();
|
|
82
|
+
const colors = useColors();
|
|
83
|
+
const box = useRef(null), canvas = useRef(null), pill = useRef(null);
|
|
84
|
+
const targetRows = useRef(new Map()), targetLabels = useRef(new Map()), effects = useRef(null);
|
|
85
|
+
const levels = useMemo(() => validTargets(targets), [targets]);
|
|
86
|
+
const crossings = useRef(targetCrossings()), cancelBurst = useRef(undefined);
|
|
87
|
+
const [stale, setStale] = useState(false);
|
|
88
|
+
const controlled = price !== undefined;
|
|
89
|
+
const latest = useRef(usable(price) ? price : null);
|
|
90
|
+
const [reading, setReading] = useState(() => usable(price) ? price : null);
|
|
91
|
+
const digits = reading == null ? 2 : labelDecimalsFor(tickSpacingFor(reading), reading);
|
|
92
|
+
const readingOptions = useMemo(() => ({ style: 'currency', currency: 'USD', minimumFractionDigits: digits, maximumFractionDigits: digits, ...priceOptions }), [digits, JSON.stringify(priceOptions ?? {})]);
|
|
93
|
+
const targetFormat = useMemo(() => new Intl.NumberFormat(locale, readingOptions), [locale, readingOptions]);
|
|
94
|
+
// The frame loop reads the newest props without restarting, so a re-render never clears the trail.
|
|
95
|
+
const settings = useRef({ colors, locale, priceOptions, optionsKey: '', reducedMotion, levels, dgen });
|
|
96
|
+
useEffect(() => { settings.current = { colors, locale, priceOptions, optionsKey: JSON.stringify(priceOptions ?? {}), reducedMotion, levels, dgen }; if (controlled)
|
|
97
|
+
latest.current = usable(price) ? price : null; });
|
|
98
|
+
useEffect(() => { if (!dgen || reducedMotion) {
|
|
99
|
+
cancelBurst.current?.();
|
|
100
|
+
cancelBurst.current = undefined;
|
|
101
|
+
} }, [dgen, reducedMotion]);
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
if (controlled) {
|
|
104
|
+
setStale(false);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
let alive = true;
|
|
108
|
+
let deadline;
|
|
109
|
+
latest.current = null;
|
|
110
|
+
setStale(false);
|
|
111
|
+
const expire = () => { if (alive) {
|
|
112
|
+
latest.current = null;
|
|
113
|
+
setStale(true);
|
|
114
|
+
} };
|
|
115
|
+
deadline = setTimeout(expire, 30_000);
|
|
116
|
+
const stop = feed(next => { if (alive && usable(next)) {
|
|
117
|
+
latest.current = next;
|
|
118
|
+
setStale(false);
|
|
119
|
+
clearTimeout(deadline);
|
|
120
|
+
deadline = setTimeout(expire, 30_000);
|
|
121
|
+
} }, expire);
|
|
122
|
+
return () => { alive = false; clearTimeout(deadline); stop(); };
|
|
123
|
+
}, [controlled, feed]);
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
const surface = canvas.current, container = box.current;
|
|
126
|
+
if (!surface || !container)
|
|
127
|
+
return;
|
|
128
|
+
let context = null;
|
|
129
|
+
try {
|
|
130
|
+
context = surface.getContext('2d');
|
|
131
|
+
}
|
|
132
|
+
catch { /* No canvas: the caption and the price reading remain. */ }
|
|
133
|
+
let width = container.clientWidth, raf = 0, sampled = -Infinity, shown;
|
|
134
|
+
let lastFrame = -Infinity, lastBurst = -Infinity;
|
|
135
|
+
crossings.current.reset();
|
|
136
|
+
const observer = typeof ResizeObserver === 'undefined' ? undefined : new ResizeObserver(entries => { width = entries[0].contentRect.width; });
|
|
137
|
+
observer?.observe(container);
|
|
138
|
+
// The canvas cannot read CSS variables, so it takes the font stack the box resolved, again once fonts load.
|
|
139
|
+
let font = monoFallback;
|
|
140
|
+
const widths = new Map();
|
|
141
|
+
const readFont = () => { const resolved = typeof getComputedStyle === 'function' ? getComputedStyle(container).fontFamily : ''; if (resolved)
|
|
142
|
+
font = resolved; widths.clear(); };
|
|
143
|
+
readFont();
|
|
144
|
+
document.fonts?.ready.then(readFont).catch(() => { });
|
|
145
|
+
const motion = priceTween();
|
|
146
|
+
let display = NaN;
|
|
147
|
+
const layout = targetLayout();
|
|
148
|
+
let ticks = [], anchor = NaN, spacing = 0, format, formatKey = '';
|
|
149
|
+
const labels = new Map();
|
|
150
|
+
const show = (next) => { if (next !== shown) {
|
|
151
|
+
shown = next;
|
|
152
|
+
setReading(next);
|
|
153
|
+
} };
|
|
154
|
+
const step = (now) => {
|
|
155
|
+
raf = requestAnimationFrame(step);
|
|
156
|
+
const { colors, locale, priceOptions, optionsKey, reducedMotion, levels, dgen } = settings.current, target = latest.current;
|
|
157
|
+
const resumed = now - lastFrame > 2000;
|
|
158
|
+
lastFrame = now;
|
|
159
|
+
if (resumed) {
|
|
160
|
+
crossings.current.reset();
|
|
161
|
+
cancelBurst.current?.();
|
|
162
|
+
}
|
|
163
|
+
if (target == null) {
|
|
164
|
+
display = NaN;
|
|
165
|
+
motion.reset();
|
|
166
|
+
ticks = [];
|
|
167
|
+
sampled = -Infinity;
|
|
168
|
+
show(null);
|
|
169
|
+
crossings.current.reset();
|
|
170
|
+
cancelBurst.current?.();
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
display = motion.update(target, now, reducedMotion || resumed);
|
|
174
|
+
// Re-anchor the ladder before a drifting price runs past its ticks.
|
|
175
|
+
if (!(spacing > 0) || Math.abs(display - anchor) / spacing > 40) {
|
|
176
|
+
spacing = tickSpacingFor(display);
|
|
177
|
+
anchor = Math.round(display / spacing) * spacing;
|
|
178
|
+
labels.clear();
|
|
179
|
+
widths.clear();
|
|
180
|
+
}
|
|
181
|
+
const key = `${locale}|${optionsKey}|${labelDecimalsFor(spacing, display)}`;
|
|
182
|
+
if (key !== formatKey || !format) {
|
|
183
|
+
formatKey = key;
|
|
184
|
+
format = formatter(locale, priceOptions, labelDecimalsFor(spacing, display));
|
|
185
|
+
labels.clear();
|
|
186
|
+
widths.clear();
|
|
187
|
+
}
|
|
188
|
+
if (now - sampled >= ALTIMETER_SAMPLE_MS) {
|
|
189
|
+
const keep = (Math.max(width, 0) / ALTIMETER_FLOW_PX) * 1000 + 2000;
|
|
190
|
+
// Start flat, and again after a pause (a hidden tab stops the frames), so no line bridges the gap.
|
|
191
|
+
if (ticks.length === 0 || now - sampled > 2000)
|
|
192
|
+
ticks = [{ t: now - keep, p: display }];
|
|
193
|
+
ticks.push({ t: now, p: display });
|
|
194
|
+
while (ticks.length > 2 && ticks[1].t < now - keep)
|
|
195
|
+
ticks.shift();
|
|
196
|
+
sampled = now;
|
|
197
|
+
show(target);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const positions = layout.update(levels, display, spacing, height, now, reducedMotion || resumed);
|
|
201
|
+
for (const level of levels) {
|
|
202
|
+
const row = targetRows.current.get(level.id);
|
|
203
|
+
if (!row)
|
|
204
|
+
continue;
|
|
205
|
+
const label = targetLabels.current.get(level.id);
|
|
206
|
+
const position = positions.get(level.id);
|
|
207
|
+
if (!position) {
|
|
208
|
+
row.style.visibility = 'hidden';
|
|
209
|
+
if (label)
|
|
210
|
+
label.style.visibility = 'hidden';
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const { anchor, labelTop, pinned } = position;
|
|
214
|
+
row.style.visibility = pinned ? 'hidden' : 'visible';
|
|
215
|
+
row.style.transform = `translateY(${anchor}px)`;
|
|
216
|
+
row.style.borderTopColor = pinned ? 'transparent' : 'currentColor';
|
|
217
|
+
if (pinned) {
|
|
218
|
+
if (row.dataset.higAltimeterPinned !== pinned)
|
|
219
|
+
row.dataset.higAltimeterPinned = pinned;
|
|
220
|
+
}
|
|
221
|
+
else if (row.dataset.higAltimeterPinned)
|
|
222
|
+
delete row.dataset.higAltimeterPinned;
|
|
223
|
+
// Line tracking is immediate. Only the side offset eases, before collision and edge constraints.
|
|
224
|
+
if (label && labelTop !== undefined) {
|
|
225
|
+
label.style.visibility = 'visible';
|
|
226
|
+
label.style.transform = `translateY(${labelTop}px)`;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const crossed = target == null ? [] : crossings.current.update(display, levels);
|
|
230
|
+
let trailEnd;
|
|
231
|
+
// Keep DOM writes together; the canvas and labels use the same resolved geometry.
|
|
232
|
+
if (context && width > 0) {
|
|
233
|
+
const ratio = window.devicePixelRatio || 1, w = Math.max(1, Math.round(width * ratio)), h = Math.max(1, Math.round(height * ratio));
|
|
234
|
+
if (surface.width !== w)
|
|
235
|
+
surface.width = w;
|
|
236
|
+
if (surface.height !== h)
|
|
237
|
+
surface.height = h;
|
|
238
|
+
context.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
239
|
+
const targetLabelBounds = [];
|
|
240
|
+
for (const { labelTop } of positions.values())
|
|
241
|
+
if (labelTop !== undefined)
|
|
242
|
+
targetLabelBounds.push([labelTop, labelTop + 18]);
|
|
243
|
+
trailEnd = drawAltimeter(context, { width, height, now, phase: reducedMotion ? 0 : gridPhase(now), ticks, display, anchor, spacing, font, targetLabelBounds, colors: { grid: colors.nested, ladder: colors.muted, line: colors.action }, label: value => { let text = labels.get(value); if (text === undefined) {
|
|
244
|
+
text = format.format(value);
|
|
245
|
+
labels.set(value, text);
|
|
246
|
+
} return text; }, labelWidth: text => { let size = widths.get(text); if (size === undefined) {
|
|
247
|
+
size = context.measureText(text).width;
|
|
248
|
+
widths.set(text, size);
|
|
249
|
+
} return size; } });
|
|
250
|
+
}
|
|
251
|
+
if (pill.current && trailEnd !== undefined) {
|
|
252
|
+
const left = `${trailEnd}px`;
|
|
253
|
+
if (pill.current.style.left !== left)
|
|
254
|
+
pill.current.style.left = left;
|
|
255
|
+
const hit = crossed.at(-1);
|
|
256
|
+
if (hit && dgen && !reducedMotion && now - lastBurst >= ALTIMETER_BURST_COOLDOWN_MS && effects.current) {
|
|
257
|
+
lastBurst = now;
|
|
258
|
+
cancelBurst.current?.();
|
|
259
|
+
cancelBurst.current = burstAtPill(pill.current, effects.current, hit.color ?? colors.action);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
raf = requestAnimationFrame(step);
|
|
264
|
+
return () => { cancelAnimationFrame(raf); observer?.disconnect(); cancelBurst.current?.(); };
|
|
265
|
+
}, [height, controlled, controlled ? undefined : feed]);
|
|
266
|
+
return _jsxs("figure", { ...props, style: { margin: 0, inlineSize: '100%', minInlineSize: 0, ...style }, children: [_jsx("figcaption", { style: { marginBlockEnd: tokens.space.sm }, children: summary }), _jsxs("div", { ref: box, style: { position: 'relative', height, overflow: 'hidden', borderRadius: tokens.radius.xl, background: colors.background, fontFamily: monoFont }, children: [_jsx("canvas", { ref: canvas, "aria-hidden": true, style: { position: 'absolute', inset: 0, width: '100%', height: '100%' } }), _jsx("div", { "aria-hidden": true, style: { position: 'absolute', inset: 0, pointerEvents: 'none' }, children: levels.map(level => _jsxs(React.Fragment, { children: [_jsx("div", { ref: node => { if (node)
|
|
267
|
+
targetRows.current.set(level.id, node);
|
|
268
|
+
else
|
|
269
|
+
targetRows.current.delete(level.id); }, "data-hig-altimeter-target": level.id, style: { position: 'absolute', top: 0, left: 0, right: 0, visibility: 'hidden', borderTop: '2px dotted currentColor', color: level.color ?? colors.action } }), level.label && _jsxs("span", { ref: node => { if (node)
|
|
270
|
+
targetLabels.current.set(level.id, node);
|
|
271
|
+
else
|
|
272
|
+
targetLabels.current.delete(level.id); }, "data-hig-altimeter-target-label": level.id, dir: 'ltr', title: `${level.label} ${targetFormat.format(level.price)}`, style: { position: 'absolute', right: EDGE, top: 0, visibility: 'hidden', transform: 'translateY(0px)', maxWidth: `calc(100% - ${EDGE * 2}px)`, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', textAlign: 'right', color: level.color ?? colors.action, fontSize: 12, fontWeight: 600, lineHeight: '18px', fontVariantNumeric: 'tabular-nums' }, children: [_jsx("bdi", { style: { textTransform: 'capitalize' }, children: level.label }), ' ', _jsx("bdi", { children: targetFormat.format(level.price) })] })] }, level.id)) }), reading == null
|
|
273
|
+
? _jsx("p", { role: 'status', style: { position: 'absolute', inset: 0, margin: 0, display: 'grid', placeItems: 'center', color: colors.muted, fontFamily: 'var(--hig-font-sans, inherit)' }, children: stale ? (messages.stale ?? en.stale) : (messages.loadingChart ?? en.loadingChart) })
|
|
274
|
+
: _jsx("span", { ref: pill, "data-hig-altimeter-price": true, style: { position: 'absolute', right: EDGE, top: height / 2 - 18, height: 36, minWidth: 110, boxSizing: 'border-box', display: 'flex', alignItems: 'center', justifyContent: 'center', paddingInline: 10, border: `1px solid ${colors.action}`, borderRadius: tokens.radius.pill, background: colors.background, color: colors.action, fontSize: 16, fontWeight: 600, lineHeight: 1, fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap' }, children: _jsx(AnimatedNumberReadout, { value: reading, options: readingOptions }) }), _jsx("div", { ref: effects, "aria-hidden": true, style: { position: 'absolute', inset: 0, pointerEvents: 'none' } })] }), levels.length > 0 && _jsx("ul", { className: 'hig-sr-only', children: levels.map(level => _jsxs("li", { children: [level.label ? `${level.label}: ` : '', targetFormat.format(level.price)] }, level.id)) })] });
|
|
275
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** One sampled price. `t` is in milliseconds on the chart's clock. */
|
|
2
|
+
export type AltimeterTick = {
|
|
3
|
+
t: number;
|
|
4
|
+
p: number;
|
|
5
|
+
};
|
|
6
|
+
/** Grid tile size, and the pixels the grid and trail travel each second. */
|
|
7
|
+
export declare const ALTIMETER_FLOW_PX = 50;
|
|
8
|
+
/** Vertical pixels between ladder ticks. */
|
|
9
|
+
export declare const ALTIMETER_TICK_PX = 44;
|
|
10
|
+
/** Trail sampling cadence, so the line keeps flowing on a quiet market. */
|
|
11
|
+
export declare const ALTIMETER_SAMPLE_MS = 200;
|
|
12
|
+
/** Duration of the price tween. */
|
|
13
|
+
export declare const ALTIMETER_EASE_MS = 300;
|
|
14
|
+
/** Half the length of a grid mark's arm. */
|
|
15
|
+
export declare const ALTIMETER_MARK_ARM = 3;
|
|
16
|
+
/** Price between two ladder ticks: about one basis point, rounded to whole cents or a 1/2/5 step below a cent. */
|
|
17
|
+
export declare function tickSpacingFor(price: number): number;
|
|
18
|
+
/** Fraction digits that tell neighboring ladder ticks apart: at least two, at least four below $1, at most 20. */
|
|
19
|
+
export declare function labelDecimalsFor(spacing: number, price: number): number;
|
|
20
|
+
/** Horizontal offset of the grid at `now`, within (-50, 0]: one tile left per second. */
|
|
21
|
+
export declare function gridPhase(now: number): number;
|
|
22
|
+
/** The tween's cubic-bezier(0.42, 0, 0.58, 1) at time fraction `t`. */
|
|
23
|
+
export declare function easeInOut(t: number): number;
|
|
24
|
+
/** A 300ms monotone Hermite tween: retarget from the current position and retain forward velocity without overshoot. */
|
|
25
|
+
export declare function priceTween(): {
|
|
26
|
+
reset(): void;
|
|
27
|
+
update(target: number, now: number, snap?: boolean): number;
|
|
28
|
+
};
|
|
29
|
+
export type AltimeterPoint = readonly [number, number];
|
|
30
|
+
export interface AltimeterPathSink {
|
|
31
|
+
moveTo(x: number, y: number): void;
|
|
32
|
+
lineTo(x: number, y: number): void;
|
|
33
|
+
bezierCurveTo(c1x: number, c1y: number, c2x: number, c2y: number, x: number, y: number): void;
|
|
34
|
+
}
|
|
35
|
+
/** Draws a monotone cubic (Fritsch-Carlson) through `points`, so the trail never overshoots a sampled price. */
|
|
36
|
+
export declare function smoothTrail(points: readonly AltimeterPoint[], sink: AltimeterPathSink): void;
|
|
37
|
+
/**
|
|
38
|
+
* Trail points for the samples still on screen. The newest price sits at `trailEnd`; a sample `age` ms old sits
|
|
39
|
+
* `age * 50 / 1000` px to its left. The first point is interpolated onto the left edge so the line enters cleanly.
|
|
40
|
+
*/
|
|
41
|
+
export declare function trailPoints(ticks: readonly AltimeterTick[], now: number, trailStart: number, trailEnd: number, yFor: (price: number) => number): AltimeterPoint[];
|
|
42
|
+
/** Ladder prices from `anchor` in steps of `spacing` that cover `low` to `high`. */
|
|
43
|
+
export declare function ladderPrices(anchor: number, spacing: number, low: number, high: number): number[];
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// Geometry and easing for AltimeterChart, shared with the Bloxwap app's live chart: a 50px grid and trail that flow
|
|
2
|
+
// left one tile per second, a price ladder with 44px between ticks, and a 300ms ease-in-out price tween.
|
|
3
|
+
/** Grid tile size, and the pixels the grid and trail travel each second. */
|
|
4
|
+
export const ALTIMETER_FLOW_PX = 50;
|
|
5
|
+
/** Vertical pixels between ladder ticks. */
|
|
6
|
+
export const ALTIMETER_TICK_PX = 44;
|
|
7
|
+
/** Trail sampling cadence, so the line keeps flowing on a quiet market. */
|
|
8
|
+
export const ALTIMETER_SAMPLE_MS = 200;
|
|
9
|
+
/** Duration of the price tween. */
|
|
10
|
+
export const ALTIMETER_EASE_MS = 300;
|
|
11
|
+
/** Half the length of a grid mark's arm. */
|
|
12
|
+
export const ALTIMETER_MARK_ARM = 3;
|
|
13
|
+
/** Price between two ladder ticks: about one basis point, rounded to whole cents or a 1/2/5 step below a cent. */
|
|
14
|
+
export function tickSpacingFor(price) {
|
|
15
|
+
if (!Number.isFinite(price) || price <= 0)
|
|
16
|
+
return 0.01;
|
|
17
|
+
const raw = price / 10_000;
|
|
18
|
+
if (raw >= 0.01)
|
|
19
|
+
return Math.ceil(raw * 100) / 100;
|
|
20
|
+
const magnitude = Math.pow(10, Math.floor(Math.log10(raw))), normalized = raw / magnitude;
|
|
21
|
+
return (normalized <= 1 ? 1 : normalized <= 2 ? 2 : normalized <= 5 ? 5 : 10) * magnitude;
|
|
22
|
+
}
|
|
23
|
+
/** Fraction digits that tell neighboring ladder ticks apart: at least two, at least four below $1, at most 20. */
|
|
24
|
+
export function labelDecimalsFor(spacing, price) {
|
|
25
|
+
const subDollar = price > 0 && price < 1 ? 4 : 0;
|
|
26
|
+
if (!Number.isFinite(spacing) || spacing <= 0)
|
|
27
|
+
return Math.max(2, subDollar);
|
|
28
|
+
return Math.max(2, Math.min(20, Math.max(0, Math.ceil(-Math.log10(spacing) - 1e-9))), subDollar);
|
|
29
|
+
}
|
|
30
|
+
/** Horizontal offset of the grid at `now`, within (-50, 0]: one tile left per second. */
|
|
31
|
+
export function gridPhase(now) {
|
|
32
|
+
const phase = -((now * ALTIMETER_FLOW_PX) / 1000) % ALTIMETER_FLOW_PX;
|
|
33
|
+
return phase === 0 ? 0 : phase;
|
|
34
|
+
}
|
|
35
|
+
/** The tween's cubic-bezier(0.42, 0, 0.58, 1) at time fraction `t`. */
|
|
36
|
+
export function easeInOut(t) {
|
|
37
|
+
if (t <= 0)
|
|
38
|
+
return 0;
|
|
39
|
+
if (t >= 1)
|
|
40
|
+
return 1;
|
|
41
|
+
const p1x = 0.42, p2x = 0.58;
|
|
42
|
+
let u = t;
|
|
43
|
+
for (let i = 0; i < 6; i++) {
|
|
44
|
+
const v = 1 - u, x = 3 * v * v * u * p1x + 3 * v * u * u * p2x + u * u * u, dx = 3 * v * v * p1x + 6 * v * u * (p2x - p1x) + 3 * u * u * (1 - p2x);
|
|
45
|
+
if (Math.abs(dx) < 1e-6)
|
|
46
|
+
break;
|
|
47
|
+
u = Math.min(1, Math.max(0, u - (x - t) / dx));
|
|
48
|
+
}
|
|
49
|
+
// The y control points are 0 and 1, so only the last two Bernstein terms remain.
|
|
50
|
+
return 3 * (1 - u) * u * u + u * u * u;
|
|
51
|
+
}
|
|
52
|
+
/** A 300ms monotone Hermite tween: retarget from the current position and retain forward velocity without overshoot. */
|
|
53
|
+
export function priceTween() {
|
|
54
|
+
let from = NaN, to = NaN, start = 0, tangent = 0;
|
|
55
|
+
return {
|
|
56
|
+
reset() { from = to = NaN; tangent = 0; },
|
|
57
|
+
update(target, now, snap = false) {
|
|
58
|
+
if (snap || !Number.isFinite(to)) {
|
|
59
|
+
from = to = target;
|
|
60
|
+
start = now;
|
|
61
|
+
tangent = 0;
|
|
62
|
+
return target;
|
|
63
|
+
}
|
|
64
|
+
const t = Math.max(0, Math.min(1, (now - start) / ALTIMETER_EASE_MS)), delta = to - from;
|
|
65
|
+
const value = from + delta * t * t * (3 - 2 * t) + tangent * t * (1 - t) * (1 - t);
|
|
66
|
+
if (target !== to) {
|
|
67
|
+
const velocity = (delta * 6 * t * (1 - t) + tangent * (1 - t) * (1 - 3 * t)) / ALTIMETER_EASE_MS;
|
|
68
|
+
from = value;
|
|
69
|
+
to = target;
|
|
70
|
+
start = now;
|
|
71
|
+
const next = to - from, direction = Math.sign(next);
|
|
72
|
+
// A reversal starts from rest. A nearer endpoint limits the tangent so it cannot overshoot a target.
|
|
73
|
+
tangent = direction * Math.min(3 * Math.abs(next), Math.max(0, direction * velocity * ALTIMETER_EASE_MS));
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const slope = (points, i) => { const dx = points[i + 1][0] - points[i][0]; return dx === 0 ? 0 : (points[i + 1][1] - points[i][1]) / dx; };
|
|
80
|
+
const tangent = (points, i) => {
|
|
81
|
+
if (i === 0)
|
|
82
|
+
return slope(points, 0);
|
|
83
|
+
if (i === points.length - 1)
|
|
84
|
+
return slope(points, points.length - 2);
|
|
85
|
+
const before = slope(points, i - 1), after = slope(points, i);
|
|
86
|
+
return before * after <= 0 ? 0 : (before + after) / 2;
|
|
87
|
+
};
|
|
88
|
+
/** Draws a monotone cubic (Fritsch-Carlson) through `points`, so the trail never overshoots a sampled price. */
|
|
89
|
+
export function smoothTrail(points, sink) {
|
|
90
|
+
const count = points.length;
|
|
91
|
+
if (count === 0)
|
|
92
|
+
return;
|
|
93
|
+
sink.moveTo(points[0][0], points[0][1]);
|
|
94
|
+
if (count === 1)
|
|
95
|
+
return;
|
|
96
|
+
if (count === 2) {
|
|
97
|
+
sink.lineTo(points[1][0], points[1][1]);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const tangents = points.map((_, i) => tangent(points, i));
|
|
101
|
+
for (let i = 0; i < count - 1; i++) {
|
|
102
|
+
const delta = slope(points, i);
|
|
103
|
+
if (delta === 0) {
|
|
104
|
+
tangents[i] = 0;
|
|
105
|
+
tangents[i + 1] = 0;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const a = tangents[i] / delta, b = tangents[i + 1] / delta, size = a * a + b * b;
|
|
109
|
+
if (size > 9) {
|
|
110
|
+
const scale = 3 / Math.sqrt(size);
|
|
111
|
+
tangents[i] = scale * a * delta;
|
|
112
|
+
tangents[i + 1] = scale * b * delta;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
for (let i = 0; i < count - 1; i++) {
|
|
116
|
+
const [x0, y0] = points[i], [x1, y1] = points[i + 1], dx = x1 - x0;
|
|
117
|
+
sink.bezierCurveTo(x0 + dx / 3, y0 + (tangents[i] * dx) / 3, x1 - dx / 3, y1 - (tangents[i + 1] * dx) / 3, x1, y1);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Trail points for the samples still on screen. The newest price sits at `trailEnd`; a sample `age` ms old sits
|
|
122
|
+
* `age * 50 / 1000` px to its left. The first point is interpolated onto the left edge so the line enters cleanly.
|
|
123
|
+
*/
|
|
124
|
+
export function trailPoints(ticks, now, trailStart, trailEnd, yFor) {
|
|
125
|
+
const xFor = (t) => trailEnd - ((now - t) / 1000) * ALTIMETER_FLOW_PX;
|
|
126
|
+
const edge = now - ((trailEnd - trailStart) / ALTIMETER_FLOW_PX) * 1000;
|
|
127
|
+
const points = [];
|
|
128
|
+
for (let i = 0; i < ticks.length; i++) {
|
|
129
|
+
const tick = ticks[i];
|
|
130
|
+
if (tick.t < edge)
|
|
131
|
+
continue;
|
|
132
|
+
if (points.length === 0 && i > 0) {
|
|
133
|
+
const previous = ticks[i - 1], span = tick.t - previous.t, fraction = span > 0 ? (edge - previous.t) / span : 0;
|
|
134
|
+
points.push([trailStart, yFor(previous.p + fraction * (tick.p - previous.p))]);
|
|
135
|
+
}
|
|
136
|
+
points.push([xFor(tick.t), yFor(tick.p)]);
|
|
137
|
+
}
|
|
138
|
+
return points;
|
|
139
|
+
}
|
|
140
|
+
/** Ladder prices from `anchor` in steps of `spacing` that cover `low` to `high`. */
|
|
141
|
+
export function ladderPrices(anchor, spacing, low, high) {
|
|
142
|
+
if (!(spacing > 0) || !Number.isFinite(anchor) || !(high >= low))
|
|
143
|
+
return [];
|
|
144
|
+
const first = Math.ceil((low - anchor) / spacing), last = Math.floor((high - anchor) / spacing), prices = [];
|
|
145
|
+
for (let i = first; i <= last && prices.length < 200; i++)
|
|
146
|
+
prices.push(anchor + i * spacing);
|
|
147
|
+
return prices;
|
|
148
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** A host-owned price level. Plain labels align right; colors accept any CSS color, including variables. */
|
|
2
|
+
export interface AltimeterTarget {
|
|
3
|
+
/** Stable, unique identity. Changing the price re-arms this target without firing an effect. */
|
|
4
|
+
id: string;
|
|
5
|
+
price: number;
|
|
6
|
+
/** Title shown before the formatted target price, with capitalized words. Omit for a line without text. */
|
|
7
|
+
label?: string;
|
|
8
|
+
/** Defaults to the chart's primary accent. */
|
|
9
|
+
color?: string;
|
|
10
|
+
}
|
|
11
|
+
/** Keep the first valid target for each ID. Invalid prices must never enter chart geometry. */
|
|
12
|
+
export declare function validTargets(targets: readonly AltimeterTarget[]): AltimeterTarget[];
|
|
13
|
+
export interface AltimeterTargetPosition {
|
|
14
|
+
/** Actual line position, which may be outside the chart. */
|
|
15
|
+
y: number;
|
|
16
|
+
/** Row position clamped to the chart so pinned labels never inherit a huge offscreen transform. */
|
|
17
|
+
anchor: number;
|
|
18
|
+
/** Label's top in chart coordinates, after stacking. */
|
|
19
|
+
labelTop?: number;
|
|
20
|
+
pinned: 'top' | 'bottom' | null;
|
|
21
|
+
}
|
|
22
|
+
/** Labels stay inside the chart and share the chart capsule layout's collision avoidance. */
|
|
23
|
+
export declare function placeTargets(targets: readonly AltimeterTarget[], display: number, spacing: number, height: number, sideOffsets?: ReadonlyMap<string, number>): Map<string, AltimeterTargetPosition>;
|
|
24
|
+
/** Follow the line exactly; animate only its above/below offset, then resolve collisions and edge pinning. */
|
|
25
|
+
export declare function targetLayout(): {
|
|
26
|
+
reset(): void;
|
|
27
|
+
update(targets: readonly AltimeterTarget[], display: number, spacing: number, height: number, now: number, snap?: boolean): Map<string, AltimeterTargetPosition>;
|
|
28
|
+
};
|
|
29
|
+
/** Tracks displayed prices, so the effect coincides with the line reaching the live pill. */
|
|
30
|
+
export declare function targetCrossings(): {
|
|
31
|
+
reset(): void;
|
|
32
|
+
update(price: number, targets: readonly AltimeterTarget[]): AltimeterTarget[];
|
|
33
|
+
};
|