Animations
Animations bring your backgrounds to life with CSS keyframes. This page documents every animation, its options and how to combine them.
Note: animations need the xeerpe stylesheet. If you have not added it yet, see Setup.
Overview
xeerpe ships nine animations: pulse, rotate, breathe, aurora, shimmer, liquid, plasma, float and drift. Each one is a Builder method, so you add it to the chain like any other layer.
import { Builder } from 'xeerpe'
const style = new Builder()
.radialGradient({
from: '#34d399',
to: '#052e1c',
})
.pulse()
.toStyle()An animation does not add a background layer. It adds one animation property to the style returned by toStyle(), which points to a keyframe animation defined in the xeerpe stylesheet.
The same animation can also be added with the generic animation() method, which takes the animation name and its options. In this form the options are required, so pass an empty object to use the defaults.
new Builder()
.animation('pulse', {})
.toStyle()Animations act on one of two targets:
- The background:
breathe,aurora,shimmeranddriftscale or move the background.liquiddoes both, and also shifts its hue. - The whole element:
pulse,rotate,floatandplasmaaffect the element and everything inside it, text included.
Setup
The keyframes live in a stylesheet that you load once, in the entry file of your app.
import 'xeerpe/animations.css'Note: without the stylesheet the style is applied but nothing moves, and the browser reports no error. For CSS, SCSS and Angular setups, see Add the stylesheet.
All keyframes are prefixed with xeerpe-, so they never collide with your own animations.
Shared options
Every animation accepts the same four options, plus target on the five that move the background. Each animation has its own defaults, listed in its section below.
durationstringHow long one cycle takes, as a CSS time such as '4s' or '800ms'.
easingstringThe timing function, for example 'linear', 'ease-in-out', 'cubic-bezier(0.4, 0, 0.2, 1)' or 'steps(4)'.
direction'normal''reverse''alternate''alternate-reverse'Whether the animation plays forward, backward or alternates between the two. Only pulse and aurora use it. The other animations ignore it.
iterationCountnumber'infinite'How many times the animation runs. Use a number to play it a fixed number of times, or 'infinite' to loop forever.
target'gradients''patterns''all'Which layers of the background the animation moves. 'gradients' is the default: the gradients move and patterns and effects stay in place. 'patterns' moves only the patterns, over a still gradient. 'all' moves every layer. Only breathe, aurora, liquid, shimmer and drift use it, and 'patterns' only has an effect on the ones that move the background (shimmer, drift and liquid). Any other value throws an error.
Note:
pulse,rotate,floatandplasmahave notarget. They change a property of the whole element (opacity,transformorfilter), which CSS cannot apply to one layer only, so they always affect everything.
Note: apart from
target, the values are inserted into the CSS as they are, without validation. If a value is not valid CSS, for example a duration without a unit, the browser ignores the wholeanimationdeclaration and nothing moves.
| Animation | What it changes | target |
|---|---|---|
breathe, aurora | Background size, per layer | 'gradients' or 'all' |
shimmer, drift, liquid | Background position, per layer | 'gradients', 'patterns' or 'all' |
pulse | Opacity of the element | Not available |
rotate, float | Transform of the element | Not available |
plasma | Filter of the element | Not available |
Example
Every option is optional, and any you leave out keeps the default of the animation.
new Builder()
.radialGradient({
from: '#34d399',
to: '#052e1c',
})
.pulse({
duration: '2s',
easing: 'ease-in',
direction: 'reverse',
iterationCount: 3,
})
.toStyle()The code above produces this animation value:
animation: xeerpe-pulse 2s ease-in reverse 3Pulse
Fades the element between full opacity and 70% opacity, which makes it breathe softly. It changes the opacity of the whole element, content included, so the page behind shows through.
Defaults
{
duration: '4s',
easing: 'ease-in-out',
direction: 'alternate',
iterationCount: 'infinite',
}Examples
Pulsing glow
A radial glow that fades in and out.
new Builder()
.radialGradient({
from: '#34d399',
to: '#052e1c',
colorToPosition: '75%',
})
.pulse({ duration: '3s' })
.toStyle()Rotate
Spins the element a full turn around its center. It rotates the whole element and its content, so it works best on a square or round element.
Defaults
{
duration: '12s',
easing: 'linear',
iterationCount: 'infinite',
}Examples
Color wheel
A conic gradient on a round element becomes a spinning color wheel.
new Builder()
.conicGradient({
colors: [
'#f472b6',
'#a78bfa',
'#38bdf8',
'#34d399',
'#f472b6',
],
})
.rotate({ duration: '8s' })
.toStyle()Breathe
Zooms the background in and out, from 100% to 200% of its size and back. At its largest the background is magnified, so the details of the gradient spread out.
Defaults
{
duration: '12s',
easing: 'ease-in-out',
iterationCount: 'infinite',
}Examples
Breathing mesh
The blobs of a mesh gradient swell and shrink.
new Builder()
.meshGradient({
background: '#070b1f',
layers: [
{
from: 'rgba(124,58,237,0.85)',
to: 'transparent',
position: '12% 18%',
colorToPosition: '60%',
},
{
from: 'rgba(34,211,238,0.65)',
to: 'transparent',
position: '88% 20%',
colorToPosition: '55%',
},
{
from: 'rgba(244,114,182,0.65)',
to: 'transparent',
position: '70% 100%',
colorToPosition: '60%',
},
],
})
.breathe({ duration: '8s' })
.toStyle()Aurora
A gentler version of breathe: the background grows from 100% to 120% of its size and back. It adds slow movement without changing the look of the gradient.
Defaults
{
duration: '12s',
easing: 'ease-in-out',
direction: 'alternate',
iterationCount: 'infinite',
}Examples
Northern lights
A subtle drift on a mesh gradient, enough to feel alive without drawing attention.
new Builder()
.meshGradient({
background: '#04121c',
layers: [
{
from: 'rgba(16,185,129,0.8)',
to: 'transparent',
position: '15% 25%',
colorToPosition: '60%',
},
{
from: 'rgba(56,189,248,0.65)',
to: 'transparent',
position: '85% 30%',
colorToPosition: '55%',
},
{
from: 'rgba(168,85,247,0.6)',
to: 'transparent',
position: '55% 100%',
colorToPosition: '60%',
},
],
})
.aurora({ duration: '8s' })
.toStyle()Shimmer
Slides the background horizontally, from right to left, which produces a moving highlight. It is the classic loading skeleton effect.
For a clean loop, the gradient needs three things:
- A wider background: a gradient needs to be larger than the element, otherwise there is nothing to slide. When you do not set a
backgroundSizeon the gradient,shimmerwidens it to'200% 100%'for you. Set your own value, such as'400% 100%', to control how far the band travels. - The same color at both ends, because the background repeats and a different color would leave a visible seam.
- A horizontal direction (
90deg), so the band travels in the same direction as the animation.
Defaults
{
duration: '3s',
easing: 'linear',
iterationCount: 'infinite',
}Examples
Loading skeleton
A lighter band that travels across a dark surface.
new Builder()
.linearGradient({
colors: ['#1e293b', '#475569', '#1e293b'],
angle: '90deg',
backgroundSize: '200% 100%',
})
.shimmer({ duration: '2.5s' })
.toStyle()Liquid
Combines three movements: the background slides from one side to the other, zooms from 100% to 200%, and its hue shifts by 30 degrees. The result is a gradient that flows and changes color slowly.
Note:
liquidanimates thefilterof the element, so it replaces any blur applied withblur()andtype: 'blur'while it runs.
Defaults
{
duration: '8s',
easing: 'ease-in-out',
iterationCount: 'infinite',
}Examples
Flowing gradient
A four-color gradient that drifts and changes hue.
new Builder()
.linearGradient({
colors: [
'#06b6d4',
'#6366f1',
'#d946ef',
'#f97316',
],
angle: '135deg',
})
.liquid()
.toStyle()Plasma
Cycles the hue of the whole element through a full turn, so every color in it changes continuously. It affects the content of the element too.
Note:
plasmaanimates thefilterof the element, so it replaces any blur applied withblur()andtype: 'blur'while it runs.
Defaults
{
duration: '6s',
easing: 'linear',
iterationCount: 'infinite',
}Examples
Hue cycle
A mesh gradient whose colors travel around the color wheel.
new Builder()
.meshGradient({
background: '#070b1f',
layers: [
{
from: 'rgba(124,58,237,0.85)',
to: 'transparent',
position: '12% 18%',
colorToPosition: '60%',
},
{
from: 'rgba(34,211,238,0.65)',
to: 'transparent',
position: '88% 20%',
colorToPosition: '55%',
},
{
from: 'rgba(244,114,182,0.65)',
to: 'transparent',
position: '70% 100%',
colorToPosition: '60%',
},
],
})
.plasma({ duration: '8s' })
.toStyle()Float
Moves the whole element up by 10 pixels and back down, like it is floating. It pairs well with glow(), because the light moves with the element.
Defaults
{
duration: '6s',
easing: 'ease-in-out',
iterationCount: 'infinite',
}Examples
Floating card
A panel with a soft glow that rises and sinks.
new Builder()
.linearGradient({
from: '#1e293b',
to: '#0f172a',
})
.glow({
color: 'rgba(56,189,248,0.5)',
amount: '40px',
y: '20px',
})
.float()
.toStyle()Drift
Moves the background from the top left to the bottom right and back. Like shimmer, it needs a background larger than the element. When the gradient has no backgroundSize, drift widens it to '300% 300%' for you, so the movement reveals different parts of the gradient. Set your own backgroundSize to change it.
Defaults
{
duration: '10s',
easing: 'ease-in-out',
iterationCount: 'infinite',
}Examples
Wandering gradient
A multi-color gradient three times larger than its element, slowly sliding across it.
new Builder()
.linearGradient({
colors: [
'#0ea5e9',
'#6366f1',
'#ec4899',
'#f59e0b',
],
angle: '135deg',
backgroundSize: '300% 300%',
})
.drift()
.toStyle()Combining animations
You can add more than one animation to the same chain. They run at the same time, each with its own options.
new Builder()
.linearGradient({
colors: [
'#0ea5e9',
'#6366f1',
'#ec4899',
'#f59e0b',
],
angle: '135deg',
backgroundSize: '300% 300%',
})
.drift({ duration: '12s' })
.plasma({ duration: '16s' })
.toStyle()Good to know
- Same property, last one wins. Two animations that change the same property do not add up.
floatandrotateboth animatetransform, so with.float().rotate()the element only rotates, and with.rotate().float()it only floats. - Only gradients move by default.
breathe,aurora,liquid,shimmeranddriftchange the size or the position of the background. Thetargetoption chooses which layers follow:'gradients'(the default) moves the gradients and keeps patterns and effects in place,'patterns'slides only the patterns over a still gradient (for the position animationsshimmer,driftandliquid), and'all'moves every layer. For example,.shimmer({ target: 'all' })makes a grid slide together with the gradient.toStyle()writes a few--xeerpe-*custom properties for this, so use the style object it returns together with the xeerpe stylesheet, and update both when you upgrade. - Filters are replaced.
liquidandplasmaanimatefilter, which replaces any blur applied withblur()andtype: 'blur'while they run.
Reduced motion
The animations do not check the user's motion preferences on their own. To respect prefers-reduced-motion, add this rule to your stylesheet. It stops every xeerpe animation for the users who asked for less motion.
@media (prefers-reduced-motion: reduce) {
[style*='xeerpe-'] {
animation: none !important;
}
}Types reference
type AnimationType =
| 'pulse'
| 'rotate'
| 'breathe'
| 'aurora'
| 'shimmer'
| 'liquid'
| 'plasma'
| 'float'
| 'drift'
interface AnimationOptions {
duration?: string
easing?: string
direction?:
| 'normal'
| 'reverse'
| 'alternate'
| 'alternate-reverse'
iterationCount?: number | 'infinite'
target?: 'gradients' | 'patterns' | 'all'
}