A WebGL effect without three.js. 2.2 KB instead of 134 KB
is what the smallest three.js version weighs, while my code weighs 2.2 KB
In short
The video at the top of my website ripples around the cursor. On a computer this is done by hand-written WebGL, without the three.js library. Compressed, that code weighs 2.2 KB. The smallest three.js version I could build weighs 134 KB.
Three.js is a very good library for 3D graphics in the browser. For a while the first screen of my site ran on it and showed a 3D scene. But I went back to the rippling video and removed three.js from the project, because nothing else was using it. This effect has no 3D in it at all. It is one video and one simple program for the graphics card.
What the effect does
A ripple spreads out from the cursor and slightly bends the picture. Close to the cursor the colours separate a little, and over everything I lay a subtle grain, like on film stock. A plain video and CSS cannot do this, because each pixel has to be calculated separately, depending on where the cursor is. A program like that is a shader, and the graphics card runs it.
How it works without a library
It takes three things:
- One triangle. It is bigger than the screen, so it covers all of it. The graphics card runs the shader for every pixel underneath.
- The video frame as a texture. The video plays in an ordinary video element, and its current frame goes to the graphics card.
- The shader. For each pixel it calculates the distance from the cursor, shifts the point where the picture is read and adds grain.
Three.js would give me a camera, a scene, materials and lighting. I use none of those here, and I would still have to write the shader code myself.
What it weighs
For comparison I built the bare minimum this effect needs in three.js 0.187: a renderer, a scene, a camera, one triangle, a video texture and an empty shader. I minified both files with the same tool (esbuild) and compressed them with gzip.
| Minified | Compressed | |
|---|---|---|
| My code with the shader | 4.6 KB | 2.2 KB |
| Three.js minimum, without the effect code | 535 KB | 134 KB |
To be fair, the library could be downloaded only on computers, so a phone would not pay that whole difference. A phone does download my code, even though it does not run it. Even so, a computer would have to download about 60 times more code for the same effect.
A small file is not everything
This effect can load the computer the whole time the page is open. The most expensive part is sending the video frame to the graphics card, so I limit that in three ways:
- New frames only. The video has 24 frames per second, while the screen refreshes at least 60 times. I send a frame only when the browser reports a new one. If it cannot do that, I send at most 30 per second.
- Only when it is visible. After you scroll away or switch to another tab, the effect stops sending and drawing frames.
- No extra pixels. The video is 1600 pixels wide and sits under a dark overlay, so even on high-density screens I draw at normal resolution.
When the effect is not there at all
The effect is an extra, and the site has to work without it. An ordinary video keeps playing underneath. The shader does not start when:
- the screen is up to 900 pixels wide, which means phones and many tablets,
- the computer reports 4 processor cores or fewer,
- someone has limited animations in their system or turned on the site’s accessibility mode,
- the browser does not support WebGL or the shader fails to compile.
The effect layer appears only after the first video frame has been drawn. Before that, it covered the first screen in black until the video had loaded.
When I would choose three.js
Hand-written code has its price too. I am responsible for every detail, even for keeping the picture from showing upside down, because WebGL counts texture coordinates from the bottom, while a video frame starts at the top. For a real 3D scene, with models, light and a camera, I would choose three.js without hesitation, because writing that by hand would make no sense.
Before I add a library, I check how much of it I will actually use. If you want an effect on your site that will not slow it down for customers, get in touch and we will see whether it can be done more lightly.