Heavy views
A view with thousands of nodes, or with frosted glass, is costly to scale and blur on every frame. rasterize keeps a photo of the view you are in, with SnapDOM, and moves the photo while the view sits behind; webgl also draws the zoom itself. Zoom into a bright tile with each one.
Turn it on
Install SnapDOM and its plugins next to Zumly, and hand over snapdom and stream:
npm install zumly @zumer/snapdom @zumer/snapdom-plugins
import Zumly from 'zumly'
import { snapdom } from '@zumer/snapdom'
import { stream } from '@zumer/snapdom-plugins/stream'
new Zumly({ mount: '.canvas', initialView: 'home', views, rasterize: { snapdom, stream } })
Both are optional peer dependencies: npm install zumly alone does not bring them. Without a bundler, import them from a CDN, as the example does. If SnapDOM is missing, Zumly says so once in the console and the views stay as they are.
What happens
- While you are in a view, its photo follows it: the stream repaints only what changed, at most ten times a second.
- You zoom into something: the view you leave shows the photo, as it was at that moment. Its content is still there and laid out, only hidden, so the blur and dimming, clicks on siblings and every driver work as before.
- You come back: the real view returns. Over an opaque background it crossfades from the photo; a translucent view swaps at once, because a crossfade would show both.
- A photo is never taken during a transition. If there is none yet, the zoom moves the real view. Nothing waits for a photo.
When it pays off
Three zooms in and three out, with a clock, spinners and numbers changing in every view. Chrome on a MacBook; a dropped frame is one the screen never got.
| Views | Without | animateEffects: false | With rasterize |
|---|---|---|---|
| Cards, about 3,000 nodes | 6.8% dropped | 5.2% | 1.3%, no hitch |
| Frosted glass | 52% dropped, 48% at rest | 48%, 48% at rest | 0%, 0% at rest |
| Orbit dials (light) | 0.6% | 0% | left alone |
With the processor four times slower, frosted glass drops 47% without and 0.6% with rasterize.
A light view gains nothing, so rasterize leaves it alone. It photographs a view with at least minNodes elements, or with backdrop-filter inside. Mark a view data-rasterize="true" to photograph it anyway, or data-rasterize="false" to keep it live behind (a video, a game). Views with an iframe stay live: SnapDOM can only draw a placeholder for it.
Options
| Option | Default | Description |
|---|---|---|
snapdom | the global snapdom | The snapdom function from @zumer/snapdom. |
stream | none | The stream function from @zumer/snapdom-plugins/stream. It keeps the photo current; without it, only views that hold still are rasterized. |
minNodes | 800 | Views with fewer elements are photographed only if they use backdrop-filter. |
scale | 1 | Resolution of the photo. The blur behind hides its softness; without effects, 2 keeps text behind sharp, for four times the memory. |
Good to know
- A zoom never starts from old content. When keeping a photo current would cost more than half the main thread, that view simply moves as itself. What makes it costly: a value that changes often inside a box with no solid background, or over other content (a badge on top of cards). Give such a value a solid background and it costs a few milliseconds.
- Animations are not followed: a spinner shows the angle it had when its area was last repainted, and a CSS transition shows its end only after the next repaint there. Behind a blur, nobody notices.
- Without
streamthere is one photo, taken again once the view stops changing. A view that changed since its photo moves as itself, so views with live data are not rasterized. - A view behind stands still while it is there. When you come back, the real view takes its place with whatever changed meanwhile.
- Only views with triggers are photographed. A view without them never moves behind, so following it would cost frames for a photo nobody sees. One that gets a trigger later is followed from then on.
- A zoom-in starts a frame or two later: hiding the content of the view that grows takes a moment, about 25 ms on a laptop and 65 ms on a phone-class processor. The motion that follows is the smooth part.
- Each view behind keeps its photo in a canvas: width × height × 4 bytes, times the pixel ratio squared.
Draw the zoom with WebGL
webgl goes one step further. While views move, Zumly draws them in a WebGL canvas as textures, and the views behind stay textures at rest, with their blur and dimming done in a shader. Only the view you are in is DOM. It brings rasterize with it, so it takes the same snapdom and stream, and the same minNodes and scale.
new Zumly({ mount: '.canvas', initialView: 'home', views, webgl: { snapdom, stream } })
- Before a zoom-in moves, the view coming in is photographed: 30 to 45 ms on a laptop, while the trigger looks pressed. Past 300 ms a ring shows. With
hover: truethat happens while the pointer rests on the trigger, and the zoom starts in under 20 ms. - The views behind come from
rasterize's stream, so the click does not wait for them. - Siblings, a zoom-out that follows the finger and any number of levels go through WebGL too. Past the third level, everything behind stays as one picture, so nothing goes blank around the views you see.
- Clicks, focus, text and accessibility stay the browser's: WebGL only draws what is behind or moving.
- Without WebGL2, or when the browser takes the context away, the views go back to the DOM and zoom as before.
The same board as above, Chrome on a MacBook, and the same board with the graphics drawn in software, as a GPU with nothing to spare:
| Measure | With rasterize | With webgl |
|---|---|---|
| GPU while it zooms | 40 to 70% | 7 to 8% |
| The same, processor four times slower | 42 to 60% | 1 to 2% |
| Dropped frames on a weak GPU, in / out | 79% / 82% | 13% / 2% |
| From the click to the motion | 4 to 14 ms | 30 to 45 ms; under 20 with hover |
| Option | Default | Description |
|---|---|---|
effects | {} | More effects for transitions.effects, by name, in GLSL. See below. |
glass | false | Whether the photo of a view coming in keeps its backdrop-filter. Much slower to take; the real view, glass and all, takes over when the zoom ends. |
hover | false | Build and photograph the view a trigger opens while the pointer or the focus rests on it. View functions run then, and their work is thrown away if no click follows. A view that fetches its data does it on hover too. Views with mounted(), custom elements and deferred views wait for the click. |
ancestors | true | Past the third level, keep everything behind as one picture. false lets the oldest view go, as without WebGL. |
See it on a phone: the Phone showcase opens every app with a zoom from its icon, and lets you change how often the screen is captured.
Effects in the shader
transitions.effects works as before, in CSS filter syntax, and every CSS filter function is there. Your own are a few lines of GLSL: uv moves the point of the view that is read, color changes the color read there.
new Zumly({
...,
transitions: { effects: ['pixelate(6px) brightness(.8)', 'pixelate(12px) brightness(.6)'] },
webgl: {
effects: {
// Squares of v CSS px.
pixelate: { none: 0, uv: 'vec2 cell = v / photo; st = (floor(st / cell) + 0.5) * cell;' }
}
}
})
noneis the value that leaves a view as it is. An effect takes up to four numbers,vandvs(avec4); a#hexcolor counts as three.- In
uv,stis the point of the view, from 0 to 1. Incolor,rgbandaare the color. - Both can read
photo(the view's size in CSS px),focusandfocusSize(the trigger, from 0 to 1),progress(how far the zoom has gone) andtime. animate: truekeeps a view behind redrawn at rest, for an effect that moves withtime.
Good to know
- Measured in Chrome. Safari and Firefox have WebGL2, but they are not measured yet.
- A video or an iframe behind is a still picture, as with
rasterize. - Each view behind is a texture, and each level past the third keeps one more picture, about 4 MB at a pixel ratio of 2.
- With live data on a slow processor, keeping the photo of the view you are in current can cost too much. Then
rasterizestops following it, and the next zoom-in waits for a photo of it.
Which one to use
| Your views | Use |
|---|---|
| Light: few nodes, no glass | Nothing. They are smooth already, and WebGL would only add the wait for a photo. |
| Heavy, on laptops and desktops | rasterize. No dropped frames, live data included, and the zoom starts at once. |
| Heavy, on tablets or small boards | webgl. It is the one that holds when the GPU has little to spare. |
| Effects CSS does not have | webgl. |
| A video or an iframe that must stay live behind | rasterize with data-rasterize="false" on that view, or nothing. webgl draws every view behind as a picture. |
Try the cheap fix first
transitions: { animateEffects: false } applies the blur at once instead of animating it. It needs nothing else, and with many nodes it already takes out part of the stutter. Chrome cannot run a blur animation on the compositor, so it redraws it from the main thread on every frame. Reach for rasterize when that is not enough: frosted glass, thousands of nodes, or a slow device.
Two more that cost nothing:
- Use the
cssorwaapidriver. They move views on the compositor; GSAP, Anime.js and Motion move them from the main thread, so any work there shows in the motion. - A heavy view that comes in can grow empty:
data-deferredon its trigger (ordeferred: true) adds its content when the zoom ends.
And one for frosted glass: drop it while views move. Each pane of glass blurs what is under it again on every frame, and under it everything is moving. A mark on the canvas during the motion is enough:
const canvas = document.querySelector('.canvas')
const moving = () => { canvas.dataset.moving = '' }
const still = () => { delete canvas.dataset.moving }
app.on('zoomInStart', moving).on('beforeZoomOut', moving).on('lateralStart', moving)
app.on('afterZoomIn', still).on('afterZoomOut', still).on('afterLateral', still)
.canvas[data-moving] .card { backdrop-filter: none; }
On the example's board the GPU of a zoom goes from about 60% to 17%. The glass is back as soon as the view stops.