Class Animated
A value that moves over time, made with the Anim functions and assigned to a window or drawable property that takes one.
A window springs into its tile, and fades out when it closes:
local spring = { stiffness = 220, damping_ratio = 0.78 }
local function move_into(target, tile)
target.box.position = Anim.spring(Point(tile.x, tile.y), spring)
target.box.size = Anim.spring({ width = tile.width, height = tile.height }, spring)
target:request_size({ width = tile.width, height = tile.height })
end
wm.windows:on("new_window", function(opened)
opened:on("closed", function(closed)
closed.box.opacity = Anim.tween(0, 160)
end)
end)How values move
A property takes a plain value or an animated one. A plain value never moves.
- Values are numbers, Points, Sizes, Rects and colours (
"#rrggbb"or"#rrggbbaa"). - A table with
xandyis a Point, one withwidthandheighta Size, and one with all four a Rect. - Colours blend in OKLab.
An animation starts when it is first assigned to a property, from the value on screen at that moment.
A spring also keeps the speed the property was moving at.
Assigning the same animated value to a second property doesn't restart it. Both follow the one motion.
now()andsettled()give the same answer all through one frame.
Errors for arithmetic on an animated value: a + 1 raises. Use Anim.mix to combine them.
Functions
Anim.spring(to: value, options: { stiffness: number?, damping_ratio: number? }?) -> Animatedspring
Moves toward to like a weight on a spring.
stiffnesssets how fast. Default 400.damping_ratiosets how it arrives. Default 1: no overshoot from rest. Below 1 bounces, above 1 creeps in.- It counts as settled once it is within a thousandth of the distance it travelled and nearly still, or after 60 seconds.
Errors for an option that is 0 or less, any other option, and an animated to.
window.box.position = Anim.spring(Point(100, 80), { stiffness = 300, damping_ratio = 0.7 })Anim.tween(to: value, ms: number, easing: "linear" | "ease_in" | "ease_out" | "ease_in_out"?) -> Animatedtween
Moves to to in exactly ms milliseconds along an easing curve. The eased curves are cubic.
easingdefaults to"ease_out".msof 0 jumps straight toto.- A tween starts from where the property is but not from its speed. It suits fades and colours better than motion that may be interrupted.
Errors for a negative ms and an animated to.
window.box.opacity = Anim.tween(0.85, 220, "ease_in_out")Anim.throttle(value: value | Animated, ms: number) -> Animatedthrottle
value, updated at most every ms milliseconds while it moves, then once more on the exact target when it settles. Use it where every change costs something, such as the size sent to an app with window:request_size.
- A throttled plain value is just that value.
Errors for a negative ms.
local size = Anim.spring({ width = 900, height = 600 })
window:request_size(Anim.throttle(size, 50))Anim.delay(value: value | Animated, ms: number) -> Animateddelay
value, held still for ms milliseconds after it is assigned. Then it starts from where the property is, with no speed.
- Only an animation waits. A plain value, or a tween of 0 ms, is at its target at once.
Errors for a negative ms.
other.box.opacity = Anim.delay(Anim.tween(1, 200), 120)Anim.mix(a: value | Animated, b: value | Animated, t: number | Animated) -> Animatedmix
a blended toward b by t: 0 is a, 1 is b.
tisn't clamped, so 2 goes as far pastbasais behind it.aandbstart from the property when the mix is assigned, like any animation.- An animated
tstarts from 0.
Errors when a and b are different kinds of value.
local progress = Anim.spring(1)
window.box.position = Anim.mix(Point(0, 0), Point(400, 0), progress)Methods
animated:now() -> valuenow
The value at the current frame. A colour comes back as "#rrggbbaa".
- Before the animation is assigned to a property, its target.
local drawn_at = window.box.position:now()animated:target() -> valuetarget
Where it ends. Use it to lay out against where a window is going rather than where it is now.
- For a mix, the mix of the targets of
aandbby the target oft.
animated:settled() -> booleansettled
Whether it has stopped moving.
- Always true for a plain value, and for one not yet assigned.