In November 2026 I rebuilt the homepage of my one-person web studio around a single pinned three.js scene. A procedural laptop, a monitor and a phone show real client sites, and as you scroll they rearrange for each of my five services: websites, e-shops, custom systems, SEO and ads. Everything else on the page – service cards, results, projects, reviews, FAQ – sits below that scene.
On launch day mobile PageSpeed Insights gave it 60. First Contentful Paint and Largest Contentful Paint were fine at 1.2 s and CLS was 0, but Total Blocking Time was 17 seconds. Today the same page scores 100 / 100 / 100 / 100 on mobile: FCP and LCP 1.1 s, TBT 0 ms, CLS 0. The scene, the textures and the animations are all still there.
This is what changed, in the order I found the problems. None of it is exotic, but most of it only shows up when you measure on a slow phone profile, not on a desktop with a good GPU.
The scene in one paragraph
The hero is a sticky element inside a tall section, so the visitor scrolls through several viewport heights while the canvas stays pinned. One WebGL renderer draws all five scenes; scroll progress is mapped to camera and device positions with simple easing functions. The devices are procedural geometry with PBR materials lit by three.js RoomEnvironment, and the screens are JPEG textures of real client sites, captured with headless Chrome. three.js is loaded through an import map. Without WebGL 2, or with prefers-reduced-motion: reduce, the page falls back to a static layout with the same text.
Why the first PageSpeed run said 17 seconds
Lighthouse in PageSpeed Insights runs without a GPU. WebGL still works there, but it is rendered in software (SwiftShader or similar) on the CPU. My render loop drew every frame, all the time, so on the test machine each frame was a long main-thread task. Lighthouse counts every task over 50 ms towards Total Blocking Time, and a scene that never idles produces nothing but long tasks.
A real phone with a GPU did not feel slow at all. That is the trap: the score was not lying about the CPU cost, it was measuring a situation I had not designed for – a device that renders WebGL in software.
Rule 1: no WebGL before the first paint, and not for software renderers
The fix was to stop treating the 3D scene as part of the page load. Now the text, buttons and a static hero render first, and the scene starts later:
- on the visitor’s first interaction (pointer, touch, wheel, key or scroll), in an idle moment rather than in the same frame as the interaction;
- or by itself shortly after
load, but only if the device has a real GPU; - on a software renderer (test bots, some virtual machines) only after an interaction, and in a low-power mode.
To know whether there is a real GPU without blocking the main thread or the first frame, the check runs in a Web Worker on an OffscreenCanvas, and only after first-contentful-paint:
const probe = () => new Promise(res => {
if (!window.OffscreenCanvas || !window.Worker) return res(false);
const src = "onmessage=()=>{let s='none';try{" +
"const g=new OffscreenCanvas(1,1).getContext('webgl');" +
"if(g){const x=g.getExtension('WEBGL_debug_renderer_info');" +
"s=String(g.getParameter(x?x.UNMASKED_RENDERER_WEBGL:g.RENDERER));" +
"const l=g.getExtension('WEBGL_lose_context');l&&l.loseContext()}" +
"}catch(e){}postMessage(s)}";
const w = new Worker(URL.createObjectURL(new Blob([src], { type: 'text/javascript' })));
const t = setTimeout(() => { w.terminate(); res(true); }, 3000);
w.onmessage = e => {
clearTimeout(t); w.terminate();
res(/swiftshader|llvmpipe|software|basic render/i.test(e.data)); // true = software
};
w.postMessage(0);
});
const painted = new Promise(res => {
new PerformanceObserver((list, obs) => {
if (list.getEntriesByName('first-contentful-paint').length) { obs.disconnect(); res(); }
}).observe({ type: 'paint', buffered: true });
setTimeout(res, 6000);
});
If the probe says “software”, the scene waits for an interaction and draws only every third frame while nothing is moving. If it says “hardware”, the scene starts by itself about a second after load. three.js itself is imported dynamically only at that point, so it is not part of the initial JavaScript either.
After this change mobile PageSpeed went from 60 to 99, with TBT at 0 ms.
Rule 2: never measure layout while scrolling
The next complaint came from a real visitor: the animation “shakes” when scrolling. The cause was classic layout thrashing. Several scroll handlers and animation frames called getBoundingClientRect(), offsetHeight or scrollHeight, and in between other code wrote styles. Every read after a write forces the browser to recalculate styles and layout, sometimes several times in one frame.
The fix is a tiny shared layout cache. Positions are measured only when the layout can actually change – on resize, when web fonts finish loading, and when the document height changes (lower sections lay themselves out as you approach them). During scrolling, code only reads the cached numbers and the current scrollY, and does arithmetic:
window.KSVL = (function () {
var fns = [], pend = false, y = scrollY, vh = innerHeight, vw = innerWidth;
function run() {
pend = false; y = scrollY; vh = innerHeight; vw = innerWidth;
for (var i = 0; i < fns.length; i++) { try { fns[i](); } catch (e) {} }
}
function ask() {
if (!pend) { pend = true; requestAnimationFrame(function () { setTimeout(run, 0); }); }
}
addEventListener('scroll', function () { y = scrollY; }, { passive: true });
addEventListener('resize', ask);
if (document.fonts) document.fonts.ready.then(ask);
if (window.ResizeObserver) new ResizeObserver(ask).observe(document.documentElement);
return {
measure: function (f) { fns.push(f); ask(); }, // f reads layout, rarely
y: function () { return y; }, // used every frame
vh: function () { return vh; },
vw: function () { return vw; }
};
})();
Every component registers its own measuring function with KSVL.measure(), stores what it needs (section tops, heights, the maximum scroll), and its scroll handler never touches layout again. The 3D module uses the same cache.
Rule 3: write styles only when the value changes – and keep custom properties off big containers
Two smaller things made a surprisingly large difference.
First, handlers wrote the same style on every scroll event even when the value had not changed. Comparing with the last written value and skipping the write is a one-line change in each place.
Second, the header had a scroll-progress line driven by a CSS custom property, --p, set on the whole header element. Changing a custom property on an element invalidates styles for everything that inherits from it, which in my header mega menu meant roughly 250 elements were restyled on every scroll event. The progress line is now its own element, and only its transform changes:
function onScroll() {
var y = KSVL.y();
var pr = (docMax > 0 ? Math.min(1, y / docMax) : 0).toFixed(4);
if (pr !== lastPr) { lastPr = pr; line.style.transform = 'scaleX(' + pr + ')'; }
}
Rule 4: stop what nobody can see
Below the 3D scene there are a dozen sections with their own small animations: a calendar that fills in, counters, drifting background shapes, animated SVG lines. Several of them were infinite CSS animations. They kept running while off-screen, and the SVG ones animated stroke-dashoffset, which forced layout every frame. In Lighthouse this showed up as a worse Speed Index, not as TBT.
Now an IntersectionObserver with a 300 px margin adds an is-near class to sections that are close to the viewport, and CSS pauses everything else:
.ksv2-lazy :is(#pg,#cx,#ag,#pr,#rw,#fq,#px,#bl,#fc):not(.is-near) *,
.ksv2-lazy :is(#pg,#cx,#ag,#pr,#rw,#fq,#px,#bl,#fc):not(.is-near) *::before,
.ksv2-lazy :is(#pg,#cx,#ag,#pr,#rw,#fq,#px,#bl,#fc):not(.is-near) *::after {
animation-play-state: paused !important;
}
#pg, #cx, #pr, #rw, #fq, #px, #bl, #fc {
content-visibility: auto;
contain-intrinsic-size: auto 900px;
}
content-visibility: auto lets the browser skip rendering work for sections that are far away. Before adding it to a section, check that nothing inside it is meant to overflow outside its box, because the containment clips it.
The footer needed special care. It “reveals” from under the page (position: sticky; bottom: 0), so it physically sits at the bottom of the viewport from the start, underneath the content. When the web font for its large outlined wordmark loaded, the swap moved things and Lighthouse on desktop reported CLS 0.32. The footer is now visibility: hidden with content-visibility: hidden until the final call-to-action section is near; CLS went back to 0.
Rule 5: degrade resolution, not frames
On a genuinely slow device the scene still has to adapt. Skipping frames looks like stutter, so the render loop never does that on real hardware. Instead it keeps a running average of the frame time while the scene is on screen and lowers the pixel ratio step by step – from up to 2× to 1.5×, then to 1× – only if the average stays above about 50 ms (under ~20 fps) for a while:
const soft = !!window.__ksvSoft, DPR0 = Math.min(devicePixelRatio || 1, 2);
let fAvg = 16, fN = 0, lite = soft ? 2 : 0, skip = 0, prev = 0;
if (lite) renderer.setPixelRatio(1);
const loop = t => {
requestAnimationFrame(loop);
if (document.hidden) { prev = 0; return; } // hidden tab: draw nothing
const d = prev ? t - prev : 16; prev = t;
if (stageOn && d > 0 && d < 400) { fAvg += (d - fAvg) * .05; fN++; }
if (!soft && fN > 150 && fAvg > 50 && lite < 2) {
lite++; fN = 0; fAvg = 24;
renderer.setPixelRatio(lite === 1 ? Math.min(DPR0, 1.5) : 1);
}
if (soft && !busy && (skip = (skip + 1) % 3)) return; // software WebGL at rest: every 3rd frame
tick();
};
requestAnimationFrame(loop);
The accessibility points that cost the last few
Performance was not the only category below 100. Two details are worth knowing if you build “glassy” dark-on-light designs:
- Grey text on white cards had to be at least
#5a6679; my original#7a8499did not pass contrast. - Inactive cards were dimmed with
opacity, and axe calculates contrast including opacity. Dimming them withfilter: opacity()keeps the same look and passes.
How I measured it
PageSpeed Insights tells you the score, not the cause, and its numbers move between runs. I used a few small Node scripts that drive headless Chrome directly through the DevTools Protocol (no Puppeteer, about 100 lines each):
- a scroll test that makes Chrome scroll the page down and up on a phone profile with 4× CPU throttling, and logs frame durations, layout shifts, jumps of the pinned scene and – optionally – a JavaScript profile with main-thread work;
- a “before first paint” lab that runs Chrome without a GPU on a phone profile and lists every layout calculation and what triggered it until the first frame;
- a functional check that scrolls and takes screenshots at given positions, because a hidden browser panel does not fire scroll events at all.
With 4× CPU throttling on a phone profile, the scroll test went from a median frame of 33–37 ms to 17 ms, frames over 100 ms dropped from 60 to 12, and time spent recalculating styles fell from 9.3 s to 4.5 s. Layout shift while scrolling is about 0.002.
Checklist
- Render the page without the scene first; start WebGL after first paint, on interaction or shortly after load.
- Detect software WebGL in a worker and give it an interaction-only, low-power mode.
- Import three.js dynamically when the scene starts, not in the initial bundle.
- Measure layout only on resize, font load and document height change; scroll handlers only do arithmetic.
- Write a style only if its value changed; keep frequently changing custom properties off large containers.
- Pause infinite animations off-screen and use
content-visibility: autofor far sections. - Hide sticky “reveal” footers until they are near, or they can shift when fonts load.
- When frames are slow, lower the pixel ratio – do not skip frames.
- Draw nothing while the tab is hidden (
document.hidden).
The live page is kokybiskasvetaine.lt – the site is in Lithuanian, but the scene speaks for itself. If you build something similar and get stuck on the numbers, I am happy to compare notes.
</>








