Generating 1080x1920 Viral Story Cards in Pure HTML5 Canvas (Zero Libraries)
This is Part 3 of the zero-dependency engineering series behind SOL-2027 — a web explorer for Eastman Kodak’s 1928–1989 13-month International Fixed Calendar. Part 1: For 61 Years, Kodak Ran on a 13-Month Calendar — S
This is Part 3 of the zero-dependency engineering series behind SOL-2027 — a web explorer for Eastman Kodak’s 1928–1989 13-month International Fixed Calendar.
- Part 1: For 61 Years, Kodak Ran on a 13-Month Calendar — So I Rebuilt It for 2027 in Vanilla JS
- Part 2: Computing All 13 New Moons of 2027 in Python & Pure JS (Zero Runtime Dependencies)
Once the birthday converter was live ("Wait, July 1 is actually SOL 14?!"), the main viral loop was clear: visitors wanted to save a sharp 1080×1920 (9:16) birthday card straight to Instagram Stories, TikTok, or iMessage.
My first instinct was html2canvas — and I ditched it within an hour.
Why We Ditched html2canvas
Pulling html2canvas (~200 KB minified) into a zero-dependency site wasn't just bloat. DOM-to-image libraries re-implement CSS layout in JavaScript, creating flaky edge cases where it matters most:
-
iOS Safari webfont & baseline drift: Google Fonts (
InterandSpace Mono) routinely shift vertical metrics or silently fall back inside cloned SVG/foreignObject passes on mobile Safari. -
Hidden DOM choreography: Exporting a crisp
1080×1920graphic from a 390px phone screen means maintaining an offscreen fixed-pixel mirror of your markup solely to screenshot it. - Zero reason for a server: Spinning up Puppeteer or Satori endpoints adds cold starts and bandwidth bills for something every browser's native Canvas 2D context paints locally in milliseconds.
Instead, we wrote a self-contained HTML5 Canvas 2D renderer directly inside app.js — zero dependencies, zero upload latency, and pixel-identical output across desktop and mobile.
1. One Offscreen Canvas, Two Editorial Themes
Rather than maintaining two drawing pipelines, renderStoryCardCanvas(result, theme) allocates a detached 1080×1920 <canvas> in memory (never attached to the DOM) and swaps one semantic palette block between Swiss Archival (#F4F0E8 parchment + #D94420 vermilion) and Celestial Midnight (#0E1A2B navy + #D9B464 gold).
In Celestial mode, we seed a tiny Park–Miller LCG directly from result.doy (day of year) so every birthday gets a unique, deterministic 95-star field with zero external textures:
const W = 1080, H = 1920;
const canvas = document.createElement('canvas');
canvas.width = W;
canvas.height = H;
const ctx = canvas.getContext('2d');
const isCelestial = theme === 'celestial';
const BG = isCelestial ? '#0E1A2B' : '#F4F0E8';
const INK = isCelestial ? '#F3ECDC' : '#141414';
const ACCENT = isCelestial ? '#D9B464' : '#D94420';
const sans = '"Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif';
const mono = '"Space Mono", ui-monospace, Menlo, Consolas, monospace';
ctx.fillStyle = BG;
ctx.fillRect(0, 0, W, H);
if (isCelestial) {
let seed = result.doy * 7919 + 137;
for (let i = 0; i < 95; i++) {
seed = (seed * 16807) % 2147483647;
const sx = (seed % (W - 120)) + 60;
seed = (seed * 16807) % 2147483647;
const sy = (seed % (H - 120)) + 60;
ctx.fillStyle = i % 5 === 0 ? 'rgba(217, 180, 100, 0.5)' : 'rgba(243, 236, 220, 0.32)';
ctx.beginPath();
ctx.arc(sx, sy, i % 7 === 0 ? 2.1 : 1.1, 0, Math.PI * 2);
ctx.fill();
}
} else {
ctx.fillStyle = INK;
ctx.fillRect(72, 84, W - 144, 7); // Swiss archival top rule
}
Even the 2027 lunar phase icon is pure math: drawMoonDisc() clips a circular ctx.arc() and draws the exact terminator with ctx.ellipse(cx, cy, Math.abs(Math.cos(p * 2 * Math.PI)) * radius, radius, ...) — zero emoji inconsistency across operating systems.
2. Swiss Grid Math: The 7×4 Mini-Matrix
Canvas 2D has no CSS Grid, but Swiss poster design is pure coordinate arithmetic. With fixed margins (padX = 88, innerW = 904), display headlines scale cleanly by length (164px, 136px, or 116px), descriptions wrap via ctx.measureText(), and the user's entire 28-day month plots in one loop:
const gridX = padX + boxPad, gridY = cursorY + 96;
const cellW = (innerW - boxPad * 2) / 7, cellH = 62;
const wdHeaders = ['FRI', 'SAT', 'SUN', 'MON', 'TUE', 'WED', 'THU'];
for (let d = 1; d <= 28; d++) {
const col = (d - 1) % 7;
const row = Math.floor((d - 1) / 7);
const cx = gridX + col * cellW;
const cy = gridY + 34 + row * cellH;
const isTarget = !result.isYearDay && d === result.ifcDay;
if (isTarget) {
drawRoundedRect(ctx, cx + 6, cy + 4, cellW - 12, cellH - 10, 6);
ctx.fillStyle = ACCENT;
ctx.fill();
} else if (col === 1 || col === 2) {
drawRoundedRect(ctx, cx + 6, cy + 4, cellW - 12, cellH - 10, 4);
ctx.fillStyle = isCelestial ? 'rgba(217, 180, 100, 0.08)' : '#EBE4D6';
ctx.fill();
}
ctx.font = isTarget ? `700 28px ${mono}` : `400 25px ${mono}`;
ctx.fillStyle = isTarget ? (isCelestial ? '#0E1A2B' : '#F4F0E8') : INK;
ctx.textAlign = 'center';
ctx.fillText(String(d), cx + cellW / 2, cy + 40);
}
For December 31 (result.isYearDay), a full-width pill renders underneath Row 4 for 29 · YEAR DAY (OUTSIDE THE 7-DAY WEEK).
3. Clean canvas.toBlob Downloads & Font Readiness
When exporting a 1080×1920 PNG on mobile, prefer canvas.toBlob() over giant base64 data: URIs, and wait briefly (1500ms) before revoking the object URL so mobile download managers finish cleanly:
function downloadStoryCard() {
if (!state.currentResult) return;
const canvas = renderStoryCardCanvas(state.currentResult, state.storyTheme);
const slug = state.currentResult.displayDate.replace(/\s+/g, '-').toUpperCase();
const filename = `SOL-2027-Birthday-${slug}-${state.storyTheme}.png`;
if (canvas.toBlob) {
canvas.toBlob((blob) => {
if (!blob) return;
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(() => URL.revokeObjectURL(url), 1500);
}, 'image/png');
} else {
const a = document.createElement('a');
a.href = canvas.toDataURL('image/png');
a.download = filename;
a.click();
}
}
Webfont gotcha: Canvas 2D won't re-paint if a Google Font finishes loading after fillText() runs. Because our download button is triggered post-interaction (with system fallback stacks baked into sans/mono), fonts are already cached — and if you ever invoke a Canvas renderer automatically on initial page load, simply await document.fonts.ready right before calling renderStoryCardCanvas().
Try the Live Card Generator & 2027 Editions
The entire renderer adds 0 KB of third-party dependencies and generates print-sharp 1080×1920 PNGs in milliseconds:
- Live Birthday Converter & Story Card Exporter: https://sol-2027.vercel.app
- Free 2027 Printable Starter PDF & iPad GoodNotes / ReMarkable Specs: https://sol-2027.vercel.app/printable
- 2027 Archival Wall Prints & Holiday Gift Guide: https://sol-2027.vercel.app/gifts
Have you replaced html2canvas or headless screenshots with pure Canvas 2D in production? What mobile Safari or font-loading quirks bit you?
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.