mirror of
https://github.com/charmbracelet/harmonica.git
synced 2026-07-19 10:09:33 -06:00
Harmonica
A simple, efficient spring animation library for smooth, natural motion.
import "github.com/charmbracelet/harmonica"
// A thing we want to animate.
sprite := struct{
x, xVelocity float64
y, yVelocity float64
}{}
// Where we want to animate it.
const targetX = 50.0
const targetY = 100.0
// Initialize a spring with framerate, angular frequency, and damping values.
spring := harmonica.NewSpring(harmonica.FPS60, 0.8, 0.98)
// Animate!
for {
spring.Update(&sprite.x, &sprite.xVelocity, targetX)
spring.Update(&sprite.y, &sprite.yVelocity, targetY)
time.Sleep(time.Second/60)
}
For details, see the examples and the docs.
Acknowledgements
This library is a fairly straightforward port of Ryan Juckett’s excellent damped simple harmonic oscillator originally writen in C++ in 2008 and published in 2012. Ryan’s writeup on the subject is fantastic.
License
Part of Charm.
Charm热爱开源 • Charm loves open source
Languages
Go
100%
