Skip to content

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:

lua
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 x and y is a Point, one with width and height a 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() and settled() 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.

  • stiffness sets how fast. Default 400.
  • damping_ratio sets 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.

lua
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.

  • easing defaults to "ease_out".
  • ms of 0 jumps straight to to.
  • 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.

lua
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.

lua
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.

lua
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.

  • t isn't clamped, so 2 goes as far past b as a is behind it.
  • a and b start from the property when the mix is assigned, like any animation.
  • An animated t starts from 0.

Errors when a and b are different kinds of value.

lua
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.
lua
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 a and b by the target of t.

animated:settled() -> booleansettled ​

Whether it has stopped moving.

  • Always true for a plain value, and for one not yet assigned.