@rafaelaugusto/jolt 0.1.0-rc.12 → 0.1.0-rc.13

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.
Files changed (257) hide show
  1. package/README.md +2 -2
  2. package/dist/core/circle-math.d.ts.map +1 -1
  3. package/dist/core/easing.d.ts +8 -0
  4. package/dist/core/easing.d.ts.map +1 -1
  5. package/dist/core/easing.js +12 -4
  6. package/dist/core/easing.js.map +1 -1
  7. package/dist/core/gesture-math.d.ts.map +1 -1
  8. package/dist/core/gradient-text.d.ts +24 -0
  9. package/dist/core/gradient-text.d.ts.map +1 -0
  10. package/dist/core/gradient-text.js +30 -0
  11. package/dist/core/gradient-text.js.map +1 -0
  12. package/dist/core/inspection-plot.d.ts +11 -0
  13. package/dist/core/inspection-plot.d.ts.map +1 -0
  14. package/dist/core/inspection-plot.js +57 -0
  15. package/dist/core/inspection-plot.js.map +1 -0
  16. package/dist/core/motion-behaviors.d.ts +68 -0
  17. package/dist/core/motion-behaviors.d.ts.map +1 -0
  18. package/dist/core/motion-behaviors.js +276 -0
  19. package/dist/core/motion-behaviors.js.map +1 -0
  20. package/dist/core/numeric-map.js.map +1 -1
  21. package/dist/core/particle-motion.d.ts +8 -0
  22. package/dist/core/particle-motion.d.ts.map +1 -0
  23. package/dist/core/particle-motion.js +11 -0
  24. package/dist/core/particle-motion.js.map +1 -0
  25. package/dist/core/pointer.d.ts +1 -1
  26. package/dist/core/preset-recipe.d.ts +20 -0
  27. package/dist/core/preset-recipe.d.ts.map +1 -0
  28. package/dist/core/preset-recipe.js +79 -0
  29. package/dist/core/preset-recipe.js.map +1 -0
  30. package/dist/core/reorder.d.ts +5 -0
  31. package/dist/core/reorder.d.ts.map +1 -1
  32. package/dist/core/reorder.js +17 -0
  33. package/dist/core/reorder.js.map +1 -1
  34. package/dist/core/rig.d.ts +73 -0
  35. package/dist/core/rig.d.ts.map +1 -0
  36. package/dist/core/rig.js +99 -0
  37. package/dist/core/rig.js.map +1 -0
  38. package/dist/core/scroll-header.js.map +1 -1
  39. package/dist/core/sensor-kinds.d.ts +1 -1
  40. package/dist/core/sensor-kinds.d.ts.map +1 -1
  41. package/dist/core/sensor-replay.d.ts +10 -0
  42. package/dist/core/sensor-replay.d.ts.map +1 -0
  43. package/dist/core/sensor-replay.js +32 -0
  44. package/dist/core/sensor-replay.js.map +1 -0
  45. package/dist/gradient-text/index.d.ts +2 -10
  46. package/dist/gradient-text/index.d.ts.map +1 -1
  47. package/dist/gradient-text/index.js +2 -25
  48. package/dist/gradient-text/index.js.map +1 -1
  49. package/dist/gradient-text/native.d.ts +2 -8
  50. package/dist/gradient-text/native.d.ts.map +1 -1
  51. package/dist/gradient-text/native.js.map +1 -1
  52. package/dist/index.d.ts +2 -0
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +1 -0
  55. package/dist/index.js.map +1 -1
  56. package/dist/inspector/plot.d.ts +1 -3
  57. package/dist/inspector/plot.d.ts.map +1 -1
  58. package/dist/inspector/plot.js +1 -22
  59. package/dist/inspector/plot.js.map +1 -1
  60. package/dist/motion-behaviors.d.ts +5 -54
  61. package/dist/motion-behaviors.d.ts.map +1 -1
  62. package/dist/motion-behaviors.js +18 -267
  63. package/dist/motion-behaviors.js.map +1 -1
  64. package/dist/rig/index.d.ts +2 -27
  65. package/dist/rig/index.d.ts.map +1 -1
  66. package/dist/rig/index.js +5 -89
  67. package/dist/rig/index.js.map +1 -1
  68. package/dist/testing/sensors.d.ts +2 -9
  69. package/dist/testing/sensors.d.ts.map +1 -1
  70. package/dist/testing/sensors.js +3 -21
  71. package/dist/testing/sensors.js.map +1 -1
  72. package/dist/web/angular/index.d.ts +62 -0
  73. package/dist/web/angular/index.d.ts.map +1 -0
  74. package/dist/web/angular/index.js +190 -0
  75. package/dist/web/angular/index.js.map +1 -0
  76. package/dist/web/behaviors.d.ts +5 -0
  77. package/dist/web/behaviors.d.ts.map +1 -0
  78. package/dist/web/behaviors.js +2 -0
  79. package/dist/web/behaviors.js.map +1 -0
  80. package/dist/web/bottom-sheet.js.map +1 -1
  81. package/dist/web/compose.js.map +1 -1
  82. package/dist/web/curve.js.map +1 -1
  83. package/dist/web/decay.js.map +1 -1
  84. package/dist/web/diagnostics.d.ts +1 -1
  85. package/dist/web/diagnostics.d.ts.map +1 -1
  86. package/dist/web/diagnostics.js.map +1 -1
  87. package/dist/web/drag.d.ts +11 -11
  88. package/dist/web/drag.d.ts.map +1 -1
  89. package/dist/web/effect.d.ts +2 -1
  90. package/dist/web/effect.d.ts.map +1 -1
  91. package/dist/web/effect.js +11 -4
  92. package/dist/web/effect.js.map +1 -1
  93. package/dist/web/frameworks/binding.d.ts +39 -0
  94. package/dist/web/frameworks/binding.d.ts.map +1 -0
  95. package/dist/web/frameworks/binding.js +356 -0
  96. package/dist/web/frameworks/binding.js.map +1 -0
  97. package/dist/web/gestures.js.map +1 -1
  98. package/dist/web/gradient-css.d.ts +2 -0
  99. package/dist/web/gradient-css.d.ts.map +1 -0
  100. package/dist/web/gradient-css.js +5 -0
  101. package/dist/web/gradient-css.js.map +1 -0
  102. package/dist/web/gradient-text.d.ts +24 -0
  103. package/dist/web/gradient-text.d.ts.map +1 -0
  104. package/dist/web/gradient-text.js +179 -0
  105. package/dist/web/gradient-text.js.map +1 -0
  106. package/dist/web/gradient.d.ts.map +1 -1
  107. package/dist/web/gradient.js +2 -1
  108. package/dist/web/gradient.js.map +1 -1
  109. package/dist/web/image-surface.d.ts.map +1 -1
  110. package/dist/web/image-surface.js.map +1 -1
  111. package/dist/web/inspector.d.ts +24 -0
  112. package/dist/web/inspector.d.ts.map +1 -1
  113. package/dist/web/inspector.js +76 -5
  114. package/dist/web/inspector.js.map +1 -1
  115. package/dist/web/interaction.d.ts +4 -0
  116. package/dist/web/interaction.d.ts.map +1 -1
  117. package/dist/web/interaction.js.map +1 -1
  118. package/dist/web/keyboard.js.map +1 -1
  119. package/dist/web/layout.d.ts +75 -75
  120. package/dist/web/layout.d.ts.map +1 -1
  121. package/dist/web/layout.js.map +1 -1
  122. package/dist/web/particles.d.ts.map +1 -1
  123. package/dist/web/particles.js +5 -6
  124. package/dist/web/particles.js.map +1 -1
  125. package/dist/web/plan.d.ts +2 -0
  126. package/dist/web/plan.d.ts.map +1 -1
  127. package/dist/web/plan.js.map +1 -1
  128. package/dist/web/press.js.map +1 -1
  129. package/dist/web/progress.d.ts.map +1 -1
  130. package/dist/web/properties.d.ts +4 -4
  131. package/dist/web/properties.d.ts.map +1 -1
  132. package/dist/web/property.d.ts.map +1 -1
  133. package/dist/web/property.js.map +1 -1
  134. package/dist/web/public.d.ts +19 -3
  135. package/dist/web/public.d.ts.map +1 -1
  136. package/dist/web/public.js +9 -0
  137. package/dist/web/public.js.map +1 -1
  138. package/dist/web/react/index.d.ts +12 -0
  139. package/dist/web/react/index.d.ts.map +1 -0
  140. package/dist/web/react/index.js +119 -0
  141. package/dist/web/react/index.js.map +1 -0
  142. package/dist/web/reorder.d.ts.map +1 -1
  143. package/dist/web/reorder.js +23 -2
  144. package/dist/web/reorder.js.map +1 -1
  145. package/dist/web/rig.d.ts +43 -0
  146. package/dist/web/rig.d.ts.map +1 -0
  147. package/dist/web/rig.js +307 -0
  148. package/dist/web/rig.js.map +1 -0
  149. package/dist/web/runtime.d.ts +37 -25
  150. package/dist/web/runtime.d.ts.map +1 -1
  151. package/dist/web/runtime.js +111 -8
  152. package/dist/web/runtime.js.map +1 -1
  153. package/dist/web/scheduler.d.ts.map +1 -1
  154. package/dist/web/scheduler.js.map +1 -1
  155. package/dist/web/scope.d.ts +7 -1
  156. package/dist/web/scope.d.ts.map +1 -1
  157. package/dist/web/scope.js +16 -2
  158. package/dist/web/scope.js.map +1 -1
  159. package/dist/web/scroll-header.js.map +1 -1
  160. package/dist/web/scroll-range.d.ts.map +1 -1
  161. package/dist/web/scroll-range.js.map +1 -1
  162. package/dist/web/scroll.d.ts +1 -1
  163. package/dist/web/scroll.d.ts.map +1 -1
  164. package/dist/web/scroll.js.map +1 -1
  165. package/dist/web/sensor-replay-bridge.d.ts +16 -0
  166. package/dist/web/sensor-replay-bridge.d.ts.map +1 -0
  167. package/dist/web/sensor-replay-bridge.js +12 -0
  168. package/dist/web/sensor-replay-bridge.js.map +1 -0
  169. package/dist/web/sensor-replay.d.ts +22 -0
  170. package/dist/web/sensor-replay.d.ts.map +1 -0
  171. package/dist/web/sensor-replay.js +124 -0
  172. package/dist/web/sensor-replay.js.map +1 -0
  173. package/dist/web/sensors.d.ts +14 -5
  174. package/dist/web/sensors.d.ts.map +1 -1
  175. package/dist/web/sensors.js +159 -41
  176. package/dist/web/sensors.js.map +1 -1
  177. package/dist/web/shape.d.ts +2 -0
  178. package/dist/web/shape.d.ts.map +1 -1
  179. package/dist/web/shape.js +44 -12
  180. package/dist/web/shape.js.map +1 -1
  181. package/dist/web/shared-layout.js.map +1 -1
  182. package/dist/web/size.d.ts.map +1 -1
  183. package/dist/web/size.js.map +1 -1
  184. package/dist/web/solid/index.d.ts +16 -0
  185. package/dist/web/solid/index.d.ts.map +1 -0
  186. package/dist/web/solid/index.js +89 -0
  187. package/dist/web/solid/index.js.map +1 -0
  188. package/dist/web/spring-value.d.ts.map +1 -1
  189. package/dist/web/spring-value.js.map +1 -1
  190. package/dist/web/stroke.d.ts.map +1 -1
  191. package/dist/web/stroke.js.map +1 -1
  192. package/dist/web/style.d.ts.map +1 -1
  193. package/dist/web/style.js.map +1 -1
  194. package/dist/web/surface-effect.d.ts.map +1 -1
  195. package/dist/web/surface-effect.js.map +1 -1
  196. package/dist/web/svelte/index.d.ts +50 -0
  197. package/dist/web/svelte/index.d.ts.map +1 -0
  198. package/dist/web/svelte/index.js +68 -0
  199. package/dist/web/svelte/index.js.map +1 -0
  200. package/dist/web/testing.d.ts +1 -0
  201. package/dist/web/testing.d.ts.map +1 -1
  202. package/dist/web/testing.js +1 -0
  203. package/dist/web/testing.js.map +1 -1
  204. package/dist/web/timers.d.ts.map +1 -1
  205. package/dist/web/timers.js.map +1 -1
  206. package/dist/web/types.d.ts.map +1 -1
  207. package/dist/web/value-playback.d.ts +17 -17
  208. package/dist/web/value-playback.js.map +1 -1
  209. package/dist/web/value.d.ts.map +1 -1
  210. package/dist/web/value.js.map +1 -1
  211. package/dist/web/viewport.js.map +1 -1
  212. package/dist/web/vue/index.d.ts +12 -3
  213. package/dist/web/vue/index.d.ts.map +1 -1
  214. package/dist/web/vue/index.js +115 -35
  215. package/dist/web/vue/index.js.map +1 -1
  216. package/package.json +35 -6
  217. package/src/core/easing.ts +12 -4
  218. package/src/core/gradient-text.ts +56 -0
  219. package/src/core/inspection-plot.ts +74 -0
  220. package/src/core/motion-behaviors.ts +337 -0
  221. package/src/core/particle-motion.ts +20 -0
  222. package/src/core/preset-recipe.ts +111 -0
  223. package/src/core/reorder.ts +23 -0
  224. package/src/core/rig.ts +149 -0
  225. package/src/core/sensor-replay.ts +48 -0
  226. package/src/gradient-text/index.ts +7 -43
  227. package/src/gradient-text/native.ts +2 -7
  228. package/src/index.ts +6 -0
  229. package/src/inspector/plot.ts +1 -27
  230. package/src/motion-behaviors.ts +34 -316
  231. package/src/rig/index.ts +25 -124
  232. package/src/testing/sensors.ts +4 -36
  233. package/src/web/angular/index.ts +171 -0
  234. package/src/web/behaviors.ts +12 -0
  235. package/src/web/effect.ts +10 -5
  236. package/src/web/frameworks/binding.ts +408 -0
  237. package/src/web/gradient-css.ts +8 -0
  238. package/src/web/gradient-text.ts +234 -0
  239. package/src/web/gradient.ts +2 -1
  240. package/src/web/inspector.ts +80 -5
  241. package/src/web/interaction.ts +4 -0
  242. package/src/web/particles.ts +15 -9
  243. package/src/web/plan.ts +2 -0
  244. package/src/web/public.ts +51 -4
  245. package/src/web/react/index.ts +208 -0
  246. package/src/web/reorder.ts +21 -3
  247. package/src/web/rig.ts +377 -0
  248. package/src/web/runtime.ts +128 -11
  249. package/src/web/scope.ts +32 -4
  250. package/src/web/sensor-replay-bridge.ts +23 -0
  251. package/src/web/sensor-replay.ts +140 -0
  252. package/src/web/sensors.ts +179 -57
  253. package/src/web/shape.ts +42 -9
  254. package/src/web/solid/index.ts +102 -0
  255. package/src/web/svelte/index.ts +88 -0
  256. package/src/web/testing.ts +6 -0
  257. package/src/web/vue/index.ts +122 -31
