@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.c3fd41e → 2.26.0-pr.feature-update-to-react-19.361.1e3686f

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wandelbots/wandelbots-js-react-components",
3
- "version": "2.26.0-pr.feature-update-to-react-19.361.c3fd41e",
3
+ "version": "2.26.0-pr.feature-update-to-react-19.361.1e3686f",
4
4
  "description": "React UI toolkit for building applications on top of the Wandelbots platform",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -35,8 +35,8 @@ export default function RobotAnimator({
35
35
  )
36
36
 
37
37
  interpolatorRef.current = new ValueInterpolator(initialJointValues, {
38
- tension: 120, // Spring tension for smooth animation
39
- friction: 20, // Spring friction for natural settling
38
+ tension: 120, // Controls spring stiffness - higher values create faster, more responsive motion
39
+ friction: 20, // Controls damping - higher values reduce oscillation and create smoother settling
40
40
  threshold: 0.001,
41
41
  })
42
42
 
@@ -45,13 +45,13 @@ export default function RobotAnimator({
45
45
  }
46
46
  }, [])
47
47
 
48
- // Frame-rate independent animation loop
48
+ // Animation loop that runs at the display's refresh rate
49
49
  useFrame((state, delta) => {
50
50
  if (interpolatorRef.current) {
51
51
  const isComplete = interpolatorRef.current.update(delta)
52
52
  setRotation()
53
53
 
54
- // Only invalidate if interpolation is still active
54
+ // Trigger a re-render only if the animation is still running
55
55
  if (!isComplete) {
56
56
  invalidate()
57
57
  }
@@ -436,4 +436,32 @@ describe("ValueInterpolator", () => {
436
436
  expect(result[0]).toBeLessThan(50) // Should be less than the final target due to interpolation
437
437
  })
438
438
  })
439
+
440
+ it("should provide smooth initialization without jumpiness", () => {
441
+ const interpolator = new ValueInterpolator([0], {
442
+ tension: 120,
443
+ friction: 20,
444
+ })
445
+
446
+ interpolator.setTarget([10]) // Large target change
447
+
448
+ const firstFrameValues: number[] = []
449
+
450
+ // Capture values from first few frames
451
+ for (let frame = 0; frame < 5; frame++) {
452
+ interpolator.update(1 / 60)
453
+ firstFrameValues.push(interpolator.getValue(0))
454
+ }
455
+
456
+ // First frame should not jump too aggressively
457
+ expect(firstFrameValues[0]).toBeLessThan(2) // Should start gently
458
+ expect(firstFrameValues[0]).toBeGreaterThan(0) // But should move
459
+
460
+ // Should show gradual acceleration, not sudden jumps
461
+ const firstStep = firstFrameValues[1] - firstFrameValues[0]
462
+ const secondStep = firstFrameValues[2] - firstFrameValues[1]
463
+
464
+ expect(secondStep).toBeGreaterThanOrEqual(firstStep) // Should accelerate gradually
465
+ expect(firstStep).toBeLessThan(1.5) // First step should be modest
466
+ })
439
467
  })
