Live chart
The signature piece: a canvas map of dots and line figures with labelled points that drifts and can be dragged, a readout with a day and night switch, live clock, date and position, a floating framed notice, a floating product tile and a retro help window. Day mode swaps black and white and inverts the map.
Part of the style
Night Chart Console
7 parts that belong together, with their tokens, type and rules.
https://kitbash.me/components/night-chart-console/chart/figma.html?palette=cd9b878c69fb756c
Use the Figma MCP server (https://mcp.figma.com/mcp). Download https://kitbash.me/components/night-chart-console/chart/figma.html?palette=cd9b878c69fb756c and pass that whole HTML document to the html_to_figma tool, so the part "Live chart" lands as editable layers in my open Figma file. Fonts come from Google Fonts: if a face is missing in Figma, pick the closest one.
Code
<style>
/* Goes after the pack's base.css */
:root {
--night: #ebccc8;
--paper: #1b0d13;
--paper-dim: rgba(27, 13, 19, 0.6);
--paper-faint: rgba(27, 13, 19, 0.36);
--night-dim: rgba(235, 204, 200, 0.6);
--night-faint: rgba(235, 204, 200, 0.4);
--smoke: #a28884;
--fog: #211219;
--wordmark-shade: #34232b;
--map-ghost: #544049;
--retro-face: #3c2b33;
--retro-chip: #23141b;
--retro-edge: #705b64;
--scene-sky: #917a84;
--scene-sea: #8d7f85;
--retro-bar: #9d7486;
--retro-bar-end: #cba9b7;
--map-line: #d4b8c4;
--scene-glow: #79636d;
--map-live: #66545c;
--scene-lamp: #7b4b62;
--scene-amber: #624d56;
}
</style>
<section class="chart" aria-label="Live relay map">
<div class="chart__sky">
<canvas
class="chart__map"
tabindex="0"
role="img"
aria-label="Map of the rooftop aerials passing the signal along the coast. Drag it, or use the left and right arrow keys, to move along the coast."
></canvas>
<div class="chart__ticker">
<div class="chart__readout">
<button class="chart__switch" type="button" role="switch" aria-checked="true" aria-label="Night view">
<span class="chart__switch-pill" aria-hidden="true">
<span class="chart__switch-label chart__switch-label--night">Night</span>
<span class="chart__switch-label chart__switch-label--day">Day</span>
<span class="chart__switch-knob">
<svg class="chart__switch-icon chart__switch-icon--night" viewBox="0 0 24 24" focusable="false">
<path d="M20.5 14.2A8.6 8.6 0 0 1 9.8 3.5a8.6 8.6 0 1 0 10.7 10.7z" />
</svg>
<svg class="chart__switch-icon chart__switch-icon--day" viewBox="0 0 24 24" focusable="false">
<circle cx="12" cy="12" r="4.5" />
<path d="M12 2v3M12 19v3M2 12h3M19 12h3M4.9 4.9 7 7M17 17l2.1 2.1M4.9 19.1 7 17M17 7l2.1-2.1" />
</svg>
</span>
</span>
</button>
<span class="chart__label">Gull Reach · 87.9</span>
<time class="chart__value chart__clock">12:00:00 AM</time>
<span class="chart__sub chart__date">SAT · NOV 28, 2026</span>
<span class="chart__sub">47.6021° N · 9.4410° W</span>
</div>
<div class="chart__readout chart__readout--end">
<span class="chart__label">Next set</span>
<span class="chart__value">Nov 28</span>
</div>
</div>
<div class="chart__help">
<div class="chart__window" role="dialog" aria-label="About this map" inert>
<div class="chart__window-bar">
<span class="chart__window-title">Relay Map</span>
<button class="chart__window-close" type="button" aria-label="Close">
<svg viewBox="0 0 10 10" aria-hidden="true" focusable="false"><path d="M2 2l6 6M8 2l-6 6" /></svg>
</button>
</div>
<p class="chart__window-body">Every dot is a rooftop aerial passing our signal along the coast, live, right now. Drag the map left or right to follow it.</p>
</div>
<button class="chart__help-button" type="button" aria-expanded="false">What is this map?</button>
</div>
</div>
<aside class="chart__notice" aria-label="Tonight on air">
<button class="chart__close" type="button" aria-label="Dismiss this notice">
<svg viewBox="0 0 10 10" aria-hidden="true" focusable="false"><path d="M1.5 1.5l7 7M8.5 1.5l-7 7" /></svg>
</button>
<h2 class="chart__notice-title">Tonight on air</h2>
<p class="chart__notice-copy">Who is behind the desk on the boat this week.</p>
<a class="chart__show" href="#">
<span class="chart__show-thumb" aria-hidden="true"></span>
<span class="chart__show-name">Fog Set with Odile Marr: Sat, Nov. 28th, 11PM</span>
<span class="chart__show-state">Listen</span>
</a>
</aside>
<aside class="chart__tile" aria-label="From the shop">
<button class="chart__close chart__close--chip" type="button" aria-label="Dismiss this product">
<svg viewBox="0 0 10 10" aria-hidden="true" focusable="false"><path d="M1.5 1.5l7 7M8.5 1.5l-7 7" /></svg>
</button>
<a class="chart__tile-link" href="#">
<span class="chart__tile-picture">
<svg class="chart__tile-art" viewBox="0 0 200 200" aria-hidden="true" focusable="false">
<path class="chart__tile-cloth" d="M50 124C50 64 76 40 100 40s50 24 50 84z" />
<rect class="chart__tile-cloth" x="43" y="116" width="114" height="44" rx="7" />
<path class="chart__tile-rib" d="M52 122h96" />
<text class="chart__tile-print" x="100" y="145" text-anchor="middle">Small Hours</text>
</svg>
</span>
<span class="chart__tile-name">Small Hours Dock Beanie</span>
</a>
</aside>
</section>
<style>
/* Night is the default. Day swaps the two inks; the map itself is drawn once in night colours and inverted. */
.chart {
--chart-ground: var(--night);
--chart-ink: var(--paper);
--chart-ink-dim: var(--paper-dim);
--chart-ink-faint: var(--paper-faint);
position: relative;
background-color: var(--chart-ground);
color: var(--chart-ink);
transition: background-color var(--time-slow) ease, color var(--time-slow) ease;
}
.chart.is-day {
--chart-ground: var(--paper);
--chart-ink: var(--night);
--chart-ink-dim: var(--night-dim);
--chart-ink-faint: var(--night-faint);
}
.chart__sky {
position: relative;
height: clamp(40rem, 56.25vw, 50.625rem);
overflow: hidden;
border-bottom: var(--frame) solid var(--chart-ink);
transition: border-color var(--time-slow) ease;
}
.chart__map {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
cursor: grab;
touch-action: pan-y;
-webkit-mask-image: linear-gradient(to bottom, var(--night) 74%, transparent 96%);
mask-image: linear-gradient(to bottom, var(--night) 74%, transparent 96%);
transition: filter var(--time-slow) ease;
}
.chart__map.is-held {
cursor: grabbing;
}
.chart.is-day .chart__map {
filter: invert(1) grayscale(1);
}
/* Readout along the bottom edge. It lets drags through to the map; only the switch takes clicks. */
.chart__ticker {
position: absolute;
inset: auto 0 0;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: var(--space-s);
padding: 0 clamp(1rem, 2.2vw, 2rem) var(--space-m);
pointer-events: none;
}
.chart__readout {
display: grid;
min-width: 0;
justify-items: start;
text-transform: uppercase;
white-space: nowrap;
}
.chart__readout--end {
justify-items: end;
}
.chart__label {
color: var(--chart-ink-dim);
font-size: var(--text-s);
letter-spacing: var(--tracking-wide);
line-height: 1.3;
}
.chart__value {
font-size: var(--text-l);
letter-spacing: var(--tracking-tight);
line-height: 1.15;
}
.chart__sub {
color: var(--chart-ink-faint);
font-size: var(--text-xs);
letter-spacing: var(--tracking-wide);
line-height: 1.4;
}
.chart__switch {
display: flex;
align-items: center;
min-height: var(--tap);
margin-bottom: 0.25rem;
padding: 0;
border: 0;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
pointer-events: auto;
}
.chart__switch-pill {
position: relative;
display: block;
width: 6.5rem;
height: 1.75rem;
border: var(--frame) solid currentColor;
border-radius: var(--radius-pill);
background-color: var(--chart-ground);
transition: background-color var(--time-slow) ease;
}
.chart__switch-label {
position: absolute;
top: 50%;
left: 0.8125rem;
font-size: var(--text-xs);
letter-spacing: var(--tracking-wide);
line-height: 1;
text-transform: uppercase;
translate: 0 -50%;
transition: opacity var(--time) ease;
}
.chart__switch-label--day {
left: auto;
right: 0.625rem;
opacity: 0;
}
.chart__switch-knob {
position: absolute;
top: 0.1875rem;
left: 0.1875rem;
display: grid;
place-items: center;
width: 1.25rem;
height: 1.25rem;
border-radius: 50%;
background-color: var(--chart-ink);
color: var(--chart-ground);
translate: 4.625rem 0;
transition: translate var(--time) var(--ease-out), background-color var(--time-slow) ease, color var(--time-slow) ease;
}
.chart__switch-icon {
grid-area: 1 / 1;
width: 0.8125rem;
height: 0.8125rem;
fill: currentColor;
stroke: currentColor;
stroke-linecap: round;
stroke-width: 2.2;
transition: opacity var(--time) ease;
}
.chart__switch-icon--night {
stroke: none;
}
.chart__switch-icon--day {
opacity: 0;
}
.chart.is-day .chart__switch-knob {
translate: 0 0;
}
.chart.is-day .chart__switch-label--night,
.chart.is-day .chart__switch-icon--night {
opacity: 0;
}
.chart.is-day .chart__switch-label--day,
.chart.is-day .chart__switch-icon--day {
opacity: 1;
}
/* Retro help: a bevelled button and the small window it opens. */
.chart__help {
position: absolute;
right: clamp(1rem, 1.7vw, 1.5rem);
bottom: 7.5rem;
z-index: 2;
display: grid;
justify-items: end;
gap: 0.75rem;
pointer-events: none;
}
.chart__help-button,
.chart__window,
.chart__window-close {
border: 2px solid;
border-color: var(--paper) var(--retro-edge) var(--retro-edge) var(--paper);
color: var(--night);
}
.chart__help-button {
position: relative;
padding: 0.25rem 0.625rem;
border-radius: var(--radius-chip);
background: var(--retro-chip);
font: 700 var(--text-xs) / 1.2 var(--font-mono);
text-transform: uppercase;
white-space: nowrap;
cursor: pointer;
pointer-events: auto;
}
.chart__help-button::before,
.chart__window-close::before,
.chart__close::before {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: max(100%, var(--tap));
height: max(100%, var(--tap));
translate: -50% -50%;
}
.chart__help-button:active,
.chart__window-close:active {
border-color: var(--retro-edge) var(--paper) var(--paper) var(--retro-edge);
}
.chart__window {
width: min(16.25rem, 100vw - 2 * var(--gutter));
background: var(--retro-face);
opacity: 0;
visibility: hidden;
translate: 0 0.375rem;
transition: opacity var(--time) ease, translate var(--time) var(--ease-out), visibility 0s var(--time);
pointer-events: auto;
}
.chart__help.is-open .chart__window {
opacity: 1;
visibility: visible;
translate: 0 0;
transition-delay: 0s;
}
.chart__window-bar {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.125rem 0.125rem 0.125rem 0.375rem;
background: linear-gradient(90deg, var(--retro-bar), var(--retro-bar-end));
color: var(--paper);
font-size: var(--text-xs);
font-weight: 700;
}
.chart__window-close {
position: relative;
display: grid;
place-items: center;
width: 1rem;
height: 0.875rem;
padding: 0;
border-width: 1px;
background: var(--retro-face);
cursor: pointer;
}
.chart__window-close svg,
.chart__close svg {
width: 0.625rem;
height: 0.625rem;
fill: none;
stroke: currentColor;
stroke-width: 1.2;
}
.chart__window-close svg {
width: 0.5rem;
height: 0.5rem;
stroke-width: 2;
}
.chart__window-body {
margin: 0;
padding: 0.75rem;
font-size: var(--text-xs);
line-height: 1.5;
}
/* Floating frames over the map. */
.chart__notice,
.chart__tile {
position: absolute;
transition: opacity var(--time) ease, visibility 0s var(--time);
}
.chart__notice.is-dismissed,
.chart__tile.is-dismissed {
opacity: 0;
visibility: hidden;
}
.chart__close {
position: absolute;
top: 0.5rem;
right: 0.5rem;
z-index: 1;
display: grid;
place-items: center;
width: 1.375rem;
height: 1.375rem;
padding: 0;
border: 0;
border-radius: var(--radius-chip);
background: none;
color: inherit;
cursor: pointer;
}
.chart__close--chip {
background: color-mix(in srgb, var(--night) 30%, transparent);
color: var(--paper);
}
.chart__notice {
left: var(--gutter);
top: clamp(13rem, 21vw, 19rem);
width: min(25.5rem, 100% - 2 * var(--gutter));
padding: var(--space-l);
border: var(--frame) solid var(--chart-ink);
background-color: var(--chart-ground);
transition: opacity var(--time) ease, visibility 0s var(--time), background-color var(--time-slow) ease, border-color var(--time-slow) ease;
}
.chart__notice-title {
margin: 0 0 var(--space-xs);
font-size: var(--text-l);
font-weight: 400;
letter-spacing: var(--tracking-tight);
line-height: 1.15;
text-transform: uppercase;
}
.chart__notice-copy {
max-width: 17rem;
margin: 0 0 var(--space-s);
}
.chart__show {
display: grid;
grid-template-columns: auto minmax(0, 1fr) auto;
align-items: center;
gap: var(--space-m);
padding: 0.9375rem;
border: var(--frame) solid var(--chart-ink);
color: inherit;
text-decoration: none;
transition: border-color var(--time-slow) ease;
}
.chart__show-thumb {
width: 3rem;
height: 3rem;
border: var(--frame) solid var(--chart-ink-dim);
background:
radial-gradient(circle at 68% 30%, var(--scene-amber) 0 9%, transparent 10%),
linear-gradient(to top, var(--night) 28%, transparent 28%),
linear-gradient(160deg, var(--scene-sea), var(--scene-sky) 60%, var(--scene-glow));
}
.chart__show-state {
text-decoration: underline var(--frame) currentColor;
text-transform: uppercase;
text-underline-offset: 0.4em;
transition: text-decoration-color var(--time-fast) var(--ease-fast);
}
.chart__show:hover .chart__show-state,
.chart__show:focus-visible .chart__show-state {
text-decoration-color: transparent;
}
.chart__tile {
right: var(--gutter);
top: clamp(8.75rem, 16vw, 16.5rem);
width: 9.0625rem;
}
.chart__tile-link {
display: block;
color: inherit;
text-decoration: none;
transition: translate var(--time) ease-out;
}
.chart__tile-link:hover,
.chart__tile-link:focus-visible {
translate: 0 calc(-1 * var(--lift));
}
.chart__tile-picture {
display: block;
aspect-ratio: 1;
border: var(--frame) solid var(--chart-ink);
border-bottom: 0;
background: var(--paper);
transition: border-color var(--time-slow) ease;
}
.chart__tile-art {
display: block;
width: 100%;
height: 100%;
}
.chart__tile-cloth {
fill: var(--night);
}
.chart__tile-rib {
fill: none;
stroke: var(--smoke);
stroke-width: 1.5;
}
.chart__tile-print {
fill: var(--wordmark-shade);
font: italic 700 17px var(--font-script);
paint-order: stroke;
stroke: var(--night);
stroke-width: 5px;
}
.chart__tile-name {
display: block;
padding: 0.8125rem var(--space-s);
border: var(--frame) solid var(--chart-ink);
background-color: var(--chart-ground);
text-align: center;
transition: background-color var(--time-slow) ease, border-color var(--time-slow) ease;
}
.chart :focus-visible {
outline: var(--frame) solid var(--chart-ink);
outline-offset: 3px;
}
.chart__map:focus-visible {
outline-offset: -4px;
}
@media (max-width: 48rem) {
.chart__help {
right: var(--gutter);
bottom: 6.75rem;
}
.chart__notice {
position: relative;
top: auto;
left: auto;
width: auto;
margin: var(--space-l) var(--gutter);
padding: var(--space-m);
}
.chart__notice.is-dismissed,
.chart__tile {
display: none;
}
.chart__show {
gap: 0.75rem;
padding: 0.75rem;
}
}
@media (prefers-reduced-motion: reduce) {
.chart,
.chart *,
.chart *::before {
transition-duration: 0s;
}
}
</style>
<script>
(() => {
const WORLD_WIDTH = 2600;
const WORLD_HEIGHT = 1000;
const COLUMNS = 9;
const ROWS = 4;
const EMPTY_SHARE = 0.28;
const DUST_COUNT = 300;
const SEED = 879;
const START_CENTRE = 1300;
const DRIFT_PER_SECOND = 5;
const LONGEST_FRAME = 100;
const KEY_STEP = 60;
const TURN = Math.PI * 2;
const LABEL_SIZE = 15;
const DAY = "is-day";
const OPEN = "is-open";
const HELD = "is-held";
const DISMISSED = "is-dismissed";
const DAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
const MONTHS = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
/* Named aerials: each one starts a figure of lines. */
const MASTS = [
{ label: "Cold Harbour", x: 1180, y: 250, ink: "star", radius: 2.2 },
{ label: "Gull Reach", x: 560, y: 740, ink: "star", radius: 2.2 },
{ label: "Saltmarsh", x: 800, y: 770, ink: "star", radius: 2.2 },
{ label: "Old Quarry", x: 1480, y: 830, ink: "star", radius: 2.2 },
{ label: "Kestrel Hill", x: 2080, y: 690, ink: "star", radius: 2.2 },
{ label: "Pier Nine", x: 2440, y: 300, ink: "star", radius: 2.2 },
];
/* Named points that stand alone, off the lines. */
const BEACONS = [
{ label: "The Boat", x: 1300, y: 540, ink: "live", radius: 2.4 },
{ label: "Lightship", x: 1580, y: 705, ink: "ghost", radius: 5 },
];
const seededRandom = (seed) => {
let state = seed;
return () => {
state = (state + 0x6d2b79f5) | 0;
let mixed = Math.imul(state ^ (state >>> 15), 1 | state);
mixed = (mixed + Math.imul(mixed ^ (mixed >>> 7), 61 | mixed)) ^ mixed;
return ((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296;
};
};
const between = (random, low, high) => low + random() * (high - low);
const wholeBetween = (random, low, high) => Math.floor(between(random, low, high + 1));
const walkFrom = (random, start, steps) => {
const points = [start];
let heading = between(random, 0, TURN);
for (let step = 0; step < steps; step += 1) {
heading += between(random, -1.1, 1.1);
const reach = between(random, 45, 135);
const last = points[points.length - 1];
points.push({ x: last.x + Math.cos(heading) * reach, y: last.y + Math.sin(heading) * reach });
}
return points;
};
const ringAround = (random, centre) => {
const corners = wholeBetween(random, 3, 5);
const turn = between(random, 0, TURN);
const ring = Array.from({ length: corners }, (_, corner) => {
const angle = turn + (corner / corners) * TURN + between(random, -0.3, 0.3);
const reach = between(random, 28, 72);
return { x: centre.x + Math.cos(angle) * reach, y: centre.y + Math.sin(angle) * reach };
});
return [...ring, ring[0]];
};
/* A figure is a list of connected lines: a ring with a tail, or a spine with an optional branch. */
const figureFrom = (random, start) => {
if (random() < 0.2) {
const ring = ringAround(random, start);
return [ring, walkFrom(random, ring[1], wholeBetween(random, 1, 3))];
}
const spine = walkFrom(random, start, wholeBetween(random, 2, 6));
const branches = random() < 0.35 && spine.length > 3;
return branches ? [spine, walkFrom(random, spine[Math.floor(spine.length / 2)], wholeBetween(random, 1, 2))] : [spine];
};
const scatteredStarts = (random) => {
const starts = [];
for (let column = 0; column < COLUMNS; column += 1) {
for (let row = 0; row < ROWS; row += 1) {
if (random() < EMPTY_SHARE) continue;
starts.push({
x: ((column + between(random, 0.15, 0.85)) * WORLD_WIDTH) / COLUMNS,
y: ((row + between(random, 0.15, 0.85)) * WORLD_HEIGHT) / ROWS,
});
}
}
return starts;
};
const buildWorld = () => {
const random = seededRandom(SEED);
const starts = [...MASTS, ...scatteredStarts(random)];
return {
figures: starts.map((start) => figureFrom(random, start)),
dust: Array.from({ length: DUST_COUNT }, () => ({
x: random() * WORLD_WIDTH,
y: random() * WORLD_HEIGHT,
radius: between(random, 0.4, 1.25),
glow: between(random, 0.25, 1),
})),
places: [...MASTS, ...BEACONS],
};
};
const WORLD = buildWorld();
const readInks = (root) => {
const style = getComputedStyle(root);
const token = (name) => style.getPropertyValue(name).trim();
return {
star: token("--paper"),
line: token("--map-line"),
live: token("--map-live"),
ghost: token("--map-ghost"),
ground: token("--night"),
labelFont: `500 ${LABEL_SIZE}px ${token("--font-label")}`,
};
};
const dot = (context, at, radius) => {
context.beginPath();
context.arc(at.x, at.y, radius, 0, TURN);
context.fill();
};
const paintDust = (context, place, inks) => {
context.fillStyle = inks.star;
WORLD.dust.forEach((speck) => {
context.globalAlpha = speck.glow;
dot(context, place(speck), speck.radius);
});
context.globalAlpha = 1;
};
const paintFigures = (context, place, inks) => {
context.strokeStyle = inks.line;
context.lineWidth = 1;
context.beginPath();
WORLD.figures.flat().forEach((line) => {
line.forEach((point, index) => {
const at = place(point);
if (index === 0) context.moveTo(at.x, at.y);
else context.lineTo(at.x, at.y);
});
});
context.stroke();
context.fillStyle = inks.star;
WORLD.figures.flat(2).forEach((point) => dot(context, place(point), 1.3));
};
const paintPlaces = (context, place, inks) => {
context.font = inks.labelFont;
context.shadowColor = inks.ground;
context.shadowBlur = 5;
WORLD.places.forEach((spot) => {
const at = place(spot);
context.fillStyle = inks[spot.ink];
dot(context, at, spot.radius);
context.fillText(spot.label, at.x + spot.radius + 4, at.y - 5);
});
context.shadowBlur = 0;
};
/* The world repeats sideways: these are the left edges, in world units, of every copy that shows. */
const copyEdges = (view) => {
const left = view.centre - view.width / view.scale / 2;
const first = -(((left % WORLD_WIDTH) + WORLD_WIDTH) % WORLD_WIDTH);
const edges = [];
for (let edge = first; edge * view.scale < view.width; edge += WORLD_WIDTH) edges.push(edge);
return edges;
};
const paint = (context, view, inks) => {
context.clearRect(0, 0, view.width, view.height);
copyEdges(view).forEach((edge) => {
const place = (point) => ({ x: (point.x + edge) * view.scale, y: point.y * view.scale });
paintDust(context, place, inks);
paintFigures(context, place, inks);
paintPlaces(context, place, inks);
});
};
const fitCanvas = (canvas, context, view) => {
const box = canvas.getBoundingClientRect();
const density = window.devicePixelRatio || 1;
canvas.width = Math.round(box.width * density);
canvas.height = Math.round(box.height * density);
context.setTransform(density, 0, 0, density, 0, 0);
view.width = box.width;
view.height = box.height;
view.scale = box.height / WORLD_HEIGHT;
};
const setUpDrag = (canvas, view, repaint) => {
let lastX = null;
const release = () => {
lastX = null;
view.held = false;
canvas.classList.remove(HELD);
};
canvas.addEventListener("pointerdown", (event) => {
lastX = event.clientX;
view.held = true;
canvas.classList.add(HELD);
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (lastX === null) return;
view.centre -= (event.clientX - lastX) / view.scale;
lastX = event.clientX;
repaint();
});
canvas.addEventListener("pointerup", release);
canvas.addEventListener("pointercancel", release);
canvas.addEventListener("keydown", (event) => {
const step = { ArrowLeft: -KEY_STEP, ArrowRight: KEY_STEP }[event.key];
if (!step) return;
event.preventDefault();
view.centre += step;
repaint();
});
};
/* The map creeps sideways while it is on screen and nobody holds it. */
const setUpDrift = (canvas, view, repaint) => {
let lastTime = null;
let onScreen = false;
const frame = (time) => {
if (!onScreen) {
lastTime = null;
return;
}
if (lastTime !== null && !view.held) view.centre += (DRIFT_PER_SECOND * Math.min(time - lastTime, LONGEST_FRAME)) / 1000;
lastTime = time;
repaint();
requestAnimationFrame(frame);
};
new IntersectionObserver(([entry]) => {
const wasOnScreen = onScreen;
onScreen = entry.isIntersecting;
if (onScreen && !wasOnScreen) requestAnimationFrame(frame);
}).observe(canvas);
};
const setUpMap = (root) => {
const canvas = root.querySelector(".chart__map");
const context = canvas.getContext("2d");
const inks = readInks(root);
const view = { width: 0, height: 0, scale: 1, centre: START_CENTRE, held: false };
const repaint = () => paint(context, view, inks);
new ResizeObserver(() => {
fitCanvas(canvas, context, view);
repaint();
}).observe(canvas);
document.fonts.load(inks.labelFont).then(repaint);
setUpDrag(canvas, view, repaint);
if (!matchMedia("(prefers-reduced-motion: reduce)").matches) setUpDrift(canvas, view, repaint);
};
const twoDigits = (number) => String(number).padStart(2, "0");
const clockText = (now) => {
const hours = now.getHours();
return `${twoDigits(hours % 12 || 12)}:${twoDigits(now.getMinutes())}:${twoDigits(now.getSeconds())} ${hours < 12 ? "AM" : "PM"}`;
};
const dateText = (now) => `${DAYS[now.getDay()]} · ${MONTHS[now.getMonth()]} ${now.getDate()}, ${now.getFullYear()}`;
const setUpClock = (root) => {
const clock = root.querySelector(".chart__clock");
const date = root.querySelector(".chart__date");
const tick = () => {
const now = new Date();
clock.textContent = clockText(now);
date.textContent = dateText(now);
};
tick();
setInterval(tick, 1000);
};
const setUpSwitch = (root) => {
const control = root.querySelector(".chart__switch");
control.addEventListener("click", () => {
const day = root.classList.toggle(DAY);
control.setAttribute("aria-checked", String(!day));
});
};
const setUpHelp = (root) => {
const help = root.querySelector(".chart__help");
const button = help.querySelector(".chart__help-button");
const helpWindow = help.querySelector(".chart__window");
const setOpen = (open) => {
help.classList.toggle(OPEN, open);
helpWindow.inert = !open;
button.setAttribute("aria-expanded", String(open));
};
button.addEventListener("click", () => setOpen(!help.classList.contains(OPEN)));
help.querySelector(".chart__window-close").addEventListener("click", () => {
setOpen(false);
button.focus();
});
help.addEventListener("keydown", (event) => {
if (event.key !== "Escape") return;
setOpen(false);
button.focus();
});
};
const setUpDismissing = (root) => {
root.querySelectorAll(".chart__close").forEach((close) => {
close.addEventListener("click", () => close.parentElement.classList.add(DISMISSED));
});
};
document.querySelectorAll(".chart").forEach((root) => {
setUpMap(root);
setUpClock(root);
setUpSwitch(root);
setUpHelp(root);
setUpDismissing(root);
});
})();
</script>