@@ -1,9 +1,17 @@
1
+ import {
2
+ sensorKinds,
3
+ sensorUnits,
4
+ type SensorKind,
5
+ type VectorSensorKind,
6
+ type SensorUnit,
7
+ } from '../core/sensor-kinds.js'
1
8
  import { effectOptions, type Realm } from './effect.js'
2
9
  import type { PlatformHost } from './platform-host.js'
3
10
  import { NumericValue, type WebReadableValue } from './value.js'
4
11
  import type { WebInteraction } from './interaction.js'
12
+ import { registerSensorReplay, type SensorReplaySession } from './sensor-replay-bridge.js'
5
13
 
6
- export type WebSensorKind = 'rotation' | 'gravity'
14
+ export type WebSensorKind = SensorKind
7
15
  export type WebSensorStatus =
8
16
  | 'idle'
9
17
  | 'permission-required'
@@ -21,6 +29,7 @@ export interface WebSensorOptions {
21
29
  }
22
30
  export interface WebSensor extends WebInteraction {
23
31
  readonly kind: WebSensorKind
32
+ readonly unit: SensorUnit
24
33
  readonly status: WebSensorStatus
25
34
  /** True only after a valid reading, never inferred from the presence of an API. */
26
35
  readonly available: boolean
@@ -30,13 +39,16 @@ export interface WebSensor extends WebInteraction {
30
39
  /** Call directly from a user gesture. Never prompts during construction/resume. */
31
40
  requestPermission(): Promise<'granted' | 'denied' | 'unavailable'>
32
41
  snapshot(): {
42
+ unit: SensorUnit
33
43
  status: WebSensorStatus
34
44
  available: boolean
35
45
  timestamp: number | null
36
46
  samples: number
37
47
  values: number[]
38
48
  reason?: string
39
- backend: 'device-orientation' | 'device-motion' | 'none'
49
+ backend: 'device-orientation' | 'device-motion' | 'replay' | 'none'
50
+ /** Describes the retained values, including after a replay has finished. */
51
+ sampleSource: 'device-event' | 'replay' | 'none'
40
52
  }
41
53
  }
42
54
  export interface WebRotationSensor extends WebSensor {
@@ -50,9 +62,12 @@ export interface WebRotationSensor extends WebSensor {
50
62
  }>
51
63
  recenter(): void
52
64
  }
53
- export interface WebGravitySensor extends WebSensor {
54
- readonly kind: 'gravity'
65
+ export interface WebVectorSensor<
66
+ K extends VectorSensorKind = VectorSensorKind,
67
+ > extends WebSensor {
68
+ readonly kind: K
55
69
  }
70
+ export interface WebGravitySensor extends WebVectorSensor<'gravity'> {}
56
71
  export class SensorValueNode extends NumericValue {
57
72
  demand?: (active: boolean) => void
58
73
  protected override demandChanged() {
@@ -112,63 +127,72 @@ interface Sample {
112
127
  values: number[]
113
128
  timestamp: number
114
129
  }
115
- // One DOM listener for each hardware event per document, across runtimes/subscribers.
130
+ /** DeviceMotion vectors follow the native contract: m/s², downward gravity, radians/s. */
131
+ export function motionVector(
132
+ kind: Exclude<WebSensorKind, 'rotation'>,
133
+ event: DeviceMotionEvent,
134
+ ): number[] | undefined {
135
+ const xyz = (value: DeviceMotionEventAcceleration | null) =>
136
+ value && [value.x, value.y, value.z]
137
+ let values: (number | null)[] | null | undefined
138
+ if (kind === 'acceleration') values = xyz(event.accelerationIncludingGravity)
139
+ else if (kind === 'linearAcceleration') values = xyz(event.acceleration)
140
+ else if (kind === 'gyroscope') {
141
+ const rate = event.rotationRate
142
+ // DeviceMotion alpha/beta/gamma are rates about Z/X/Y, in degrees/s.
143
+ values = rate && [rate.beta, rate.gamma, rate.alpha]
144
+ } else if (kind === 'gravity') {
145
+ const linear = xyz(event.acceleration),
146
+ total = xyz(event.accelerationIncludingGravity)
147
+ if (linear?.every(finite) && total?.every(finite))
148
+ values = linear.map((v, i) => v! - total[i]!)
149
+ }
150
+ if (!values?.every(finite)) return
151
+ return (values as number[]).map((v) => (kind === 'gyroscope' ? (v * Math.PI) / 180 : v))
152
+ }
153
+ const finite = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v)
154
+ // One DOM listener per actual hardware event per document, even across kinds/runtimes.
155
+ type HardwareEvent = 'deviceorientation' | 'devicemotion'
116
156
  const hubs = new WeakMap<
117
157
  Document,
118
- Map<WebSensorKind, { listeners: Set<(s: Sample) => void>; stop(): void }>
158
+ Map<HardwareEvent, { listeners: Set<(event: Event) => void>; stop(): void }>
119
159
  >()
120
160
  function subscribe(
121
161
  document: Document,
122
162
  kind: WebSensorKind,
123
163
  listener: (s: Sample) => void,
124
164
  ) {
165
+ const name: HardwareEvent = kind === 'rotation' ? 'deviceorientation' : 'devicemotion'
125
166
  let map = hubs.get(document)
126
167
  if (!map) hubs.set(document, (map = new Map()))
127
- let hub = map.get(kind)
168
+ let hub = map.get(name)
128
169
  if (!hub) {
129
170
  const view = document.defaultView!,
130
- listeners = new Set<(s: Sample) => void>()
131
- const name = kind === 'rotation' ? 'deviceorientation' : 'devicemotion'
171
+ listeners = new Set<(event: Event) => void>()
132
172
  const read = (event: Event) => {
133
- let values: number[]
134
- if (kind === 'rotation') {
135
- const e = event as DeviceOrientationEvent
136
- if (
137
- ![e.alpha, e.beta, e.gamma].every(
138
- (v) => typeof v === 'number' && Number.isFinite(v),
139
- )
140
- )
141
- return
142
- values = orientationQuaternion(e.alpha!, e.beta!, e.gamma!)
143
- } else {
144
- const e = event as DeviceMotionEvent,
145
- total = e.accelerationIncludingGravity,
146
- linear = e.acceleration
147
- // Specific force includes +g on a face-up device; Jolt exposes downward gravity.
148
- if (
149
- !total ||
150
- !linear ||
151
- ![total.x, total.y, total.z, linear.x, linear.y, linear.z].every(
152
- (v) => typeof v === 'number' && Number.isFinite(v),
153
- )
154
- )
155
- return
156
- values = [linear.x! - total.x!, linear.y! - total.y!, linear.z! - total.z!]
157
- }
158
- if (!Number.isFinite(event.timeStamp)) return
159
- for (const fn of [...listeners]) fn({ values, timestamp: event.timeStamp })
173
+ for (const fn of [...listeners]) fn(event)
160
174
  }
161
175
  view.addEventListener(name, read)
162
176
  hub = { listeners, stop: () => view.removeEventListener(name, read) }
163
- map.set(kind, hub)
177
+ map.set(name, hub)
178
+ }
179
+ const read = (event: Event) => {
180
+ if (!finite(event.timeStamp)) return
181
+ let values: number[] | undefined
182
+ if (kind === 'rotation') {
183
+ const e = event as DeviceOrientationEvent
184
+ if ([e.alpha, e.beta, e.gamma].every(finite))
185
+ values = orientationQuaternion(e.alpha!, e.beta!, e.gamma!)
186
+ } else values = motionVector(kind, event as DeviceMotionEvent)
187
+ if (values) listener({ values, timestamp: event.timeStamp })
164
188
  }
165
- hub.listeners.add(listener)
189
+ hub.listeners.add(read)
166
190
  const source = hub
167
191
  return () => {
168
- source.listeners.delete(listener)
192
+ source.listeners.delete(read)
169
193
  if (!source.listeners.size) {
170
194
  source.stop()
171
- map!.delete(kind)
195
+ map!.delete(name)
172
196
  }
173
197
  }
174
198
  }
@@ -178,10 +202,9 @@ export function createSensor(
178
202
  options: WebSensorOptions,
179
203
  host: PlatformHost,
180
204
  makeValue: (initial: number) => { node: SensorValueNode; value: WebReadableValue },
181
- ): WebRotationSensor | WebGravitySensor {
205
+ ): WebRotationSensor | WebVectorSensor {
182
206
  effectOptions(options, ['interval', 'coordinates', 'reducedMotion', 'onStatus'])
183
- if (!['rotation', 'gravity'].includes(kind))
184
- throw new TypeError('sensor supports rotation and gravity.')
207
+ if (!sensorKinds.includes(kind)) throw new TypeError(`Unknown sensor kind: ${kind}`)
185
208
  const interval =
186
209
  options.interval === 'auto' || options.interval === undefined ? 0 : options.interval
187
210
  if (
@@ -203,7 +226,9 @@ export function createSensor(
203
226
  const constructor = (
204
227
  kind === 'rotation' ? view.DeviceOrientationEvent : view.DeviceMotionEvent
205
228
  ) as (Function & { requestPermission?: () => Promise<string> }) | undefined
206
- const supported = view.isSecureContext && typeof constructor === 'function'
229
+ // DeviceMotion exposes no magnetic vector. Do not synthesize one from orientation.
230
+ const supported =
231
+ kind !== 'magneticField' && view.isSecureContext && typeof constructor === 'function'
207
232
  let permission: 'granted' | 'denied' | 'required' = constructor?.requestPermission
208
233
  ? 'required'
209
234
  : 'granted'
@@ -212,6 +237,8 @@ export function createSensor(
212
237
  held = false,
213
238
  available = false,
214
239
  reason: string | undefined
240
+ let replay: { session: SensorReplaySession; orientation: number } | undefined
241
+ let sampleSource: 'device-event' | 'replay' | 'none' = 'none'
215
242
  let timestamp: number | null = null,
216
243
  samples = 0,
217
244
  reference = identity(),
@@ -242,18 +269,20 @@ export function createSensor(
242
269
  rebase = true
243
270
  timestamp = null
244
271
  }
245
- let rawGravity = [0, 0, 0]
272
+ let rawVector = [0, 0, 0]
246
273
  const paint = () => {
247
274
  const angle =
248
275
  options.coordinates === 'device'
249
276
  ? 0
250
- : (view.screen.orientation?.angle ??
251
- (view as unknown as { orientation?: number }).orientation ??
252
- 0)
277
+ : replay
278
+ ? replay.orientation
279
+ : (view.screen.orientation?.angle ??
280
+ (view as unknown as { orientation?: number }).orientation ??
281
+ 0)
253
282
  values =
254
283
  kind === 'rotation'
255
284
  ? rotationValues(relative, angle, values.slice(3))
256
- : interfaceVector(rawGravity, angle)
285
+ : interfaceVector(rawVector, angle)
257
286
  values.forEach((value, index) => {
258
287
  if (!nodes[index]!.node.disposed) nodes[index]!.node.set(value)
259
288
  })
@@ -265,8 +294,37 @@ export function createSensor(
265
294
  }
266
295
  const update = () => {
267
296
  if (ended) return
297
+ if (replay) {
298
+ const current = replay
299
+ detach?.()
300
+ detach = undefined
301
+ timeout?.()
302
+ timeout = undefined
303
+ available = false
304
+ reason = 'synthetic-replay'
305
+ const paused =
306
+ held ||
307
+ host.document.hidden ||
308
+ (options.reducedMotion !== 'continue' && host.reduced())
309
+ const active = !paused && demand.size > 0
310
+ status(
311
+ paused
312
+ ? 'paused'
313
+ : !demand.size
314
+ ? 'idle'
315
+ : sampleSource === 'replay'
316
+ ? 'active'
317
+ : 'starting',
318
+ )
319
+ if (!ended && replay === current) current.session.allowed(active)
320
+ return
321
+ }
268
322
  if (!supported) {
269
- reason = view.isSecureContext ? 'missing-api' : 'insecure-context'
323
+ reason = !view.isSecureContext
324
+ ? 'insecure-context'
325
+ : kind === 'magneticField'
326
+ ? 'unsupported-kind'
327
+ : 'missing-api'
270
328
  stop()
271
329
  status('unavailable')
272
330
  return
@@ -322,10 +380,11 @@ export function createSensor(
322
380
  reset = false
323
381
  }
324
382
  relative = multiply(inverse(reference), q)
325
- } else rawGravity = sample.values
383
+ } else rawVector = sample.values
326
384
  timestamp = sample.timestamp
327
385
  samples++
328
386
  available = true
387
+ sampleSource = 'device-event'
329
388
  reason = undefined
330
389
  timeout?.()
331
390
  timeout = undefined
@@ -339,6 +398,7 @@ export function createSensor(
339
398
  }
340
399
  const handle = {
341
400
  kind,
401
+ unit: sensorUnits[kind],
342
402
  x: nodes[0]!.value,
343
403
  y: nodes[1]!.value,
344
404
  z: nodes[2]!.value,
@@ -401,17 +461,21 @@ export function createSensor(
401
461
  return pending!
402
462
  },
403
463
  snapshot: () => ({
464
+ unit: sensorUnits[kind],
404
465
  status: state,
405
466
  available: available && !ended,
406
467
  timestamp,
407
468
  samples,
408
469
  values: [...values],
409
470
  reason,
410
- backend: !supported
411
- ? ('none' as const)
412
- : kind === 'rotation'
413
- ? ('device-orientation' as const)
414
- : ('device-motion' as const),
471
+ sampleSource,
472
+ backend: replay
473
+ ? ('replay' as const)
474
+ : !supported
475
+ ? ('none' as const)
476
+ : kind === 'rotation'
477
+ ? ('device-orientation' as const)
478
+ : ('device-motion' as const),
415
479
  }),
416
480
  pause() {
417
481
  if (!ended) {
@@ -431,6 +495,8 @@ export function createSensor(
431
495
  dispose() {
432
496
  if (ended) return
433
497
  ended = true
498
+ replay?.session.dispose()
499
+ replay = undefined
434
500
  stop()
435
501
  nodes.forEach((n) => n.node.dispose())
436
502
  off
@@ -439,8 +505,64 @@ export function createSensor(
439
505
  .forEach((fn) => fn())
440
506
  status('disposed')
441
507
  },
442
- } as WebRotationSensor | WebGravitySensor
508
+ } as WebRotationSensor | WebVectorSensor
443
509
  try {
510
+ off.push(
511
+ registerSensorReplay(handle, (session) => {
512
+ if (ended) throw new Error('Sensor is disposed.')
513
+ replay?.session.dispose()
514
+ if (ended) throw new Error('Sensor was disposed during replay replacement.')
515
+ stop()
516
+ const current = { session, orientation: 0 }
517
+ replay = current
518
+ available = false
519
+ sampleSource = 'none'
520
+ rebase = true
521
+ reset = true
522
+ update()
523
+ return {
524
+ now: () => host.scheduler.now(),
525
+ after: (ms, action) => host.after(ms, action),
526
+ inject(sample) {
527
+ if (
528
+ ended ||
529
+ replay !== current ||
530
+ held ||
531
+ !demand.size ||
532
+ host.document.hidden ||
533
+ (options.reducedMotion !== 'continue' && host.reduced())
534
+ )
535
+ return
536
+ current.orientation = sample.orientation ?? 0
537
+ if (kind === 'rotation') {
538
+ const length = Math.hypot(...sample.values)
539
+ const q = sample.values.map((v) => v / length) as Q
540
+ if (rebase) {
541
+ if (reset) relative = identity()
542
+ reference = multiply(q, inverse(relative))
543
+ rebase = false
544
+ reset = false
545
+ }
546
+ relative = multiply(inverse(reference), q)
547
+ } else rawVector = [...sample.values]
548
+ timestamp = sample.time
549
+ samples++
550
+ sampleSource = 'replay'
551
+ available = false
552
+ reason = 'synthetic-replay'
553
+ paint()
554
+ status('active')
555
+ },
556
+ end() {
557
+ if (replay !== current) return
558
+ replay = undefined
559
+ available = false
560
+ stop()
561
+ update()
562
+ },
563
+ }
564
+ }),
565
+ )
444
566
  view.screen.orientation?.addEventListener('change', orientationChanged)
445
567
  view.addEventListener('orientationchange', orientationChanged)
446
568
  off.push(
package/src/web/shape.ts CHANGED
@@ -31,6 +31,8 @@ export interface WebShapeOptions extends WebShapeValues {
31
31
  lineCap?: 'butt' | 'round' | 'square'
32
32
  lineJoin?: 'miter' | 'round' | 'bevel'
33
33
  fillRule?: 'nonzero' | 'evenodd'
34
+ /** Default stop/dispose on DOM removal; pause retains the drawing for reattachment. */
35
+ unload?: 'stop' | 'pause'
34
36
  }
35
37
  export interface WebShape {
36
38
  readonly disposed: boolean
@@ -61,6 +63,7 @@ export function createShape(
61
63
  effectTarget(target, host)
62
64
  effectOptions(options, [
63
65
  'd',
66
+ 'unload',
64
67
  'viewBox',
65
68
  'stroke',
66
69
  'fill',
@@ -71,6 +74,12 @@ export function createShape(
71
74
  'strokeStart',
72
75
  'strokeEnd',
73
76
  ])
77
+ if (
78
+ options.unload !== undefined &&
79
+ options.unload !== 'stop' &&
80
+ options.unload !== 'pause'
81
+ )
82
+ throw new TypeError('Invalid shape unload policy.')
74
83
  let frame = resolve(
75
84
  { path: shapePath(options.d), start: 0, end: 1 },
76
85
  { strokeStart: options.strokeStart, strokeEnd: options.strokeEnd },
@@ -114,6 +123,7 @@ export function createShape(
114
123
  layer.append(svg)
115
124
  let ended = false,
116
125
  held = false,
126
+ detached = false,
117
127
  restart = false
118
128
  let active: EffectAnimation | undefined
119
129
  const off: (() => void)[] = []
@@ -142,14 +152,32 @@ export function createShape(
142
152
  layer.remove()
143
153
  for (const release of off.splice(0).reverse()) release()
144
154
  }
145
- const valid = () =>
146
- target.isConnected &&
155
+ const intact = () =>
147
156
  target.ownerDocument === host.document &&
148
157
  layer.parentNode === target &&
149
158
  svg.parentNode === layer
159
+ const valid = () => target.isConnected && intact()
160
+ const hold = () => {
161
+ if (active && !active.paused) {
162
+ restart = true
163
+ active.pause()
164
+ }
165
+ }
166
+ const continuePlayback = () => {
167
+ if (held || detached || ended) return
168
+ if (restart) active?.play()
169
+ restart = false
170
+ }
171
+ const retainDetached = () => {
172
+ if (ended || options.unload !== 'pause' || target.isConnected || !intact())
173
+ return false
174
+ detached = true
175
+ hold()
176
+ return true
177
+ }
150
178
  const assert = () => {
151
179
  if (ended || !valid()) {
152
- dispose()
180
+ if (!retainDetached()) dispose()
153
181
  throw new Error('Shape is disposed or detached.')
154
182
  }
155
183
  }
@@ -194,7 +222,7 @@ export function createShape(
194
222
  render(completed ? track.end : time)
195
223
  active = undefined
196
224
  })
197
- if (held) {
225
+ if (held || detached) {
198
226
  active.pause()
199
227
  restart = true
200
228
  }
@@ -217,14 +245,13 @@ export function createShape(
217
245
  suspend() {
218
246
  if (held || ended) return
219
247
  held = true
220
- restart = !!active && !active.paused
221
- active?.pause()
248
+ hold()
222
249
  },
223
250
  resume() {
224
251
  if (!held || ended) return
225
252
  held = false
226
- if (restart) active?.play()
227
- restart = false
253
+ if (valid()) detached = false
254
+ continuePlayback()
228
255
  },
229
256
  dispose,
230
257
  }
@@ -246,9 +273,15 @@ export function createShape(
246
273
  play: handle.resume,
247
274
  cancel: stop,
248
275
  dispose,
276
+ retainDetached,
249
277
  },
250
278
  () => {
251
- if (!valid()) dispose()
279
+ if (!valid()) {
280
+ if (!retainDetached()) dispose()
281
+ } else if (detached) {
282
+ detached = false
283
+ continuePlayback()
284
+ }
252
285
  },
253
286
  ),
254
287
  )
@@ -0,0 +1,102 @@
1
+ import { createEffect, createSignal, getOwner, onCleanup, type Accessor } from 'solid-js'
2
+ import {
3
+ animateBinding,
4
+ presenceBinding,
5
+ type PresenceOptions,
6
+ motionBinding,
7
+ type MotionSetup,
8
+ pressBinding,
9
+ type AnimateValue,
10
+ type PressValue,
11
+ type BindingOptions,
12
+ } from '../frameworks/binding.js'
13
+ export type {
14
+ AnimateValue,
15
+ PressValue,
16
+ BindingOptions,
17
+ MotionSetup,
18
+ } from '../frameworks/binding.js'
19
+
20
+ export function createDirectives(options: BindingOptions = {}) {
21
+ return {
22
+ motion(node: HTMLElement, setup: Accessor<MotionSetup>) {
23
+ if (!getOwner()) throw new Error('Motion directives require a Solid owner.')
24
+ let binding: ReturnType<typeof motionBinding> | undefined
25
+ onCleanup(() => binding?.destroy())
26
+ createEffect(() => {
27
+ if (binding) binding.update(setup())
28
+ else binding = motionBinding(node, setup(), options)
29
+ })
30
+ },
31
+ animate(node: HTMLElement, value: Accessor<AnimateValue | undefined>) {
32
+ if (!getOwner()) throw new Error('Motion directives require a Solid owner.')
33
+ // First effect applies the declaration after render; server effects do not run.
34
+ let live: ReturnType<typeof animateBinding> | undefined
35
+ onCleanup(() => live?.destroy())
36
+ createEffect(() => {
37
+ live ??= animateBinding(node, undefined, options)
38
+ live.update(value())
39
+ })
40
+ },
41
+ press(node: HTMLElement, value: Accessor<PressValue | undefined>) {
42
+ if (!getOwner()) throw new Error('Motion directives require a Solid owner.')
43
+ let live: ReturnType<typeof pressBinding> | undefined
44
+ onCleanup(() => live?.destroy())
45
+ createEffect(() => {
46
+ if (!live) live = pressBinding(node, value(), options)
47
+ else live.update(value())
48
+ })
49
+ },
50
+ }
51
+ }
52
+ export const { animate, press, motion } = createDirectives()
53
+
54
+ export type { PresenceOptions } from '../frameworks/binding.js'
55
+ /** Use `show()` with Show and `use:presence` on one retained root. SSR reads only the boolean. */
56
+ export function createPresence(
57
+ present: Accessor<boolean>,
58
+ options: PresenceOptions = {},
59
+ ) {
60
+ if (!getOwner()) throw new Error('Presence requires a Solid owner.')
61
+ const [retained, retain] = createSignal(present())
62
+ let binding: ReturnType<typeof presenceBinding> | undefined,
63
+ disposed = false
64
+ onCleanup(() => {
65
+ disposed = true
66
+ binding?.destroy()
67
+ binding = undefined
68
+ })
69
+ createEffect(() => {
70
+ const next = present()
71
+ if (next) retain(true)
72
+ binding?.update(next)
73
+ if (!binding && !next) retain(false)
74
+ })
75
+ return {
76
+ show: () => present() || retained(),
77
+ presence(node: HTMLElement) {
78
+ let current: ReturnType<typeof presenceBinding> | undefined
79
+ onCleanup(() => {
80
+ current?.destroy()
81
+ if (binding === current) binding = undefined
82
+ })
83
+ // A newly cloned Solid template can still belong to an inert document.
84
+ // Like the other directives, acquire its runtime only after insertion.
85
+ createEffect(() => {
86
+ if (!current) {
87
+ if (binding && !binding.disposed)
88
+ throw new Error('Presence requires exactly one root.')
89
+ current = presenceBinding(
90
+ node,
91
+ present(),
92
+ () => {
93
+ if (!disposed && !present()) retain(false)
94
+ },
95
+ options,
96
+ )
97
+ binding = current
98
+ } else current.update(present())
99
+ })
100
+ },
101
+ }
102
+ }
@@ -0,0 +1,88 @@
1
+ import { onDestroy } from 'svelte'
2
+ import { writable } from 'svelte/store'
3
+ import {
4
+ animateBinding,
5
+ presenceBinding,
6
+ type PresenceOptions,
7
+ motionBinding,
8
+ type MotionSetup,
9
+ pressBinding,
10
+ type AnimateValue,
11
+ type PressValue,
12
+ type BindingOptions,
13
+ } from '../frameworks/binding.js'
14
+ export type {
15
+ AnimateValue,
16
+ PressValue,
17
+ BindingOptions,
18
+ MotionSetup,
19
+ } from '../frameworks/binding.js'
20
+
21
+ /** Svelte DOM actions; each action owns only its own resources. */
22
+ export function animate(node: HTMLElement, value?: AnimateValue) {
23
+ return animateBinding(node, value)
24
+ }
25
+ export function press(node: HTMLElement, value?: PressValue) {
26
+ return pressBinding(node, value)
27
+ }
28
+ /** Create actions borrowing an application-owned runtime. */
29
+ export function createActions(options: BindingOptions) {
30
+ return {
31
+ motion: (node: HTMLElement, setup: MotionSetup) =>
32
+ motionBinding(node, setup, options),
33
+ animate: (node: HTMLElement, value?: AnimateValue) =>
34
+ animateBinding(node, value, options),
35
+ press: (node: HTMLElement, value?: PressValue) => pressBinding(node, value, options),
36
+ }
37
+ }
38
+
39
+ /** Scoped setup for values, pointer, drag, scroll, timelines, layout and diagnostics. */
40
+ export function motion(node: HTMLElement, setup: MotionSetup) {
41
+ return motionBinding(node, setup)
42
+ }
43
+
44
+ export type { PresenceOptions } from '../frameworks/binding.js'
45
+ /** A readable retained-presence store plus its DOM action. Create during component initialization. */
46
+ export function createPresence(initial: boolean, options: PresenceOptions = {}) {
47
+ const retained = writable(initial)
48
+ let present = initial,
49
+ disposed = false,
50
+ binding: ReturnType<typeof presenceBinding> | undefined
51
+ function destroy() {
52
+ if (disposed) return
53
+ disposed = true
54
+ binding?.destroy()
55
+ binding = undefined
56
+ }
57
+ onDestroy(destroy)
58
+ return {
59
+ subscribe: retained.subscribe,
60
+ set(next: boolean) {
61
+ if (disposed) return
62
+ present = next
63
+ if (next) retained.set(true)
64
+ binding?.update(next)
65
+ if (!binding && !next) retained.set(false)
66
+ },
67
+ presence(node: HTMLElement) {
68
+ if (disposed) throw new Error('Presence owner is disposed.')
69
+ if (binding) throw new Error('Presence requires exactly one root.')
70
+ const current = presenceBinding(
71
+ node,
72
+ present,
73
+ () => {
74
+ if (!present && !disposed) retained.set(false)
75
+ },
76
+ options,
77
+ )
78
+ binding = current
79
+ return {
80
+ destroy() {
81
+ current.destroy()
82
+ if (binding === current) binding = undefined
83
+ },
84
+ }
85
+ },
86
+ destroy,
87
+ }
88
+ }