@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.e3316bb → 2.26.0-pr.feature-update-to-react-19.361.0d1640d

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.
@@ -0,0 +1,268 @@
1
+ import React from "react"
2
+ import * as THREE from "three"
3
+
4
+ /**
5
+ * Smooth value interpolation utility for animating between values
6
+ * even with rapid updates. Uses THREE.MathUtils.lerp for optimized interpolation.
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * // Basic usage with spring-like animation (similar to react-spring)
11
+ * const interpolator = new ValueInterpolator([0, 0, 0], {
12
+ * speed: 0.1, // Interpolation speed (0-1)
13
+ * easing: 'spring', // Spring-like animation with slight overshoot
14
+ * onChange: (values) => {
15
+ * // Update your objects with interpolated values
16
+ * robot.joints.forEach((joint, i) => {
17
+ * joint.rotation.y = values[i]
18
+ * })
19
+ * }
20
+ * })
21
+ *
22
+ * // Different easing options:
23
+ * // 'linear' - smooth linear interpolation
24
+ * // 'spring' - spring-like with overshoot (similar to react-spring)
25
+ * // 'easeOut' - smooth deceleration
26
+ * // 'easeInOut' - smooth acceleration and deceleration
27
+ *
28
+ * // Set new target values
29
+ * interpolator.setTarget([1.5, -0.8, 2.1])
30
+ *
31
+ * // React hook usage
32
+ * function MyComponent() {
33
+ * const [interpolator, currentValues] = useInterpolation([0, 0, 0], {
34
+ * speed: 0.15,
35
+ * easing: 'spring',
36
+ * onChange: (values) => console.log('Values updated:', values)
37
+ * })
38
+ *
39
+ * useEffect(() => {
40
+ * interpolator.setTarget([1, 2, 3])
41
+ * }, [])
42
+ *
43
+ * return <div>Current values: {currentValues.join(', ')}</div>
44
+ * }
45
+ * ```
46
+ */
47
+
48
+ export type EasingFunction = "linear" | "spring" | "easeOut" | "easeInOut"
49
+
50
+ export interface InterpolationOptions {
51
+ /** Interpolation speed factor (0-1, where 1 is instant) */
52
+ speed?: number
53
+ /** Minimum threshold to consider interpolation complete */
54
+ threshold?: number
55
+ /** Easing function to use for interpolation */
56
+ easing?: EasingFunction
57
+ /** Callback when values change during interpolation */
58
+ onChange?: (values: number[]) => void
59
+ /** Callback when interpolation reaches target values */
60
+ onComplete?: (values: number[]) => void
61
+ }
62
+
63
+ export class ValueInterpolator {
64
+ private currentValues: number[] = []
65
+ private targetValues: number[] = []
66
+ private animationId: number | null = null
67
+ private options: Required<InterpolationOptions>
68
+
69
+ constructor(
70
+ initialValues: number[] = [],
71
+ options: InterpolationOptions = {},
72
+ ) {
73
+ this.options = {
74
+ speed: 0.1,
75
+ threshold: 0.001,
76
+ easing: "spring",
77
+ onChange: () => {},
78
+ onComplete: () => {},
79
+ ...options,
80
+ }
81
+
82
+ this.currentValues = [...initialValues]
83
+ this.targetValues = [...initialValues]
84
+ }
85
+
86
+ /**
87
+ * Set new target values to interpolate towards
88
+ */
89
+ setTarget(newValues: number[]): void {
90
+ this.targetValues = [...newValues]
91
+
92
+ // Ensure currentValues array has the same length
93
+ while (this.currentValues.length < newValues.length) {
94
+ this.currentValues.push(newValues[this.currentValues.length])
95
+ }
96
+ if (this.currentValues.length > newValues.length) {
97
+ this.currentValues = this.currentValues.slice(0, newValues.length)
98
+ }
99
+
100
+ this.startInterpolation()
101
+ }
102
+
103
+ /**
104
+ * Get the current interpolated values
105
+ */
106
+ getCurrentValues(): number[] {
107
+ return [...this.currentValues]
108
+ }
109
+
110
+ /**
111
+ * Get a specific interpolated value by index
112
+ */
113
+ getValue(index: number): number {
114
+ return this.currentValues[index] ?? 0
115
+ }
116
+
117
+ /**
118
+ * Check if interpolation is currently active
119
+ */
120
+ isInterpolating(): boolean {
121
+ return this.animationId !== null
122
+ }
123
+
124
+ /**
125
+ * Stop the current interpolation
126
+ */
127
+ stop(): void {
128
+ if (this.animationId !== null) {
129
+ cancelAnimationFrame(this.animationId)
130
+ this.animationId = null
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Instantly set values without interpolation
136
+ */
137
+ setImmediate(values: number[]): void {
138
+ this.stop()
139
+ this.currentValues = [...values]
140
+ this.targetValues = [...values]
141
+ this.options.onChange(this.currentValues)
142
+ }
143
+
144
+ /**
145
+ * Update interpolation options
146
+ */
147
+ updateOptions(newOptions: Partial<InterpolationOptions>): void {
148
+ this.options = { ...this.options, ...newOptions }
149
+ }
150
+
151
+ /**
152
+ * Destroy the interpolator and clean up resources
153
+ */
154
+ destroy(): void {
155
+ this.stop()
156
+ }
157
+
158
+ /**
159
+ * Apply easing function to the interpolation
160
+ */
161
+ private applyEasing(current: number, target: number, speed: number): number {
162
+ switch (this.options.easing) {
163
+ case "linear":
164
+ return THREE.MathUtils.lerp(current, target, speed)
165
+
166
+ case "spring":
167
+ // Improved spring-like easing with better convergence
168
+ const diff = target - current
169
+ // Use a modified approach that maintains spring feel but ensures convergence
170
+ const springAlpha = Math.min(speed * 1.5, 0.8) // Cap to prevent overshooting too much
171
+ return THREE.MathUtils.lerp(current, target, springAlpha)
172
+
173
+ case "easeOut":
174
+ // Ease out cubic for smooth deceleration
175
+ const t = Math.min(speed * 2, 1)
176
+ const easedT = 1 - Math.pow(1 - t, 3)
177
+ return THREE.MathUtils.lerp(current, target, easedT)
178
+
179
+ case "easeInOut":
180
+ // Ease in-out for smooth acceleration and deceleration
181
+ const t2 = Math.min(speed * 2, 1)
182
+ const easedT2 =
183
+ t2 < 0.5 ? 4 * t2 * t2 * t2 : 1 - Math.pow(-2 * t2 + 2, 3) / 2
184
+ return THREE.MathUtils.lerp(current, target, easedT2)
185
+
186
+ default:
187
+ return THREE.MathUtils.lerp(current, target, speed)
188
+ }
189
+ }
190
+
191
+ private startInterpolation(): void {
192
+ if (this.animationId !== null) {
193
+ return // Already interpolating
194
+ }
195
+
196
+ this.animate()
197
+ }
198
+
199
+ private animate = (): void => {
200
+ let hasChanges = false
201
+ let isComplete = true
202
+
203
+ for (let i = 0; i < this.currentValues.length; i++) {
204
+ const current = this.currentValues[i]
205
+ const target = this.targetValues[i]
206
+ const diff = Math.abs(target - current)
207
+
208
+ if (diff > this.options.threshold) {
209
+ isComplete = false
210
+ const newValue = this.applyEasing(current, target, this.options.speed)
211
+ this.currentValues[i] = newValue
212
+ hasChanges = true
213
+ } else if (current !== target) {
214
+ // Snap to target if within threshold
215
+ this.currentValues[i] = target
216
+ hasChanges = true
217
+ }
218
+ }
219
+
220
+ if (hasChanges) {
221
+ this.options.onChange(this.currentValues)
222
+ }
223
+
224
+ if (isComplete) {
225
+ this.animationId = null
226
+ this.options.onComplete(this.currentValues)
227
+ } else {
228
+ this.animationId = requestAnimationFrame(this.animate)
229
+ }
230
+ }
231
+ }
232
+
233
+ /**
234
+ * React hook for using the ValueInterpolator
235
+ */
236
+ export function useInterpolation(
237
+ initialValues: number[] = [],
238
+ options: InterpolationOptions = {},
239
+ ): [ValueInterpolator, number[]] {
240
+ const interpolatorRef = React.useRef<ValueInterpolator | null>(null)
241
+ const [currentValues, setCurrentValues] =
242
+ React.useState<number[]>(initialValues)
243
+
244
+ // Initialize interpolator
245
+ if (!interpolatorRef.current) {
246
+ interpolatorRef.current = new ValueInterpolator(initialValues, {
247
+ ...options,
248
+ onChange: (values) => {
249
+ setCurrentValues([...values])
250
+ options.onChange?.(values)
251
+ },
252
+ })
253
+ }
254
+
255
+ // Update options when they change
256
+ React.useEffect(() => {
257
+ interpolatorRef.current?.updateOptions(options)
258
+ }, [options])
259
+
260
+ // Cleanup on unmount
261
+ React.useEffect(() => {
262
+ return () => {
263
+ interpolatorRef.current?.destroy()
264
+ }
265
+ }, [])
266
+
267
+ return [interpolatorRef.current!, currentValues]
268
+ }
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ export * from "./components/robots/SupportedRobot"
16
16
  export * from "./components/safetyBar/SafetyBar"
17
17
  export * from "./components/SelectableFab"
18
18
  export * from "./components/utils/hooks"
19
+ export * from "./components/utils/interpolation"
19
20
  export * from "./components/VelocitySlider"
20
21
  export * from "./components/wandelscript-editor/WandelscriptEditor"
21
22
  export * from "./i18n/config"