@wardic/core 1.2.0 → 1.3.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/dist/BaseValue.d.ts.map +1 -1
- package/dist/BaseValue.js +2 -0
- package/dist/BaseValue.js.map +1 -1
- package/dist/Batch.d.ts +6 -0
- package/dist/Batch.d.ts.map +1 -1
- package/dist/Batch.js +40 -10
- package/dist/Batch.js.map +1 -1
- package/dist/DefaultTransition.d.ts +21 -12
- package/dist/DefaultTransition.d.ts.map +1 -1
- package/dist/DefaultTransition.js +43 -2
- package/dist/DefaultTransition.js.map +1 -1
- package/dist/FrameLoop.d.ts +5 -1
- package/dist/FrameLoop.d.ts.map +1 -1
- package/dist/FrameLoop.js.map +1 -1
- package/dist/Machine.d.ts +3 -0
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +4 -3
- package/dist/Machine.js.map +1 -1
- package/dist/Spring.d.ts +7 -0
- package/dist/Spring.d.ts.map +1 -1
- package/dist/Spring.js +16 -0
- package/dist/Spring.js.map +1 -1
- package/dist/UnitScale.d.ts +39 -0
- package/dist/UnitScale.d.ts.map +1 -0
- package/dist/UnitScale.js +45 -0
- package/dist/UnitScale.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/frameLoop.js +5 -5
- package/dist/internal/frameLoop.js.map +1 -1
- package/dist/unstable/BatchCalls.d.ts +150 -0
- package/dist/unstable/BatchCalls.d.ts.map +1 -0
- package/dist/unstable/BatchCalls.js +319 -0
- package/dist/unstable/BatchCalls.js.map +1 -0
- package/dist/unstable/Config.d.ts +11 -0
- package/dist/unstable/Config.d.ts.map +1 -1
- package/dist/unstable/Config.js.map +1 -1
- package/dist/unstable/FrameEffects.d.ts +59 -0
- package/dist/unstable/FrameEffects.d.ts.map +1 -0
- package/dist/unstable/FrameEffects.js +142 -0
- package/dist/unstable/FrameEffects.js.map +1 -0
- package/dist/unstable/FrameTime.d.ts +1 -4
- package/dist/unstable/FrameTime.d.ts.map +1 -1
- package/dist/unstable/FrameTime.js +1 -4
- package/dist/unstable/FrameTime.js.map +1 -1
- package/dist/unstable/Imperative.d.ts +292 -0
- package/dist/unstable/Imperative.d.ts.map +1 -0
- package/dist/unstable/Imperative.js +922 -0
- package/dist/unstable/Imperative.js.map +1 -0
- package/dist/unstable/RetargetLegs.d.ts +2 -1
- package/dist/unstable/RetargetLegs.d.ts.map +1 -1
- package/dist/unstable/RetargetLegs.js +23 -15
- package/dist/unstable/RetargetLegs.js.map +1 -1
- package/dist/unstable/Spine.d.ts +17 -14
- package/dist/unstable/Spine.d.ts.map +1 -1
- package/dist/unstable/Spine.js +33 -16
- package/dist/unstable/Spine.js.map +1 -1
- package/package.json +1 -1
- package/src/BaseValue.ts +2 -0
- package/src/Batch.ts +62 -9
- package/src/DefaultTransition.ts +53 -2
- package/src/FrameLoop.ts +6 -2
- package/src/Machine.ts +7 -3
- package/src/Spring.ts +16 -0
- package/src/UnitScale.ts +56 -0
- package/src/index.ts +1 -0
- package/src/internal/frameLoop.ts +5 -5
- package/src/unstable/BatchCalls.ts +419 -0
- package/src/unstable/Config.ts +17 -0
- package/src/unstable/FrameEffects.ts +172 -0
- package/src/unstable/FrameTime.ts +1 -5
- package/src/unstable/Imperative.ts +1232 -0
- package/src/unstable/RetargetLegs.ts +20 -11
- package/src/unstable/Spine.ts +48 -21
package/src/Batch.ts
CHANGED
|
@@ -34,6 +34,7 @@ import * as Machine from "./Machine.ts"
|
|
|
34
34
|
import type * as Playback from "./Playback.ts"
|
|
35
35
|
import * as Scalar from "./Scalar.ts"
|
|
36
36
|
import * as Spring from "./Spring.ts"
|
|
37
|
+
import * as UnitScale from "./UnitScale.ts"
|
|
37
38
|
import { dual } from "./internal/dual.ts"
|
|
38
39
|
import { tickOf, writableOf } from "./internal/playhead.ts"
|
|
39
40
|
import { resolveKeyframes } from "./internal/keyframes.ts"
|
|
@@ -113,6 +114,12 @@ export interface Options<S> {
|
|
|
113
114
|
/** Called once a frame, after every member's write: Motion's `onUpdate`, for the whole Batch. */
|
|
114
115
|
// ADR-0069 decision 16: never per member.
|
|
115
116
|
readonly onFrame?: () => void
|
|
117
|
+
/**
|
|
118
|
+
* The Environment's unit scale: each key's springs' rest thresholds read at the units it gives for the
|
|
119
|
+
* key and its leg's distance, a threshold a spring is given kept as it is. Motion's own when left out.
|
|
120
|
+
*/
|
|
121
|
+
// ADR-0069 decision 8 (#883): an Environment in metres gives `x`, `y` and `z` 100.
|
|
122
|
+
readonly unitScale?: UnitScale.Rule | undefined
|
|
116
123
|
}
|
|
117
124
|
|
|
118
125
|
/** A Batch of Subjects `S`: built by `make`, never as a literal. */
|
|
@@ -188,6 +195,8 @@ interface State<S> {
|
|
|
188
195
|
readonly stride: number
|
|
189
196
|
/** Each key's index, by its name. */
|
|
190
197
|
readonly indexOfKey: Readonly<Record<string, number>>
|
|
198
|
+
/** Each key's units for its rest thresholds, by its name and a leg's distance (#883). */
|
|
199
|
+
readonly unitScale: UnitScale.Rule
|
|
191
200
|
capacity: number
|
|
192
201
|
/** How many slots have ever been entered: every slot below is a member's. */
|
|
193
202
|
used: number
|
|
@@ -423,6 +432,7 @@ export const make = <S>(options: Options<S>): Batch<S> => {
|
|
|
423
432
|
indices[key] = k
|
|
424
433
|
return indices
|
|
425
434
|
}),
|
|
435
|
+
unitScale: UndefinedOr.match(options.unitScale, { onUndefined: () => UnitScale.MOTIONS, onDefined: identity }),
|
|
426
436
|
capacity,
|
|
427
437
|
used: 0,
|
|
428
438
|
subjects: [],
|
|
@@ -641,8 +651,9 @@ const turn = <S>(state: State<S>, at: number, rule: number, goal: number, start:
|
|
|
641
651
|
state.originVelocities[at] = speed
|
|
642
652
|
state.delays[at] = entry.delay + wait
|
|
643
653
|
const distance = goal - start
|
|
644
|
-
state.
|
|
645
|
-
state.
|
|
654
|
+
const unit = state.unitScale(state.keys[at % state.stride], distance)
|
|
655
|
+
state.restDeltas[at] = Spring.restDeltaIn(entry.spring, distance, unit)
|
|
656
|
+
state.restSpeeds[at] = Spring.restSpeedIn(entry.spring, distance, unit)
|
|
646
657
|
// A spring that ends at its duration starts from rest, as Motion's (#359).
|
|
647
658
|
if (entry.kind !== TIMED) return
|
|
648
659
|
state.velocities[at] = 0
|
|
@@ -756,8 +767,9 @@ export const retarget: {
|
|
|
756
767
|
state.elapsed[at] = fresh
|
|
757
768
|
state.delays[at] = entry.delay + wait
|
|
758
769
|
const distance = target - state.positions[at]
|
|
759
|
-
state.
|
|
760
|
-
state.
|
|
770
|
+
const unit = state.unitScale(state.keys[at % state.stride], distance)
|
|
771
|
+
state.restDeltas[at] = Spring.restDeltaIn(entry.spring, distance, unit)
|
|
772
|
+
state.restSpeeds[at] = Spring.restSpeedIn(entry.spring, distance, unit)
|
|
761
773
|
// A spring that ends at its duration starts from rest, as Motion's (#359).
|
|
762
774
|
if (entry.kind === TIMED) state.velocities[at] = 0
|
|
763
775
|
}
|
|
@@ -1264,6 +1276,12 @@ interface AnimationState<S> {
|
|
|
1264
1276
|
/** Each member it moves, by rank, its first key's index in `ats`, and one past the last's; whether each has been told it landed. */
|
|
1265
1277
|
firsts: Int32Array
|
|
1266
1278
|
told: Uint8Array
|
|
1279
|
+
/**
|
|
1280
|
+
* Each of its keys, 1 once it has rested at its target while it moved it, or from its play when it
|
|
1281
|
+
* starts there (one that snaps): a key taken after it rested doesn't keep it from landing.
|
|
1282
|
+
*/
|
|
1283
|
+
// #951, Motion's: a value's animation that finished before a later one took the value had resolved.
|
|
1284
|
+
rested: Uint8Array
|
|
1267
1285
|
/** The members it's given that it moves nothing of, told they landed at its first play. */
|
|
1268
1286
|
idle: ReadonlyArray<number>
|
|
1269
1287
|
idleTold: boolean
|
|
@@ -1541,7 +1559,8 @@ const tellMembers = <S>(animation: AnimationState<S>, tell: (slot: number) => vo
|
|
|
1541
1559
|
/**
|
|
1542
1560
|
* Whether `animation` has landed, once a step while it plays: every key it still moves at its target,
|
|
1543
1561
|
* past its delay; or, played backwards, back at its start. It ends then. A key a later change took keeps
|
|
1544
|
-
* it from landing (#433's latest-wins, as a ward's): it ends
|
|
1562
|
+
* it from landing (#433's latest-wins, as a ward's): it ends once the keys it kept are at rest, telling
|
|
1563
|
+
* `onTaken` (#951). One taken after it rested there, or that started there, doesn't.
|
|
1545
1564
|
*/
|
|
1546
1565
|
const checkLanding = <S>(animation: AnimationState<S>): void => {
|
|
1547
1566
|
const { state, options } = animation
|
|
@@ -1549,6 +1568,7 @@ const checkLanding = <S>(animation: AnimationState<S>): void => {
|
|
|
1549
1568
|
const tell = options.onMemberLanded
|
|
1550
1569
|
if (Predicate.isNotUndefined(tell) && !backwards) tellMembers(animation, tell)
|
|
1551
1570
|
const count = animation.ats.length
|
|
1571
|
+
const rested = animation.rested
|
|
1552
1572
|
let atRest = true
|
|
1553
1573
|
let held = 0
|
|
1554
1574
|
// Once a step, per key of a playing change: a counted loop with plain reads.
|
|
@@ -1556,7 +1576,9 @@ const checkLanding = <S>(animation: AnimationState<S>): void => {
|
|
|
1556
1576
|
if (!holds(animation, i)) continue
|
|
1557
1577
|
held++
|
|
1558
1578
|
if (backwards) continue
|
|
1559
|
-
|
|
1579
|
+
const resting = keyAtRest(animation, i)
|
|
1580
|
+
if (resting) rested[i] = 1
|
|
1581
|
+
if (!resting) atRest = false
|
|
1560
1582
|
}
|
|
1561
1583
|
if (held > 0 && backwards && animation.time > 0) return
|
|
1562
1584
|
if (!atRest) return
|
|
@@ -1570,10 +1592,36 @@ const checkLanding = <S>(animation: AnimationState<S>): void => {
|
|
|
1570
1592
|
}
|
|
1571
1593
|
}
|
|
1572
1594
|
finish(animation)
|
|
1573
|
-
if (held < count)
|
|
1595
|
+
if (held < count) {
|
|
1596
|
+
if (takenInFlight(animation)) {
|
|
1597
|
+
UndefinedOr.map((options as TakenHook).onTaken, (told) => told())
|
|
1598
|
+
return
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1574
1601
|
UndefinedOr.map(options.onLanded, (told) => told())
|
|
1575
1602
|
}
|
|
1576
1603
|
|
|
1604
|
+
/**
|
|
1605
|
+
* A change's `onTaken`, which a binding's options carry beside `animate`'s or `keyframes`': called once
|
|
1606
|
+
* the change ends with a key a later change took while it moved it, which it then never lands.
|
|
1607
|
+
*/
|
|
1608
|
+
// #951: Motion's never resolves then; the scope it was started in hears it end. Not on AnimateOptions or
|
|
1609
|
+
// KeyframesOptions, so outside core's semver: a binding gives it as `@wardic/core/unstable/BatchCalls`'
|
|
1610
|
+
// `Taken<O>`, the one field this names.
|
|
1611
|
+
interface TakenHook {
|
|
1612
|
+
readonly onTaken?: (() => void) | undefined
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
/** Whether a later change took a key of `animation`'s before it rested there (#951): at its end, once. */
|
|
1616
|
+
const takenInFlight = <S>(animation: AnimationState<S>): boolean => {
|
|
1617
|
+
const count = animation.ats.length
|
|
1618
|
+
for (let i = 0; i < count; i++) {
|
|
1619
|
+
if (animation.rested[i] !== 0) continue
|
|
1620
|
+
if (!holds(animation, i)) return true
|
|
1621
|
+
}
|
|
1622
|
+
return false
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1577
1625
|
/** A driven change's transition for a member's key, before its copy: its function's, or its own or `default`, else the key's own. */
|
|
1578
1626
|
const animationBagsOf = <S>(state: State<S>, given: RetargetOptions["transition"]): ((slot: number, k: number) => Transition) => {
|
|
1579
1627
|
if (Predicate.isFunction(given)) return given
|
|
@@ -1649,6 +1697,7 @@ const animationFor = <S>(
|
|
|
1649
1697
|
indexAt: new Int32Array(0),
|
|
1650
1698
|
firsts: new Int32Array(0),
|
|
1651
1699
|
told: new Uint8Array(0),
|
|
1700
|
+
rested: new Uint8Array(0),
|
|
1652
1701
|
idle: [],
|
|
1653
1702
|
idleTold: false,
|
|
1654
1703
|
}
|
|
@@ -1953,12 +2002,15 @@ export const play = (self: Animation): number => {
|
|
|
1953
2002
|
state.drivers[rule] = animation
|
|
1954
2003
|
})
|
|
1955
2004
|
let setOff = 0
|
|
2005
|
+
// Played again, each key rests anew; one that starts at its target, as one that snaps does, has.
|
|
2006
|
+
if (animation.rested.length !== count) animation.rested = new Uint8Array(count)
|
|
1956
2007
|
for (let i = 0; i < count; i++) {
|
|
1957
2008
|
const at = animation.ats[i]
|
|
1958
2009
|
const rule = animation.rules[i]
|
|
1959
2010
|
turn(state, at, rule, animation.goals[i], animation.starts[i], animation.speeds[i], animation.waits[i])
|
|
1960
2011
|
state.elapsed[at] = time
|
|
1961
2012
|
valueAt(state, at, rule, time, true)
|
|
2013
|
+
animation.rested[i] = Number(keyAtRest(animation, i))
|
|
1962
2014
|
setOff += moving(state, slotAt(state, at))
|
|
1963
2015
|
}
|
|
1964
2016
|
return setOff
|
|
@@ -2075,8 +2127,9 @@ export const durationOf = (self: Animation): number => {
|
|
|
2075
2127
|
longest = Math.max(longest, framesUntil(entry.spring.duration, delay))
|
|
2076
2128
|
continue
|
|
2077
2129
|
}
|
|
2078
|
-
const
|
|
2079
|
-
const
|
|
2130
|
+
const unit = state.unitScale(state.keys[animation.ats[i] % state.stride], distance)
|
|
2131
|
+
const restDelta = Spring.restDeltaIn(entry.spring, distance, unit)
|
|
2132
|
+
const restSpeed = Spring.restSpeedIn(entry.spring, distance, unit)
|
|
2080
2133
|
let position = animation.starts[i]
|
|
2081
2134
|
let speed = animation.speeds[i]
|
|
2082
2135
|
let time = 0
|
package/src/DefaultTransition.ts
CHANGED
|
@@ -7,16 +7,19 @@
|
|
|
7
7
|
* import * as DefaultTransition from "@wardic/core/DefaultTransition"
|
|
8
8
|
*
|
|
9
9
|
* const defaults = yield* DefaultTransition.DefaultTransition
|
|
10
|
-
* defaults.transitionFor("x", 100) // `@wardic/dom`'s: a spring
|
|
11
|
-
* defaults.transitionFor("opacity", 1) //
|
|
10
|
+
* defaults.transitionFor("x", 100) // `byKey`'s, `@wardic/dom`'s: a spring
|
|
11
|
+
* defaults.transitionFor("opacity", 1) // `byKey`'s: a 0.3 s tween
|
|
12
12
|
*/
|
|
13
13
|
// CONTEXT.md **Default transition**; ADR-0051. Motion's `getDefaultTransition` (default-transitions.ts at
|
|
14
14
|
// 16c6122). Core defines the contract and an Environment provides it (#357): `@wardic/dom`'s is Motion's table,
|
|
15
15
|
// its `DomDefaultTransition`; one with no opinion, the Test Environment's, `oneSpring`. A
|
|
16
16
|
// binding reads it once, as it reads the Positional keys; its `keyframes` for more than two stops, as
|
|
17
17
|
// Motion's (#429). Tier 2 (ADR-0008), for binding authors.
|
|
18
|
+
import * as Bool from "effect/Boolean"
|
|
18
19
|
import * as Context from "effect/Context"
|
|
19
20
|
import * as Layer from "effect/Layer"
|
|
21
|
+
import * as Option from "effect/Option"
|
|
22
|
+
import * as BaseValue from "./BaseValue.ts"
|
|
20
23
|
import type * as Machine from "./Machine.ts"
|
|
21
24
|
|
|
22
25
|
/** A transition a key runs on: a spring, or a tween. */
|
|
@@ -59,3 +62,51 @@ export const ONE_TWEEN: KeyframesTransition = { type: "tween" }
|
|
|
59
62
|
*/
|
|
60
63
|
// #429.
|
|
61
64
|
export const layerOneSpring: Layer.Layer<DefaultTransition> = /*#__PURE__*/ Layer.succeed(DefaultTransition, { transitionFor: () => ONE_SPRING, keyframes: ONE_TWEEN })
|
|
65
|
+
|
|
66
|
+
/** A transform's but a scale's: Motion's `underDampedSpring`. */
|
|
67
|
+
const TRANSFORM: Transition = { type: "spring", stiffness: 500, damping: 25, restSpeed: 10 }
|
|
68
|
+
|
|
69
|
+
/** A scale's toward 0: Motion's `criticallyDampedSpring`, critically damped, so it doesn't overshoot past 0. */
|
|
70
|
+
const SCALE_TO_ZERO: Transition = { type: "spring", stiffness: 550, damping: 2 * Math.sqrt(550), restSpeed: 10 }
|
|
71
|
+
|
|
72
|
+
/** A scale's toward anything else: Motion's `criticallyDampedSpring`, with its damping of 30. */
|
|
73
|
+
const SCALE: Transition = { type: "spring", stiffness: 550, damping: 30, restSpeed: 10 }
|
|
74
|
+
|
|
75
|
+
/** Every other key's: Motion's `ease`. */
|
|
76
|
+
const EASE: Transition = { type: "tween", duration: 0.3, ease: [0.25, 0.1, 0.35, 1] }
|
|
77
|
+
|
|
78
|
+
/** The transition through more than two stops by key, as Motion's `keyframesTransition`: a 0.8 s tween, eased out. */
|
|
79
|
+
// #429: on its `animateMotionValue`'s `easeOut`.
|
|
80
|
+
export const KEYFRAMES_TWEEN: KeyframesTransition = { type: "tween", duration: 0.8, ease: "easeOut" }
|
|
81
|
+
|
|
82
|
+
/** A transform's: a scale's, by its target, or the rest's. */
|
|
83
|
+
const transformFor = (key: string, target: number | string): Transition =>
|
|
84
|
+
Bool.match(key.startsWith("scale"), {
|
|
85
|
+
onTrue: () => Bool.match(target === 0, { onTrue: () => SCALE_TO_ZERO, onFalse: () => SCALE }),
|
|
86
|
+
onFalse: () => TRANSFORM,
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The transition `key` runs on toward `target` by its name, as Motion picks it: a transform but a scale
|
|
91
|
+
* (`x`, `rotate`, `rotateY`…) on a spring of 500 / 25, at rest under 10 a second; a scale on a spring of
|
|
92
|
+
* 550, critically damped toward 0, else damped 30; any other key on a 0.3 s tween, eased by the cubic
|
|
93
|
+
* Bézier (0.25, 0.1, 0.35, 1). The same object for the same answer.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* DefaultTransition.byKey("x", 100) // { type: "spring", stiffness: 500, damping: 25, restSpeed: 10 }
|
|
97
|
+
* DefaultTransition.byKey("uProgress", 1) // a 0.3 s tween
|
|
98
|
+
*/
|
|
99
|
+
// Motion's `getDefaultTransition` (`motion-dom/src/animation/utils/default-transitions.ts` at 16c6122).
|
|
100
|
+
// #882 (ADR-0073, *Amendment (#882)*, Q2): dom's table (ADR-0051, #357), moved here, so a scene's keys run
|
|
101
|
+
// on what the page's do (ADR-0073, decision 22); dom's `DomDefaultTransition` is this.
|
|
102
|
+
export const byKey = (key: string, target: number | string): Transition =>
|
|
103
|
+
Bool.match(Option.isSome(BaseValue.transformIdentity(key)), { onTrue: () => transformFor(key, target), onFalse: () => EASE })
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The Default transition by key: {@link byKey} for each key, and {@link KEYFRAMES_TWEEN} through more
|
|
107
|
+
* than two stops. `@wardic/dom`'s, and `@wardic/object`'s.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* program.pipe(Effect.provide(DefaultTransition.layerByKey))
|
|
111
|
+
*/
|
|
112
|
+
export const layerByKey: Layer.Layer<DefaultTransition> = /*#__PURE__*/ Layer.succeed(DefaultTransition, { transitionFor: byKey, keyframes: KEYFRAMES_TWEEN })
|
package/src/FrameLoop.ts
CHANGED
|
@@ -28,9 +28,13 @@ export { CheckFrames, NonFiniteFrame } from "./internal/frameLoop.ts"
|
|
|
28
28
|
* an Effect doesn't compile, where a `void` one would drop the Effect silently. A Frame handed to a sink
|
|
29
29
|
* is the run's until the next tick, which may rewrite it in place: a sink that keeps a Frame, its
|
|
30
30
|
* `value` or its `velocity`, copies it.
|
|
31
|
+
*
|
|
32
|
+
* It's handed `dt` too: the time its run was just stepped over, in seconds, the step's own. That's the
|
|
33
|
+
* tick's, for a step a tick or its Late window makes, and 0 for a last-lane run's step again in the
|
|
34
|
+
* window. A sink that works out a speed from its Frames divides by it.
|
|
31
35
|
*/
|
|
32
|
-
// ADR-0027, decision 8; ADR-0030, decision 11 (`Machine.springValues`' Frame)
|
|
33
|
-
export type Sink<F> = (frame: F) => undefined
|
|
36
|
+
// ADR-0027, decision 8; ADR-0030, decision 11 (`Machine.springValues`' Frame); `dt`: *Amendment (#865)*.
|
|
37
|
+
export type Sink<F> = (frame: F, dt: number) => undefined
|
|
34
38
|
|
|
35
39
|
/**
|
|
36
40
|
* The Frame loop: `run` runs a machine on it, as `Machine.run` does, and `runLast` one stepped after
|
package/src/Machine.ts
CHANGED
|
@@ -75,6 +75,7 @@ import {
|
|
|
75
75
|
import * as Inertia from "./Inertia.ts"
|
|
76
76
|
import type * as Point from "./Point.ts"
|
|
77
77
|
import * as Spring from "./Spring.ts"
|
|
78
|
+
import * as UnitScale from "./UnitScale.ts"
|
|
78
79
|
import type * as Supersede from "./Supersede.ts"
|
|
79
80
|
import type * as Values from "./Values.ts"
|
|
80
81
|
import { dual } from "./internal/dual.ts"
|
|
@@ -1071,6 +1072,9 @@ export interface SpringValuesStart {
|
|
|
1071
1072
|
*/
|
|
1072
1073
|
// #663: a keyed store of a number allocates in V8 (about 12 B), an indexed one doesn't.
|
|
1073
1074
|
readonly records?: "value"
|
|
1075
|
+
/** The Environment's unit scale: each key's rest thresholds read at the units it gives for the key and its distance. Motion's own when left out. */
|
|
1076
|
+
// ADR-0069 decision 8 (#883).
|
|
1077
|
+
readonly unitScale?: UnitScale.Rule | undefined
|
|
1074
1078
|
}
|
|
1075
1079
|
|
|
1076
1080
|
/** A start's values when it gives none. */
|
|
@@ -1188,7 +1192,7 @@ export const springValues = (
|
|
|
1188
1192
|
options: SpringOptions = NO_OPTIONS,
|
|
1189
1193
|
start: SpringValuesStart = NO_START,
|
|
1190
1194
|
): Machine<Frame.SpringValues> => {
|
|
1191
|
-
const { from = NO_START_VALUES, velocity = NO_START_VALUES, records } = start
|
|
1195
|
+
const { from = NO_START_VALUES, velocity = NO_START_VALUES, records, unitScale = UnitScale.MOTIONS } = start
|
|
1192
1196
|
const writesVelocity = Predicate.isUndefined(records)
|
|
1193
1197
|
const keys = Rec.keys(target)
|
|
1194
1198
|
/** A key's start: its own value, else the options', else 0. */
|
|
@@ -1209,8 +1213,8 @@ export const springValues = (
|
|
|
1209
1213
|
const velocities = Arr.map(keys, (key) => startOf(velocity, key, options.velocity))
|
|
1210
1214
|
const elapsed = Arr.map(keys, () => 0)
|
|
1211
1215
|
// Each key's rest thresholds, by its own distance (#344), as Motion's are per value.
|
|
1212
|
-
const restDeltas = Arr.map(keys, (
|
|
1213
|
-
const restSpeeds = Arr.map(keys, (
|
|
1216
|
+
const restDeltas = Arr.map(keys, (key, i) => Spring.restDeltaIn(spring, targets[i] - positions[i], unitScale(key, targets[i] - positions[i])))
|
|
1217
|
+
const restSpeeds = Arr.map(keys, (key, i) => Spring.restSpeedIn(spring, targets[i] - positions[i], unitScale(key, targets[i] - positions[i])))
|
|
1214
1218
|
// The Frame's records, built from `{}` a key at a time. Built with `Rec.fromEntries`, the
|
|
1215
1219
|
// step allocated 0.38 MB a frame on Node's `springValues/loop`, against 0.14, and ran slower
|
|
1216
1220
|
// (ADR-0030, *Implementation in #111*).
|
package/src/Spring.ts
CHANGED
|
@@ -399,6 +399,22 @@ export const restDeltaFor = (spring: Resolved, distance: number, scale = 1): num
|
|
|
399
399
|
export const restSpeedFor = (spring: Resolved, distance: number, scale = 1): number =>
|
|
400
400
|
thresholdOf(spring.restSpeed, distance, GRANULAR_REST_SPEED, REST_SPEED) / scale
|
|
401
401
|
|
|
402
|
+
/**
|
|
403
|
+
* A leg's distance threshold for a key whose units are `unit` of Motion's (an Environment's `UnitScale`): the one
|
|
404
|
+
* the spring is given, as it is, else by `distance` read in Motion's units. Allocates nothing.
|
|
405
|
+
*/
|
|
406
|
+
// ADR-0069 decision 8: `restFor(spring, d * unitScale, unitScale)`, a given threshold winning.
|
|
407
|
+
export const restDeltaIn = (spring: Resolved, distance: number, unit: number): number => {
|
|
408
|
+
if (Predicate.isNotUndefined(spring.restDelta)) return spring.restDelta
|
|
409
|
+
return restDeltaFor(spring, distance * unit, unit)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** {@link restDeltaIn}'s speed. */
|
|
413
|
+
export const restSpeedIn = (spring: Resolved, distance: number, unit: number): number => {
|
|
414
|
+
if (Predicate.isNotUndefined(spring.restSpeed)) return spring.restSpeed
|
|
415
|
+
return restSpeedFor(spring, distance * unit, unit)
|
|
416
|
+
}
|
|
417
|
+
|
|
402
418
|
/** A threshold given, else the granular one for a leg under {@link GRANULAR}, else the coarse one. Plain reads: a Retarget runs it per key. */
|
|
403
419
|
const thresholdOf = (given: number | undefined, distance: number, granular: number, coarse: number): number => {
|
|
404
420
|
if (Predicate.isNotUndefined(given)) return given
|
package/src/UnitScale.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many of Motion's units a key counts as, so a spring's rest thresholds fit the distances it moves:
|
|
3
|
+
* an Environment's rule, by the key and the distance its leg goes, 1 for every key by default, Motion's
|
|
4
|
+
* own. A scene in metres gives `x`, `y` and `z` 100 (`layer(100)`), so a 10 m move rests within 5 mm and
|
|
5
|
+
* a 4 cm one within 0.05 mm, where Motion's thresholds would stop a move of 5 units or more 4–5% short and
|
|
6
|
+
* snap it. A key whose values span 0 to 1 can rest by its share of the distance instead (`relative`), as a
|
|
7
|
+
* blended key's progress does. A threshold a spring is given is kept as it is.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import * as UnitScale from "@wardic/core/UnitScale"
|
|
11
|
+
*
|
|
12
|
+
* const env = Layer.mergeAll(sceneLayers, UnitScale.layer(100))
|
|
13
|
+
* // or a rule of the Environment's own: a colour's channels by their share of the distance
|
|
14
|
+
* const rule: UnitScale.Rule = (key, distance) => (isChannel(key) ? UnitScale.relative(distance, 0.001) : UnitScale.position(100)(key, distance))
|
|
15
|
+
*/
|
|
16
|
+
// ADR-0069 decision 8; ADR-0073, *Amendment (#883)*: a `Context.Reference` with its default, read by core
|
|
17
|
+
// itself from the runtime its spines and Batches hold, so no binding passes it (as `PressClaims` and
|
|
18
|
+
// `DragActivity` are References). A rule by key and distance since PR 3 (Q2): three's linear colour
|
|
19
|
+
// channels rest relative to their distance. Tier 2 (ADR-0008): Effect-typed, for Environment authors.
|
|
20
|
+
import * as Context from "effect/Context"
|
|
21
|
+
import * as Layer from "effect/Layer"
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* How many of Motion's units `key` counts as on a leg of `distance` (its target less its start, in the
|
|
25
|
+
* key's own units): the scale its spring's rest thresholds are read at. Asked once per key per leg.
|
|
26
|
+
*/
|
|
27
|
+
export type Rule = (key: string, distance: number) => number
|
|
28
|
+
|
|
29
|
+
/** Motion's own: every key at 1. */
|
|
30
|
+
export const MOTIONS: Rule = () => 1
|
|
31
|
+
|
|
32
|
+
/** The keys a Subject's position names. */
|
|
33
|
+
const POSITION: ReadonlySet<string> = new Set(["x", "y", "z"])
|
|
34
|
+
|
|
35
|
+
/** `x`, `y` and `z` at `scale` of Motion's units, every other key at 1: a scene in metres gives 100. */
|
|
36
|
+
// Once a leg per key: a plain branch, no matcher made.
|
|
37
|
+
export const position = (scale: number): Rule => (key) => {
|
|
38
|
+
if (POSITION.has(key)) return scale
|
|
39
|
+
return 1
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The scale for a key that rests by its share of `distance`, as a blended key's 0 → 100 progress does
|
|
44
|
+
* (`Machine.springBlend`): every leg lands in the same time, however far it goes. `floor` is the least
|
|
45
|
+
* distance it counts, so a key that isn't moving but has a speed still comes to rest.
|
|
46
|
+
*/
|
|
47
|
+
export const relative = (distance: number, floor: number): number => 100 / Math.max(Math.abs(distance), floor)
|
|
48
|
+
|
|
49
|
+
/** The Environment's rule: Motion's, unless its Layers give another. */
|
|
50
|
+
export const UnitScale: Context.Reference<Rule> = /*#__PURE__*/ Context.Reference<Rule>("@wardic/core/UnitScale", { defaultValue: () => MOTIONS })
|
|
51
|
+
|
|
52
|
+
/** A Layer giving `rule` as the Environment's. */
|
|
53
|
+
export const layerWith = (rule: Rule): Layer.Layer<never> => Layer.succeed(UnitScale, rule)
|
|
54
|
+
|
|
55
|
+
/** A Layer giving `x`, `y` and `z` at `scale`: `layerWith(position(scale))`. */
|
|
56
|
+
export const layer = (scale: number): Layer.Layer<never> => layerWith(position(scale))
|
package/src/index.ts
CHANGED
|
@@ -56,5 +56,6 @@ export * as Spring from "./Spring.ts"
|
|
|
56
56
|
export * as Stagger from "./Stagger.ts"
|
|
57
57
|
export * as Supersede from "./Supersede.ts"
|
|
58
58
|
export * as Values from "./Values.ts"
|
|
59
|
+
export * as UnitScale from "./UnitScale.ts"
|
|
59
60
|
export * as Velocity from "./Velocity.ts"
|
|
60
61
|
export * as Wave from "./Wave.ts"
|
|
@@ -256,9 +256,9 @@ const checkValue = (value: unknown): void => {
|
|
|
256
256
|
/** `sink`, each Frame checked first: a run's sink with `CheckFrames` on. */
|
|
257
257
|
const checked =
|
|
258
258
|
<F extends Frame.Frame<unknown>>(sink: FrameLoop.Sink<F>): FrameLoop.Sink<F> =>
|
|
259
|
-
(frame) => {
|
|
259
|
+
(frame, dt) => {
|
|
260
260
|
checkValue(frame.value)
|
|
261
|
-
return sink(frame)
|
|
261
|
+
return sink(frame, dt)
|
|
262
262
|
}
|
|
263
263
|
|
|
264
264
|
/** A run's wait for its end. Built once (#460). */
|
|
@@ -394,7 +394,7 @@ export const make = Effect.fnUntraced(
|
|
|
394
394
|
if (entry.removed) continue
|
|
395
395
|
try {
|
|
396
396
|
const frame = entry.step(dt)
|
|
397
|
-
entry.sink(frame)
|
|
397
|
+
entry.sink(frame, dt)
|
|
398
398
|
if (entry.settles && frame.done) end(entry, Exit.succeed(frame))
|
|
399
399
|
} catch (defect) {
|
|
400
400
|
end(entry, dieNamed(entry.name, defect))
|
|
@@ -435,7 +435,7 @@ export const make = Effect.fnUntraced(
|
|
|
435
435
|
if (entry.removed) continue
|
|
436
436
|
try {
|
|
437
437
|
const frame = entry.step(dt)
|
|
438
|
-
entry.sink(frame)
|
|
438
|
+
entry.sink(frame, dt)
|
|
439
439
|
if (entry.settles && frame.done) end(entry, Exit.succeed(frame))
|
|
440
440
|
} catch (defect) {
|
|
441
441
|
end(entry, dieNamed(entry.name, defect))
|
|
@@ -490,7 +490,7 @@ export const make = Effect.fnUntraced(
|
|
|
490
490
|
const stepOne = (entry: Entry, dt: number): void => {
|
|
491
491
|
try {
|
|
492
492
|
const frame = entry.step(dt)
|
|
493
|
-
entry.sink(frame)
|
|
493
|
+
entry.sink(frame, dt)
|
|
494
494
|
if (entry.settles && frame.done) end(entry, Exit.succeed(frame))
|
|
495
495
|
} catch (defect) {
|
|
496
496
|
end(entry, dieNamed(entry.name, defect))
|