Channels and engines
Own an animation of your own with a Channel (a number with a plan) and a FrameEngine (a clock subscriber that sleeps when nothing moves).
A Channel is one number with a plan: retarget(to, at, motion) is a command that heads for to from the state drawn at at, velocity included; launch(to, at, motion, from) starts from an explicit { value, velocity } (a release); valueAt(time) and velocityAt(time) allocate nothing. A FrameEngine is the clock subscriber that draws channels: it subscribes only while moving(time) is true or a callback is scheduled, runs step(time) and then draw(time) in the commit phase, and sleeps when nothing moves. Time is seconds on the engine's clock (now), and timers is a millisecond Timers on that time for a gesture recognizer that must decide on the motion's frames.
An engine that respects reduced motion
motion(m) applies the clock's reduced-motion preference to a motion: a spring becomes m.reduced (a jump by default), a tween or a jump is kept. An engine that builds every command through this.motion(...) follows the policy without reading a media query.
import { channel, FrameEngine } from '@freestylejs/ani-core'
class DotEngine extends FrameEngine {
private readonly x = channel(0)
constructor(private readonly element: HTMLElement) {
super()
}
/** A command: it keeps the drawn value and velocity; reduced motion turns the spring into a jump. */
moveTo(target: number): void {
this.x.retarget(
target,
this.now,
this.motion({ kind: 'spring', spring: { duration: 0.4, bounce: 0.15 } })
)
this.wake()
}
protected step(): void {}
protected moving(time: number): boolean {
return !this.x.settledAt(time)
}
protected draw(time: number): void {
this.element.style.transform = `translateX(${this.x.valueAt(time)}px)`
}
}
const engine = new DotEngine(document.querySelector<HTMLElement>('#dot')!)
engine.moveTo(200)Owning an engine in a component
Whoever creates an engine disposes it. useTimeline owns any object with a dispose(): it makes one for the component's lifetime, disposes it on unmount, and keeps the same object under StrictMode's effect replay (a replayed effect never disposes it).
import { channel, FrameEngine } from '@freestylejs/ani-core'
import { useTimeline } from '@freestylejs/ani-react'
import { useEffect } from 'react'
class FadeEngine extends FrameEngine {
private readonly opacity = channel(0)
private element: HTMLElement | null = null
attach = (element: HTMLElement | null): void => {
this.element = element
this.wake()
}
show(visible: boolean): void {
this.opacity.retarget(
visible ? 1 : 0,
this.now,
this.motion({ kind: 'tween', duration: 0.3 })
)
this.wake()
}
protected step(): void {}
protected moving(time: number): boolean {
return !this.opacity.settledAt(time)
}
protected draw(time: number): void {
if (this.element) this.element.style.opacity = String(this.opacity.valueAt(time))
}
}
export function Fade({ visible }: { visible: boolean }) {
const engine = useTimeline(() => new FadeEngine())
useEffect(() => {
engine.show(visible)
}, [engine, visible])
return <div ref={engine.attach} />
}clock (createClock / defaultClock)
The frame loop that drives timelines. Use the shared default clock, or create independent clocks for tests and isolated engines.
values (value codecs)
Animate colors, lengths and strings with numbers. Codecs turn display values into number vectors for the engine and back.