@@ -63,6 +63,7 @@ export class ValueInterpolator {
63
63
  private targetUpdateTime: number = 0
64
64
  private animationId: number | null = null
65
65
  private options: Required<InterpolationOptions>
66
+ private updateCount: number = 0 // Track how many updates have occurred
66
67
 
67
68
  // Spring physics state
68
69
  private velocities: number[] = []
@@ -85,48 +86,65 @@ export class ValueInterpolator {
85
86
  this.previousTargetValues = [...initialValues]
86
87
  this.velocities = new Array(initialValues.length).fill(0)
87
88
  this.targetUpdateTime = performance.now()
89
+ this.updateCount = 0
88
90
  }
89
91
 
90
92
  /**
91
- * Update interpolation using spring physics (call this in useFrame for frame-rate independence)
92
- * @param delta - Frame delta time for smooth animation
93
+ * Update interpolation using spring physics
94
+ *
95
+ * Call this method every frame for smooth animation. In React Three Fiber,
96
+ * call from within useFrame callback with the provided delta time.
97
+ *
98
+ * @param delta - Time elapsed since last update in seconds (e.g., 1/60 ≈ 0.0167 for 60fps)
99
+ * @returns true when interpolation is complete (all values reached their targets)
93
100
  */
94
101
  update(delta: number = 1 / 60): boolean {
95
102
  let hasChanges = false
96
103
  let isComplete = true
97
104
 
98
- // Clamp delta to prevent instability from large frame drops
99
- const clampedDelta = Math.min(delta, 1 / 15) // Max 66ms frame time
105
+ // Increment update counter for initialization smoothing
106
+ this.updateCount++
107
+
108
+ // Limit delta to prevent physics instability during large frame drops
109
+ const clampedDelta = Math.min(delta, 1 / 15) // Maximum 66ms frame time allowed
110
+
111
+ // Apply gentle ramp-up for the first few frames to prevent initial jumpiness
112
+ const initializationFactor =
113
+ this.updateCount <= 3
114
+ ? Math.min(this.updateCount / 3, 1) // Gradual ramp over first 3 frames
115
+ : 1
100
116
 
101
117
  for (let i = 0; i < this.currentValues.length; i++) {
102
118
  const current = this.currentValues[i]
103
119
  const target = this.targetValues[i]
104
120
  const velocity = this.velocities[i]
105
121
 
106
- // Spring physics calculation
122
+ // Calculate spring physics forces
107
123
  const displacement = target - current
108
- const springForce = displacement * this.options.tension
124
+ const springForce =
125
+ displacement * this.options.tension * initializationFactor
109
126
  const dampingForce = velocity * this.options.friction
110
127
 
111
- // Net force and acceleration
128
+ // Calculate acceleration from net force (F = ma, assuming mass = 1)
112
129
  const acceleration = springForce - dampingForce
113
130
 
114
- // Update velocity and position using Verlet integration for stability
131
+ // Integrate physics using Verlet method for numerical stability
115
132
  const newVelocity = velocity + acceleration * clampedDelta
116
133
  const newValue = current + newVelocity * clampedDelta
117
134
 
118
- // Check if we're close enough to target and velocity is low enough
135
+ // Determine if this value has settled (close to target with low velocity)
119
136
  const isValueComplete =
120
137
  Math.abs(displacement) < this.options.threshold &&
121
138
  Math.abs(newVelocity) < this.options.threshold * 10
122
139
 
123
140
  if (!isValueComplete) {
124
141
  isComplete = false
142
+ // Continue spring animation
125
143
  this.currentValues[i] = newValue
126
144
  this.velocities[i] = newVelocity
127
145
  hasChanges = true
128
146
  } else {
129
- // Snap to target when close enough
147
+ // Snap exactly to target when close enough (prevents endless micro-movements)
130
148
  if (this.currentValues[i] !== target) {
131
149
  this.currentValues[i] = target
132
150
  this.velocities[i] = 0
@@ -147,8 +165,10 @@ export class ValueInterpolator {
147
165
  }
148
166
 
149
167
  /**
150
- * Set new target values to interpolate towards
151
- * Handles irregular target updates smoothly by considering update frequency
168
+ * Set new target values for the interpolation to move towards
169
+ *
170
+ * Includes smart blending for very rapid target updates (faster than 120fps)
171
+ * to prevent jarring movements when targets change frequently.
152
172
  */
153
173
  setTarget(newValues: number[]): void {
154
174
  const now = performance.now()
@@ -159,21 +179,24 @@ export class ValueInterpolator {
159
179
  this.targetValues = [...newValues]
160
180
  this.targetUpdateTime = now
161
181
 
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
182
+ // Reset update counter for smooth initialization when target changes
183
+ this.updateCount = 0
184
+
185
+ // Apply target blending for extremely rapid updates to prevent jarring jumps
186
+ // Only activates when targets change faster than 120fps (< 8ms between updates)
164
187
  if (
165
188
  timeSinceLastUpdate < 8 &&
166
189
  timeSinceLastUpdate > 0 &&
167
190
  this.previousTargetValues.length > 0
168
191
  ) {
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
192
+ // Blend between previous and new target based on time elapsed
193
+ const blendFactor = Math.min(timeSinceLastUpdate / 8, 1) // 0 to 1 over 8ms
171
194
 
172
195
  for (let i = 0; i < this.targetValues.length; i++) {
173
196
  const prev = this.previousTargetValues[i] || 0
174
197
  const next = newValues[i] || 0
175
198
 
176
- // Only blend if the change is significant (> 10% of the range)
199
+ // Only blend significant changes to avoid unnecessary smoothing
177
200
  const change = Math.abs(next - prev)
178
201
  if (change > 0.1) {
179
202
  this.targetValues[i] = prev + (next - prev) * blendFactor
@@ -181,43 +204,49 @@ export class ValueInterpolator {
181
204
  }
182
205
  }
183
206
 
184
- // Ensure arrays have the same length
207
+ // Ensure value and velocity arrays have matching lengths
185
208
  while (this.currentValues.length < newValues.length) {
186
209
  this.currentValues.push(newValues[this.currentValues.length])
187
- this.velocities.push(0) // Initialize velocity for new values
210
+ this.velocities.push(0) // New values start with zero velocity
188
211
  }
189
212
  if (this.currentValues.length > newValues.length) {
190
213
  this.currentValues = this.currentValues.slice(0, newValues.length)
191
214
  this.velocities = this.velocities.slice(0, newValues.length)
192
215
  }
193
216
 
194
- // Don't auto-start interpolation - let manual update() calls handle it
195
- // This prevents conflicts with useFrame() calls
217
+ // Does not start automatic interpolation - requires manual update() calls
218
+ // This design prevents conflicts when using with React Three Fiber's useFrame
196
219
  }
197
220
 
198
221
  /**
199
- * Get the current interpolated values
222
+ * Get a copy of all current interpolated values
200
223
  */
201
224
  getCurrentValues(): number[] {
202
225
  return [...this.currentValues]
203
226
  }
204
227
 
205
228
  /**
206
- * Get a specific interpolated value by index
229
+ * Get a single interpolated value by its array index
207
230
  */
208
231
  getValue(index: number): number {
209
232
  return this.currentValues[index] ?? 0
210
233
  }
211
234
 
212
235
  /**
213
- * Check if interpolation is currently active
236
+ * Check if automatic interpolation is currently running
237
+ *
238
+ * This only tracks auto-interpolation started with startAutoInterpolation().
239
+ * Manual update() calls are not tracked by this method.
214
240
  */
215
241
  isInterpolating(): boolean {
216
242
  return this.animationId !== null
217
243
  }
218
244
 
219
245
  /**
220
- * Stop the current interpolation
246
+ * Stop automatic interpolation if it's running
247
+ *
248
+ * This cancels the internal animation frame loop but does not affect
249
+ * manual update() calls.
221
250
  */
222
251
  stop(): void {
223
252
  if (this.animationId !== null) {
@@ -236,6 +265,7 @@ export class ValueInterpolator {
236
265
  this.previousTargetValues = [...values]
237
266
  this.velocities = new Array(values.length).fill(0) // Reset velocities
238
267
  this.targetUpdateTime = performance.now()
268
+ this.updateCount = 0 // Reset update counter
239
269
  this.options.onChange(this.currentValues)
240
270
  }
241
271
 
@@ -247,14 +277,21 @@ export class ValueInterpolator {
247
277
  }
248
278
 
249
279
  /**
250
- * Start automatic interpolation (use when not calling update() manually)
280
+ * Start automatic interpolation with an animation loop
281
+ *
282
+ * This begins a requestAnimationFrame loop that calls update() automatically.
283
+ * For React Three Fiber components, prefer using manual update() calls
284
+ * within useFrame hooks instead.
251
285
  */
252
286
  startAutoInterpolation(): void {
253
287
  this.startInterpolation()
254
288
  }
255
289
 
256
290
  /**
257
- * Destroy the interpolator and clean up resources
291
+ * Clean up all resources and stop any running animations
292
+ *
293
+ * This cancels any active animation frames and resets internal state.
294
+ * Call this when the component unmounts or is no longer needed.
258
295
  */
259
296
  destroy(): void {
260
297
  this.stop()
@@ -269,8 +306,8 @@ export class ValueInterpolator {
269
306
  }
270
307
 
271
308
  private animate = (): void => {
272
- // Use the main update method which now handles spring physics
273
- const isComplete = this.update(1 / 60) // Default 60fps for auto-interpolation
309
+ // Use delta timing with a fallback for consistent automatic animations
310
+ const isComplete = this.update(1 / 60) // Simulate 60fps for auto-interpolation
274
311
 
275
312
  if (!isComplete) {
276
313
  this.animationId = requestAnimationFrame(this.animate)