Avenra
Liquid GlassAPI Reference
Liquid GlassAPI Reference

Spring

The physics primitive driving all animations.

Spring

A damped harmonic oscillator class used to drive all animations in Liquid Glass.

Constructor

new Spring(value: number, stiffness?: number, damping?: number)

ParamTypeDefaultDescription
valuenumberRequiredInitial value of the spring
stiffnessnumber300Spring stiffness (higher = faster movement)
dampingnumber20Damping coefficient (higher = less overshoot)

Props

PropTypeDescription
valuenumberCurrent simulated value
targetnumberThe goal value the spring is moving towards
velocitynumberCurrent velocity of the spring
stiffnessnumberStiffness coefficient
dampingnumberDamping coefficient

Methods

MethodReturnsDescription
setTarget(t: number)voidSet a new goal value
update(dt: number)numberStep the simulation by dt seconds; returns the new value
isSettled()booleanReturns true when the spring has reached the target and stopped moving

Basic Usage

import { Spring } from '@avenra/liquid-glass';

const spring = new Spring(0);
spring.setTarget(100);

function loop() {
  const val = spring.update(1/60); // Assuming 60fps
  console.log('Current value:', val);
  
  if (!spring.isSettled()) {
    requestAnimationFrame(loop);
  }
}
loop();

On this page