Components Layout
Pixel Scroll Transition
Join full-height sections with a field of square pixels that changes color as the reader scrolls.
Scroll into the light.
Keep scrolling.
Scroll back to rewind.
Usage
Pass your sections in reading order. Give each section a Title and optionally a Description. Choose a Pattern, then scroll across a section boundary. Scrolling back retraces the same transition.
Register pixel-scroll-transition with Head and pass its properties to Render. Go setup guide
Pattern accepts random (default), wave, diagonal, spiral, or checker. Select any pattern in the demo to try it on both transitions. PixelSize sets the approximate width of a square from 4 to 120 CSS pixels (default 24). Set HideControls to true to use a single pattern.
Give each section a Theme of dark or light, or omit it to alternate the two backgrounds. The headings and descriptions render in Go. Readers who request reduced motion or disable JavaScript can read the sections without the animated bands.
Connect two sections
err := mono.UI.Render(w, "pixel-scroll-transition", mono.Props{
"Title": "Exhibition entrance",
"Pattern": "wave", "PixelSize": 18, "HideControls": true,
"Sections": []mono.Props{
{"Title": "After dark", "Theme": "dark"},
{"Title": "First light", "Theme": "light", "Description": "The morning collection opens here."},
},
})Go usage
err := mono.UI.Render(w, "pixel-scroll-transition", mono.Props{
"Pattern": "random",
"PixelSize": 24,
"Sections": []any{
map[string]any{
"Theme": "dark",
"Title": "Scroll into the light.",
},
map[string]any{
"Theme": "light",
"Title": "Keep scrolling.",
},
map[string]any{
"Theme": "dark",
"Title": "Scroll back to rewind.",
},
},
"Title": "Pixel transition playground",
})Rendered HTML
<section class="mu-pst" id="demo-pixel-scroll-transition" data-mono="pixel-scroll-transition" aria-label="Pixel transition playground" data-ps-pattern="random" data-ps-size="24">
<div class="mu-pst-tools" data-ps-controls hidden>
<div class="mu-pst-patterns" role="group" aria-label="Transition pattern">
<button type="button" data-ps-mode="random">Random</button>
<button type="button" data-ps-mode="wave">Wave</button>
<button type="button" data-ps-mode="diagonal">Diagonal</button>
<button type="button" data-ps-mode="spiral">Spiral</button>
<button type="button" data-ps-mode="checker">Checker</button>
</div>
</div>
<section class="mu-pst-panel" data-ps-panel data-ps-theme="dark" aria-labelledby="demo-pixel-scroll-transition-panel-0">
<h2 class="mu-pst-title" id="demo-pixel-scroll-transition-panel-0">Scroll into the light.</h2>
</section>
<div class="mu-pst-band" data-ps-band aria-hidden="true" hidden><canvas data-ps-canvas></canvas></div>
<section class="mu-pst-panel" data-ps-panel data-ps-theme="light" aria-labelledby="demo-pixel-scroll-transition-panel-1">
<h2 class="mu-pst-title" id="demo-pixel-scroll-transition-panel-1">Keep scrolling.</h2>
</section>
<div class="mu-pst-band" data-ps-band aria-hidden="true" hidden><canvas data-ps-canvas></canvas></div>
<section class="mu-pst-panel" data-ps-panel data-ps-theme="dark" aria-labelledby="demo-pixel-scroll-transition-panel-2">
<h2 class="mu-pst-title" id="demo-pixel-scroll-transition-panel-2">Scroll back to rewind.</h2>
</section>
</section>Go template
<section class="mu-pst" id="{{.ID}}" data-mono="pixel-scroll-transition" aria-label="{{or .Title "Pixel scroll transition"}}" data-ps-pattern="{{or .Pattern "random"}}" data-ps-size="{{or .PixelSize 24}}">
{{if and .Sections (gt (len .Sections) 1) (not .HideControls)}}
<div class="mu-pst-tools" data-ps-controls hidden>
<div class="mu-pst-patterns" role="group" aria-label="Transition pattern">
<button type="button" data-ps-mode="random">Random</button>
<button type="button" data-ps-mode="wave">Wave</button>
<button type="button" data-ps-mode="diagonal">Diagonal</button>
<button type="button" data-ps-mode="spiral">Spiral</button>
<button type="button" data-ps-mode="checker">Checker</button>
</div>
</div>
{{end}}
{{range $i, $panel := .Sections}}
{{if $i}}<div class="mu-pst-band" data-ps-band aria-hidden="true" hidden><canvas data-ps-canvas></canvas></div>{{end}}
<section class="mu-pst-panel" data-ps-panel data-ps-theme="{{or .Theme "auto"}}"{{with .Title}} aria-labelledby="{{$.ID}}-panel-{{$i}}"{{end}}>
{{with .Title}}<h2 class="mu-pst-title" id="{{$.ID}}-panel-{{$i}}">{{.}}</h2>{{end}}
{{with .Description}}<p class="mu-pst-description">{{.}}</p>{{end}}
</section>
{{else}}<p class="mu-pst-empty">No sections to display.</p>{{end}}
</section>Component CSS
.mu-pst[data-mono] {
--ps-dark: #080808;
--ps-light: #fafafa;
position: relative;
isolation: isolate;
container-type: inline-size;
width: 100%;
}
.mu-pst-tools {
position: sticky;
top: calc(var(--ps-controls-top, 1.5rem) - 1.5rem);
z-index: 2;
display: flex;
justify-content: center;
height: 0;
padding-block-start: 1.5rem;
margin-block-end: -1.5rem;
padding-inline: .6rem;
pointer-events: none;
}
.mu-pst-tools[hidden], .mu-pst-band[hidden] { display: none; }
.mu-pst-patterns {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-self: flex-start;
gap: .15rem;
padding: .2rem;
border: 1px solid #383838;
border-radius: .45rem;
background: #101010;
color: #f6f6f6;
pointer-events: auto;
}
.mu-pst-patterns button {
appearance: none;
border: 0;
border-radius: .24rem;
padding: .48em .78em;
min-height: 2.25rem;
background: transparent;
color: inherit;
font: 500 clamp(.76rem, 1.5cqi, 1rem)/1.2 Inter, "Helvetica Neue", Arial, sans-serif;
letter-spacing: -.015em;
cursor: pointer;
}
@media (hover: hover) {
.mu-pst-patterns button:hover { background: #333; }
}
.mu-pst-patterns button[aria-pressed="true"] { background: #fafafa; color: #101010; }
.mu-pst-patterns button:focus-visible { outline: 2px solid currentColor; outline-offset: -4px; }
.mu-pst-panel {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 1rem;
min-height: var(--ps-panel-height, 100vh);
min-height: var(--ps-panel-height, 100svh);
padding: clamp(4.5rem, 9cqi, 8rem) clamp(1.25rem, 4cqi, 4rem);
text-align: center;
background-color: var(--ps-dark);
color: var(--ps-light);
}
.mu-pst-panel:nth-of-type(even), .mu-pst-panel[data-ps-theme="light"] {
background-color: var(--ps-light);
color: var(--ps-dark);
}
.mu-pst-panel[data-ps-theme="dark"] {
background-color: var(--ps-dark);
color: var(--ps-light);
}
.mu-pst[data-mono] .mu-pst-title {
max-width: 30ch;
margin: 0;
color: inherit;
font: 400 clamp(2rem, 5cqi, 3.75rem)/1.1 "Playfair Display", Baskerville, Georgia, "Times New Roman", serif;
letter-spacing: -.035em;
text-wrap: balance;
overflow-wrap: anywhere;
}
.mu-pst-description {
max-width: 47ch;
margin: 0;
color: inherit;
font-size: clamp(.9375rem, 1.5cqi, 1.1rem);
line-height: 1.55;
text-wrap: pretty;
overflow-wrap: anywhere;
}
.mu-pst-band {
position: relative;
height: var(--ps-band-height, clamp(15rem, 46svh, 31.25rem));
overflow: hidden;
pointer-events: none;
}
.mu-pst-band canvas {
display: block;
width: 100%;
height: 100%;
image-rendering: pixelated;
}
.mu-pst-empty { padding: 2rem; }
@media (prefers-reduced-motion: reduce), (forced-colors: active) {
.mu-pst-band, .mu-pst-tools { display: none; }
}JavaScript module
const patterns = new Set(['random', 'wave', 'diagonal', 'spiral', 'checker']);
const clamp = (value, low, high) => Math.max(low, Math.min(high, value));
// The thresholds stay fixed when a visitor reverses direction or revisits a mode.
function makeGrid(cols, rows, pattern, seed) {
const thresholds = new Float32Array(cols * rows);
const accents = new Uint8Array(thresholds.length);
let state = (seed + 1) * 997 + cols * 31 + rows;
function random() {
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
return state / 4294967296;
}
for (let row = 0; row < rows; row++) {
const y = (row + .5) / rows;
for (let col = 0; col < cols; col++) {
const x = (col + .5) / cols;
const noise = random() - .5;
let offset = 0;
switch (pattern) {
case 'wave': offset = Math.sin(x * Math.PI * 3 + y * Math.PI) * .17; break;
case 'diagonal': offset = (x - .5) * .40; break;
case 'checker': offset = ((col + row) % 2 ? .13 : -.13); break;
case 'spiral': {
const angle = Math.atan2(y - .5, x - .5) / (2 * Math.PI);
const radius = Math.hypot(x - .5, y - .5);
offset = Math.sin((angle + radius * 1.55) * 2 * Math.PI) * .20;
break;
}
default: offset = noise * .68;
}
const i = row * cols + col;
thresholds[i] = clamp(1 - y + offset + noise * .09, .01, .99);
if (random() < .16) accents[i] = 2 + Math.floor(random() * 3);
}
}
return {thresholds, accents};
}
function geometry(band, rect, cell, pattern) {
// One backing pixel per square. Cap the bitmap even for a very large embed.
const size = Math.max(cell, Math.sqrt(rect.width * rect.height / 8192));
const cols = clamp(Math.ceil(rect.width / size), 1, 8192);
const rows = clamp(Math.ceil(rect.height / size), 1, Math.floor(8192 / cols));
if (band.canvas.width !== cols || band.canvas.height !== rows || !band.frame) {
band.canvas.width = cols;
band.canvas.height = rows;
band.frame = band.ctx.createImageData(cols, rows);
band.pixels = new Uint32Array(band.frame.data.buffer);
band.pattern = null;
}
if (band.pattern !== pattern) {
band.grid = makeGrid(cols, rows, pattern, band.index);
band.pattern = pattern;
band.progress = -1;
}
}
function draw(band, progress, from, to) {
if (band.from !== from || band.to !== to) {
const ctx = band.ctx;
// Asking the canvas to resolve CSS colors also supports user-supplied palettes.
band.colors = [from, to, '#575757', '#898989', '#bcbcbc'].map(color => {
ctx.fillStyle = color;
ctx.fillRect(0, 0, 1, 1);
return new Uint32Array(ctx.getImageData(0, 0, 1, 1).data.buffer)[0];
});
band.from = from;
band.to = to;
band.progress = -1;
}
if (progress === band.progress) return;
band.progress = progress;
const {thresholds, accents} = band.grid;
if (progress === 0 || progress === 1 || from === to) {
band.pixels.fill(band.colors[progress === 1 ? 1 : 0]);
} else {
for (let i = 0; i < thresholds.length; i++) {
const delta = progress - thresholds[i];
const shade = accents[i] && Math.abs(delta) < .055 ? accents[i] : Number(delta >= 0);
band.pixels[i] = band.colors[shade];
}
}
band.ctx.putImageData(band.frame, 0, 0);
}
export function mount(root) {
const doc = root.ownerDocument;
const win = doc.defaultView;
const bands = [...root.querySelectorAll('[data-ps-band]')].map((el, index) => {
const canvas = el.querySelector('[data-ps-canvas]');
const ctx = canvas?.getContext('2d', {alpha: false, willReadFrequently: true});
return {el, canvas, ctx, index, before: el.previousElementSibling, after: el.nextElementSibling};
}).filter(band => band.ctx && band.before && band.after);
if (!bands.length) return;
const cell = clamp(parseInt(root.dataset.psSize, 10) || 24, 4, 120);
const controls = root.querySelector('[data-ps-controls]');
const buttons = [...root.querySelectorAll('[data-ps-mode]')];
const mode = (root.dataset.psPattern || '').toLowerCase();
let pattern = patterns.has(mode) ? mode : 'random';
const motion = win.matchMedia('(prefers-reduced-motion: reduce)');
const scrollers = [];
for (let node = root.parentElement; node && node !== doc.body && node !== doc.documentElement; node = node.parentElement) {
if (['scroll', 'auto', 'overlay'].includes(win.getComputedStyle(node).overflowY)) scrollers.push(node);
}
const visible = new Set();
let frame = 0;
function render() {
frame = 0;
if (motion.matches || !root.isConnected) return;
let top = 0, bottom = win.innerHeight;
for (const parent of scrollers) {
const edge = parent.getBoundingClientRect().top + parent.clientTop;
top = Math.max(top, edge);
bottom = Math.min(bottom, edge + parent.clientHeight);
}
if (bottom <= top) return;
for (const band of visible) {
const rect = band.el.getBoundingClientRect();
if (rect.bottom <= top || rect.top >= bottom || !rect.width || !rect.height) continue;
geometry(band, rect, cell, pattern);
const progress = clamp((bottom - rect.top) / ((bottom - top + rect.height) * .8), 0, 1);
draw(band, progress, win.getComputedStyle(band.before).backgroundColor, win.getComputedStyle(band.after).backgroundColor);
}
}
function schedule() {
if (visible.size && !frame && !motion.matches) frame = win.requestAnimationFrame(render);
}
function select(value) {
pattern = value;
root.dataset.psPattern = value;
for (const button of buttons) button.setAttribute('aria-pressed', String(button.dataset.psMode === value));
schedule();
}
controls?.addEventListener('click', event => {
const button = event.target.closest('[data-ps-mode]');
if (button && controls.contains(button) && patterns.has(button.dataset.psMode)) select(button.dataset.psMode);
});
function updateMotion() {
for (const band of bands) band.el.hidden = motion.matches;
if (controls) controls.hidden = motion.matches;
if (motion.matches && frame) {
win.cancelAnimationFrame(frame);
frame = 0;
}
schedule();
}
// Only visible transition bands need scroll listeners or a drawing buffer.
let listening = false;
const targets = [win, ...scrollers];
function observeScroll(active) {
if (active === listening) return;
listening = active;
for (const target of targets) {
if (active) target.addEventListener('scroll', schedule, {passive: true});
else target.removeEventListener('scroll', schedule);
}
if (active) schedule();
}
if (win.IntersectionObserver) {
const byElement = new Map(bands.map(band => [band.el, band]));
const observer = new win.IntersectionObserver(entries => {
for (const entry of entries) {
const band = byElement.get(entry.target);
if (entry.isIntersecting) visible.add(band);
else visible.delete(band);
}
observeScroll(visible.size > 0);
schedule();
});
for (const {el} of bands) observer.observe(el);
} else {
for (const band of bands) visible.add(band);
observeScroll(true);
}
if (win.ResizeObserver) {
const sizes = new win.ResizeObserver(schedule);
sizes.observe(root);
for (const {el} of bands) sizes.observe(el);
for (const parent of scrollers) sizes.observe(parent);
}
win.addEventListener('resize', schedule);
motion.addEventListener('change', updateMotion);
win.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', schedule);
// Mono also lets the reader change the page theme without changing the OS setting.
if (win.MutationObserver) {
const theme = new win.MutationObserver(schedule);
theme.observe(doc.documentElement, {attributes: true, attributeFilter: ['data-theme', 'class', 'style']});
}
select(pattern);
updateMotion();
}Use the HTML in a static site
Copy the rendered HTML above. Download these assets into the same folder as your page, then add the tags below to its head.
Shared CSS Component CSS Ready-to-use module
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./pixel-scroll-transition.css">
<script type="module" src="./pixel-scroll-transition.js"></script>Copy into your project
Run this command from the Mono checkout. Set -out to the component directory in your application.
go run ./cmd/mono-ui add -out ./components pixel-scroll-transitionExample properties (JSON)
{
"Pattern": "random",
"PixelSize": 24,
"Sections": [
{
"Theme": "dark",
"Title": "Scroll into the light."
},
{
"Theme": "light",
"Title": "Keep scrolling."
},
{
"Theme": "dark",
"Title": "Scroll back to rewind."
}
],
"Title": "Pixel transition playground"
}