@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.49c57ce → 2.26.0-pr.feature-update-to-react-19.361.c3fd41e
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/components/robots/RobotAnimator.d.ts.map +1 -1
- package/dist/components/utils/interpolation.d.ts +33 -34
- package/dist/components/utils/interpolation.d.ts.map +1 -1
- package/dist/index.cjs +12 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +242 -262
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/robots/RobotAnimator.tsx +8 -4
- package/src/components/utils/interpolation.test.ts +277 -47
- package/src/components/utils/interpolation.ts +113 -115
|
@@ -1,47 +1,36 @@
|
|
|
1
1
|
import React from "react"
|
|
2
|
-
import * as THREE from "three"
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
|
-
* Smooth value interpolation utility
|
|
6
|
-
*
|
|
4
|
+
* Smooth value interpolation utility using spring physics with tension and friction.
|
|
5
|
+
* Designed for React Three Fiber applications with smooth, natural animations.
|
|
7
6
|
*
|
|
8
|
-
*
|
|
9
|
-
* -
|
|
10
|
-
* -
|
|
7
|
+
* Features:
|
|
8
|
+
* - Spring physics with configurable tension and friction
|
|
9
|
+
* - Frame-rate independent using delta timing
|
|
10
|
+
* - Handles irregular frame timing and rapid target updates
|
|
11
|
+
* - Manual update() calls for useFrame integration (no automatic RAF loop)
|
|
11
12
|
* - Direct mutation for performance
|
|
12
13
|
*
|
|
13
14
|
* @example
|
|
14
15
|
* ```tsx
|
|
15
|
-
* // Basic
|
|
16
|
+
* // Basic spring animation
|
|
16
17
|
* const interpolator = new ValueInterpolator([0, 0, 0], {
|
|
17
|
-
*
|
|
18
|
-
*
|
|
18
|
+
* tension: 120, // Higher = faster, stiffer spring (default: 120)
|
|
19
|
+
* friction: 20, // Higher = more damping, less oscillation (default: 20)
|
|
19
20
|
* onChange: (values) => {
|
|
20
|
-
* // Update your objects with interpolated values
|
|
21
21
|
* robot.joints.forEach((joint, i) => {
|
|
22
22
|
* joint.rotation.y = values[i]
|
|
23
23
|
* })
|
|
24
24
|
* }
|
|
25
25
|
* })
|
|
26
26
|
*
|
|
27
|
-
* // Different easing options:
|
|
28
|
-
* // 'linear' - smooth linear interpolation
|
|
29
|
-
* // 'spring' - spring-like with overshoot (similar to react-spring)
|
|
30
|
-
* // 'easeOut' - smooth deceleration
|
|
31
|
-
* // 'easeInOut' - smooth acceleration and deceleration
|
|
32
|
-
*
|
|
33
|
-
* // Set new target values
|
|
34
27
|
* interpolator.setTarget([1.5, -0.8, 2.1])
|
|
35
28
|
*
|
|
36
|
-
* // React
|
|
29
|
+
* // React Three Fiber usage
|
|
37
30
|
* function MyComponent() {
|
|
38
|
-
* const [interpolator] = useInterpolation([0, 0, 0]
|
|
39
|
-
* alpha: 0.1,
|
|
40
|
-
* easing: 'spring'
|
|
41
|
-
* })
|
|
31
|
+
* const [interpolator] = useInterpolation([0, 0, 0])
|
|
42
32
|
*
|
|
43
33
|
* useFrame((state, delta) => {
|
|
44
|
-
* // Frame-rate independent updates
|
|
45
34
|
* interpolator.update(delta)
|
|
46
35
|
* })
|
|
47
36
|
*
|
|
@@ -54,15 +43,13 @@ import * as THREE from "three"
|
|
|
54
43
|
* ```
|
|
55
44
|
*/
|
|
56
45
|
|
|
57
|
-
export type EasingFunction = "linear" | "spring" | "easeOut" | "easeInOut"
|
|
58
|
-
|
|
59
46
|
export interface InterpolationOptions {
|
|
60
|
-
/**
|
|
61
|
-
|
|
62
|
-
/**
|
|
47
|
+
/** Spring tension (higher = faster, stiffer spring) - default: 120 */
|
|
48
|
+
tension?: number
|
|
49
|
+
/** Spring friction (higher = more damping, less oscillation) - default: 20 */
|
|
50
|
+
friction?: number
|
|
51
|
+
/** Minimum threshold to consider interpolation complete - default: 0.001 */
|
|
63
52
|
threshold?: number
|
|
64
|
-
/** Easing function to use for interpolation */
|
|
65
|
-
easing?: EasingFunction
|
|
66
53
|
/** Callback when values change during interpolation */
|
|
67
54
|
onChange?: (values: number[]) => void
|
|
68
55
|
/** Callback when interpolation reaches target values */
|
|
@@ -72,17 +59,22 @@ export interface InterpolationOptions {
|
|
|
72
59
|
export class ValueInterpolator {
|
|
73
60
|
private currentValues: number[] = []
|
|
74
61
|
private targetValues: number[] = []
|
|
62
|
+
private previousTargetValues: number[] = []
|
|
63
|
+
private targetUpdateTime: number = 0
|
|
75
64
|
private animationId: number | null = null
|
|
76
65
|
private options: Required<InterpolationOptions>
|
|
77
66
|
|
|
67
|
+
// Spring physics state
|
|
68
|
+
private velocities: number[] = []
|
|
69
|
+
|
|
78
70
|
constructor(
|
|
79
71
|
initialValues: number[] = [],
|
|
80
72
|
options: InterpolationOptions = {},
|
|
81
73
|
) {
|
|
82
74
|
this.options = {
|
|
83
|
-
|
|
75
|
+
tension: 120,
|
|
76
|
+
friction: 20,
|
|
84
77
|
threshold: 0.001,
|
|
85
|
-
easing: "spring",
|
|
86
78
|
onChange: () => {},
|
|
87
79
|
onComplete: () => {},
|
|
88
80
|
...options,
|
|
@@ -90,36 +82,56 @@ export class ValueInterpolator {
|
|
|
90
82
|
|
|
91
83
|
this.currentValues = [...initialValues]
|
|
92
84
|
this.targetValues = [...initialValues]
|
|
85
|
+
this.previousTargetValues = [...initialValues]
|
|
86
|
+
this.velocities = new Array(initialValues.length).fill(0)
|
|
87
|
+
this.targetUpdateTime = performance.now()
|
|
93
88
|
}
|
|
94
89
|
|
|
95
90
|
/**
|
|
96
|
-
* Update interpolation (call this in useFrame for frame-rate independence)
|
|
91
|
+
* Update interpolation using spring physics (call this in useFrame for frame-rate independence)
|
|
97
92
|
* @param delta - Frame delta time for smooth animation
|
|
98
93
|
*/
|
|
99
94
|
update(delta: number = 1 / 60): boolean {
|
|
100
95
|
let hasChanges = false
|
|
101
96
|
let isComplete = true
|
|
102
97
|
|
|
98
|
+
// Clamp delta to prevent instability from large frame drops
|
|
99
|
+
const clampedDelta = Math.min(delta, 1 / 15) // Max 66ms frame time
|
|
100
|
+
|
|
103
101
|
for (let i = 0; i < this.currentValues.length; i++) {
|
|
104
102
|
const current = this.currentValues[i]
|
|
105
103
|
const target = this.targetValues[i]
|
|
106
|
-
const
|
|
104
|
+
const velocity = this.velocities[i]
|
|
105
|
+
|
|
106
|
+
// Spring physics calculation
|
|
107
|
+
const displacement = target - current
|
|
108
|
+
const springForce = displacement * this.options.tension
|
|
109
|
+
const dampingForce = velocity * this.options.friction
|
|
107
110
|
|
|
108
|
-
|
|
111
|
+
// Net force and acceleration
|
|
112
|
+
const acceleration = springForce - dampingForce
|
|
113
|
+
|
|
114
|
+
// Update velocity and position using Verlet integration for stability
|
|
115
|
+
const newVelocity = velocity + acceleration * clampedDelta
|
|
116
|
+
const newValue = current + newVelocity * clampedDelta
|
|
117
|
+
|
|
118
|
+
// Check if we're close enough to target and velocity is low enough
|
|
119
|
+
const isValueComplete =
|
|
120
|
+
Math.abs(displacement) < this.options.threshold &&
|
|
121
|
+
Math.abs(newVelocity) < this.options.threshold * 10
|
|
122
|
+
|
|
123
|
+
if (!isValueComplete) {
|
|
109
124
|
isComplete = false
|
|
110
|
-
// Frame-rate independent interpolation using delta
|
|
111
|
-
const newValue = this.applyEasing(
|
|
112
|
-
current,
|
|
113
|
-
target,
|
|
114
|
-
this.options.alpha,
|
|
115
|
-
delta,
|
|
116
|
-
)
|
|
117
125
|
this.currentValues[i] = newValue
|
|
126
|
+
this.velocities[i] = newVelocity
|
|
118
127
|
hasChanges = true
|
|
119
|
-
} else
|
|
120
|
-
// Snap to target
|
|
121
|
-
this.currentValues[i]
|
|
122
|
-
|
|
128
|
+
} else {
|
|
129
|
+
// Snap to target when close enough
|
|
130
|
+
if (this.currentValues[i] !== target) {
|
|
131
|
+
this.currentValues[i] = target
|
|
132
|
+
this.velocities[i] = 0
|
|
133
|
+
hasChanges = true
|
|
134
|
+
}
|
|
123
135
|
}
|
|
124
136
|
}
|
|
125
137
|
|
|
@@ -136,19 +148,51 @@ export class ValueInterpolator {
|
|
|
136
148
|
|
|
137
149
|
/**
|
|
138
150
|
* Set new target values to interpolate towards
|
|
151
|
+
* Handles irregular target updates smoothly by considering update frequency
|
|
139
152
|
*/
|
|
140
153
|
setTarget(newValues: number[]): void {
|
|
154
|
+
const now = performance.now()
|
|
155
|
+
const timeSinceLastUpdate = now - this.targetUpdateTime
|
|
156
|
+
|
|
157
|
+
// Store previous target for smooth transitions
|
|
158
|
+
this.previousTargetValues = [...this.targetValues]
|
|
141
159
|
this.targetValues = [...newValues]
|
|
160
|
+
this.targetUpdateTime = now
|
|
161
|
+
|
|
162
|
+
// Only apply blending for extremely rapid updates (< 8ms, roughly faster than 120fps)
|
|
163
|
+
// This helps with irregular robot data but doesn't interfere with normal usage
|
|
164
|
+
if (
|
|
165
|
+
timeSinceLastUpdate < 8 &&
|
|
166
|
+
timeSinceLastUpdate > 0 &&
|
|
167
|
+
this.previousTargetValues.length > 0
|
|
168
|
+
) {
|
|
169
|
+
// Very conservative blending - only smooth the most jarring rapid updates
|
|
170
|
+
const blendFactor = Math.min(timeSinceLastUpdate / 8, 1) // 0-1 based on 8ms window
|
|
171
|
+
|
|
172
|
+
for (let i = 0; i < this.targetValues.length; i++) {
|
|
173
|
+
const prev = this.previousTargetValues[i] || 0
|
|
174
|
+
const next = newValues[i] || 0
|
|
175
|
+
|
|
176
|
+
// Only blend if the change is significant (> 10% of the range)
|
|
177
|
+
const change = Math.abs(next - prev)
|
|
178
|
+
if (change > 0.1) {
|
|
179
|
+
this.targetValues[i] = prev + (next - prev) * blendFactor
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
142
183
|
|
|
143
|
-
// Ensure
|
|
184
|
+
// Ensure arrays have the same length
|
|
144
185
|
while (this.currentValues.length < newValues.length) {
|
|
145
186
|
this.currentValues.push(newValues[this.currentValues.length])
|
|
187
|
+
this.velocities.push(0) // Initialize velocity for new values
|
|
146
188
|
}
|
|
147
189
|
if (this.currentValues.length > newValues.length) {
|
|
148
190
|
this.currentValues = this.currentValues.slice(0, newValues.length)
|
|
191
|
+
this.velocities = this.velocities.slice(0, newValues.length)
|
|
149
192
|
}
|
|
150
193
|
|
|
151
|
-
|
|
194
|
+
// Don't auto-start interpolation - let manual update() calls handle it
|
|
195
|
+
// This prevents conflicts with useFrame() calls
|
|
152
196
|
}
|
|
153
197
|
|
|
154
198
|
/**
|
|
@@ -189,6 +233,9 @@ export class ValueInterpolator {
|
|
|
189
233
|
this.stop()
|
|
190
234
|
this.currentValues = [...values]
|
|
191
235
|
this.targetValues = [...values]
|
|
236
|
+
this.previousTargetValues = [...values]
|
|
237
|
+
this.velocities = new Array(values.length).fill(0) // Reset velocities
|
|
238
|
+
this.targetUpdateTime = performance.now()
|
|
192
239
|
this.options.onChange(this.currentValues)
|
|
193
240
|
}
|
|
194
241
|
|
|
@@ -200,50 +247,17 @@ export class ValueInterpolator {
|
|
|
200
247
|
}
|
|
201
248
|
|
|
202
249
|
/**
|
|
203
|
-
*
|
|
250
|
+
* Start automatic interpolation (use when not calling update() manually)
|
|
204
251
|
*/
|
|
205
|
-
|
|
206
|
-
this.
|
|
252
|
+
startAutoInterpolation(): void {
|
|
253
|
+
this.startInterpolation()
|
|
207
254
|
}
|
|
208
255
|
|
|
209
256
|
/**
|
|
210
|
-
*
|
|
257
|
+
* Destroy the interpolator and clean up resources
|
|
211
258
|
*/
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
target: number,
|
|
215
|
-
alpha: number,
|
|
216
|
-
delta: number = 1,
|
|
217
|
-
): number {
|
|
218
|
-
// Adjust alpha based on delta for frame-rate independence
|
|
219
|
-
// Standard 60fps frame time is ~0.0167, so normalize to that
|
|
220
|
-
const frameRateAdjustedAlpha = Math.min(alpha * (delta * 60), 1)
|
|
221
|
-
|
|
222
|
-
switch (this.options.easing) {
|
|
223
|
-
case "linear":
|
|
224
|
-
return THREE.MathUtils.lerp(current, target, frameRateAdjustedAlpha)
|
|
225
|
-
|
|
226
|
-
case "spring":
|
|
227
|
-
// Spring easing with frame-rate independence
|
|
228
|
-
const springAlpha = Math.min(frameRateAdjustedAlpha * 1.5, 0.8)
|
|
229
|
-
return THREE.MathUtils.lerp(current, target, springAlpha)
|
|
230
|
-
|
|
231
|
-
case "easeOut":
|
|
232
|
-
// Ease out cubic for smooth deceleration
|
|
233
|
-
const t = Math.min(frameRateAdjustedAlpha * 2, 1)
|
|
234
|
-
const easedT = 1 - Math.pow(1 - t, 3)
|
|
235
|
-
return THREE.MathUtils.lerp(current, target, easedT)
|
|
236
|
-
|
|
237
|
-
case "easeInOut":
|
|
238
|
-
// Ease in-out for smooth acceleration and deceleration
|
|
239
|
-
const t2 = Math.min(frameRateAdjustedAlpha * 2, 1)
|
|
240
|
-
const easedT2 =
|
|
241
|
-
t2 < 0.5 ? 4 * t2 * t2 * t2 : 1 - Math.pow(-2 * t2 + 2, 3) / 2
|
|
242
|
-
return THREE.MathUtils.lerp(current, target, easedT2)
|
|
243
|
-
|
|
244
|
-
default:
|
|
245
|
-
return THREE.MathUtils.lerp(current, target, frameRateAdjustedAlpha)
|
|
246
|
-
}
|
|
259
|
+
destroy(): void {
|
|
260
|
+
this.stop()
|
|
247
261
|
}
|
|
248
262
|
|
|
249
263
|
private startInterpolation(): void {
|
|
@@ -255,35 +269,13 @@ export class ValueInterpolator {
|
|
|
255
269
|
}
|
|
256
270
|
|
|
257
271
|
private animate = (): void => {
|
|
258
|
-
|
|
259
|
-
|
|
272
|
+
// Use the main update method which now handles spring physics
|
|
273
|
+
const isComplete = this.update(1 / 60) // Default 60fps for auto-interpolation
|
|
260
274
|
|
|
261
|
-
|
|
262
|
-
const current = this.currentValues[i]
|
|
263
|
-
const target = this.targetValues[i]
|
|
264
|
-
const diff = Math.abs(target - current)
|
|
265
|
-
|
|
266
|
-
if (diff > this.options.threshold) {
|
|
267
|
-
isComplete = false
|
|
268
|
-
const newValue = this.applyEasing(current, target, this.options.alpha)
|
|
269
|
-
this.currentValues[i] = newValue
|
|
270
|
-
hasChanges = true
|
|
271
|
-
} else if (current !== target) {
|
|
272
|
-
// Snap to target if within threshold
|
|
273
|
-
this.currentValues[i] = target
|
|
274
|
-
hasChanges = true
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (hasChanges) {
|
|
279
|
-
this.options.onChange(this.currentValues)
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
if (isComplete) {
|
|
283
|
-
this.animationId = null
|
|
284
|
-
this.options.onComplete(this.currentValues)
|
|
285
|
-
} else {
|
|
275
|
+
if (!isComplete) {
|
|
286
276
|
this.animationId = requestAnimationFrame(this.animate)
|
|
277
|
+
} else {
|
|
278
|
+
this.animationId = null
|
|
287
279
|
}
|
|
288
280
|
}
|
|
289
281
|
}
|
|
@@ -291,10 +283,16 @@ export class ValueInterpolator {
|
|
|
291
283
|
/**
|
|
292
284
|
* React hook for using the ValueInterpolator with useFrame
|
|
293
285
|
*
|
|
286
|
+
* This hook creates a ValueInterpolator that uses spring physics for smooth,
|
|
287
|
+
* natural animations. Call interpolator.update(delta) in your useFrame callback.
|
|
288
|
+
*
|
|
294
289
|
* @example
|
|
295
290
|
* ```tsx
|
|
296
291
|
* function AnimatedMesh() {
|
|
297
|
-
* const [interpolator] = useInterpolation([0, 0, 0], {
|
|
292
|
+
* const [interpolator] = useInterpolation([0, 0, 0], {
|
|
293
|
+
* tension: 120, // Higher = faster spring
|
|
294
|
+
* friction: 20 // Higher = more damping
|
|
295
|
+
* })
|
|
298
296
|
* const meshRef = useRef()
|
|
299
297
|
*
|
|
300
298
|
* useFrame((state, delta) => {
|