vibran

Website animation that does not slow phones: light and accessible

· 6 min read

What to do so your site's animation does not slow down a phone or bother people who asked for less motion. With examples taken from the code of the Vibran effects and from tests I ran on them.

Two different complaints

Animation causes two kinds of problems, and each has its own fix.

The first is performance: the page gets slow, the phone gets warm, the battery drains. The people who feel it are the ones with simple devices, who are also the ones who browse mostly on a phone.

The second is accessibility: for some people, movement on screen causes dizziness, nausea or headaches. Operating systems have a setting for that, and your site has to respect it.

The setting your site has to read

On iPhone it is called Reduce Motion, on Android it is Remove animations, and Windows and macOS have it too, under similar names. The browser passes the choice on to the site through a CSS and JavaScript query called prefers-reduced-motion.

In CSS, you switch the animation off inside a rule:

.background::before { animation: drift 14s ease-in-out infinite alternate; }

@media (prefers-reduced-motion: reduce) {
  .background::before { animation: none; }
}

In JavaScript, you ask before you start:

const calm = matchMedia("(prefers-reduced-motion: reduce)").matches;
if (calm) return; // do not start the effect

Reduced motion does not ask for a dead screen. It asks that whatever moves on its own stops or appears without animating. All the content stays there.

What the Vibran effects do

I checked the code of the 110 files in the effects folder. 109 of them handle prefers-reduced-motion: 59 with an @media rule in the CSS the effect injects and 57 with matchMedia in JavaScript, and some use both. The exception is the glow card (cartao-brilho), which does not do the check. Its light only moves when the mouse moves, but it is a point to fix.

In practice, each type of effect behaves differently:

EffectWith reduced motion
Aurora backgroundThe CSS animation is switched off and the patches stand still.
Text scrambleThe text shows up directly, without going through the symbols.
Light strands (fios-luz)Draws one frame and stops. No animation loop.
Marquee (faixa infinita)The strip stops, becomes a list you can scroll by touch, and the hidden copy goes away.
Magnetic buttonDoes not start. It also does not start for people without a mouse.

I did not stop at reading the code. I ran three of them in Chrome, once with the normal preference and once with it turned on through the developer tools emulation:

Bundles of glowing strands, silver on top and blue below, meeting in the center over a dark background with clouds, with the text Fios de luz
Fios de luz (light strands), which draws on a canvas. With reduced motion it stays on this frame.
A JavaScript detail

In the effects that check through JavaScript, the question is asked when the file loads. If someone turns the setting on while the page is open, those effects only obey after a reload. The ones that use a CSS rule obey right away.

Performance on a phone

Small files

The effects are plain JavaScript, with no library. The 110 minified files add up to 370 KB together, and you only load the ones you use. Aurora is 1.2 KB, Fios de luz is 5.7 KB, and the biggest of all, the zoom modal, is 6.6 KB.

Animate only what is on screen

59 effects use IntersectionObserver. Of the 49 that use requestAnimationFrame, 40 also watch whether the element is visible. In Fios de luz, the code keeps track of whether the element is on screen, stops asking for frames when it leaves and starts again when it comes back. A background you already scrolled away from does not spend battery.

A ceiling on resolution

Phones often have a pixel density of 3. Drawing on a canvas at that density means 9 pixels for each page pixel. The 29 effects that read devicePixelRatio cap the value with Math.min, at 1.25, 1.5 or 2. With a cap of 1.5, that is 2.25 pixels per page pixel, four times fewer than 9. In 15 of them the default cap is 1.5. Another 15 effects have a data-qualidade option (quality) that lowers the drawing resolution (in the liquid sphere, from 0.3 to 1).

Animate only the cheap properties

The Aurora animation only touches transform, and the blur is fixed. The marquee slides with translateX. These are properties the browser handles on the graphics card, without redoing the layout on every frame.

Less stuff on a phone

Some effects have options to lighten the load. Fios de luz accepts data-fios (strands, 70 per bundle by default, 140 at most) and data-nuvens="nao" (turns the clouds off). One way to use fewer strands on a small screen:

<section data-efeito="fios-luz">...</section>

<script>
  document.querySelector('[data-efeito="fios-luz"]').dataset.fios =
    matchMedia("(max-width: 700px)").matches ? "24" : "70";
</script>
<script src="efeitos/fios-luz.js"></script>

The small script comes before the effect file so the number is already set when the effect starts. Mouse effects, such as the custom cursor and the magnetic button, only start for people with a mouse pointer. On a phone they do nothing.

How to test on your site

  1. Reduced motion: in Chrome, developer tools, the “More tools” menu, “Rendering”, and choose prefers-reduced-motion: reduce. Reload the page and see whether everything that moved has stopped.
  2. A pretend slow phone: turn on device mode (the two-devices icon) and, in the Performance tab, throttle the CPU to 4 times slower. If the page stutters like that, on a simple device it will stutter too.
  3. Frames per second: the Rendering tab has a frame meter. Scroll the page and watch whether it drops when the effect appears.
  4. A real phone, preferably the oldest one you have around.
  5. Lighthouse, in the same tools, for Performance and Accessibility.
  6. Contrast with a moving background: wait for the whole cycle and see whether the text stays readable.

What is still up to you

Every animation must respect prefers-reduced-motion: with the setting
on, stay still or appear without animating. Animate only transform and
opacity, pause whatever leaves the screen and cap the canvas resolution.

In short

Ask for, or write, the reduced-motion rule from the start. Prefer small effects that pause off screen and cap their resolution. Test with the emulation and with a real device. To see how an effect gets into a site, read how to add an animated effect with a prompt. To see a whole page that takes these precautions, open the barbershop template with WhatsApp booking.

See the background effects running and adjust the time and the amount before copying.

See the animated backgrounds

Keep reading