Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/pie-night

Pie night sign-ups: hosts start an event, guests add the pies they are bringing and watch the list fill in live. Installable, with optional push notifications.

FollowForkOpen app

Files

render-assets.mjs

73 lines · 8.3 KB · JavaScript
// Renders the share image background (src/og-bg.ts), the fallback public/og.png,
// the PNG icons, the maskable icon, and the monochrome notification badge with headless Chrome.
// Per-event share images are drawn on the server from src/og-bg.ts (see src/og.ts).
// Needs playwright-core resolvable (node_modules is gitignored) and network for Google Fonts.
// Usage: node scripts/render-assets.mjs
import { chromium } from 'playwright-core'

import { writeFileSync, readFileSync } from 'node:fs'
import { FAVICON_SVG as favicon, HERO_SVG as hero } from '../src/art.ts'
const fonts = '<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Fredoka:wght@600;700&family=Nunito:wght@800&display=swap">'

const og = (withText) => `<!doctype html><html><head>${fonts}<style>
*{box-sizing:border-box}
body{margin:0;width:1200px;height:630px;overflow:hidden;font-family:Nunito,sans-serif;color:#fff4e4;position:relative;
background:radial-gradient(ellipse 620px 420px at 330px 360px, rgba(255,170,80,.55), transparent 70%),
radial-gradient(ellipse 900px 500px at 900px 120px, rgba(255,120,60,.25), transparent 70%),
linear-gradient(160deg,#5a2614 0%,#3a170c 55%,#2a0f08 100%);}
.lights{position:absolute;left:-20px;right:-20px;top:-48px;height:110px}
.table{position:absolute;left:0;right:0;bottom:0;height:120px;background:linear-gradient(180deg,#8a4a24,#6b3519);border-top:6px solid #a85d2e}
.table::before{content:'';position:absolute;inset:0;background:repeating-linear-gradient(90deg,rgba(0,0,0,.08) 0 3px,transparent 3px 90px)}
.pie{position:absolute;left:40px;top:150px;width:600px}
.pie svg{width:600px;height:auto;filter:drop-shadow(0 22px 18px rgba(0,0,0,.45))}
.text{position:absolute;left:636px;top:84px;width:540px}
.kicker{font-weight:800;letter-spacing:.22em;text-transform:uppercase;color:#f6c177;font-size:26px;margin:0 0 6px 6px}
h1{font-family:Fredoka;font-weight:700;font-size:126px;line-height:.9;margin:0;color:#fff4e4;text-shadow:0 6px 0 #d4622a,0 14px 30px rgba(0,0,0,.4)}
.when{display:inline-flex;align-items:center;gap:14px;margin-top:24px;background:#fff4e4;color:#7a2f14;font-family:Fredoka;font-weight:600;font-size:40px;padding:10px 28px;border-radius:999px;box-shadow:0 7px 0 #d4622a,0 16px 30px rgba(0,0,0,.35)}
.when .dot{color:#e07b2a}
p{font-size:27px;margin:16px 0 0 6px;line-height:1.25;font-weight:800;color:#ffe2b8}
.where{font-size:29px;margin:20px 0 0 6px;font-weight:800;color:#fff4e4}
.pie svg > ellipse:first-child{fill:#1e0a05;opacity:.45}
.leaf{position:absolute;opacity:.9;filter:drop-shadow(0 4px 4px rgba(0,0,0,.3))}
</style></head><body>
<svg class="lights" viewBox="0 0 1240 120" preserveAspectRatio="none"><path d="M0 30 Q155 110 310 40 T620 40 T930 40 T1240 30" fill="none" stroke="#2a120a" stroke-width="3"/>
${[60,140,220,300,380,460,540,620,700,780,860,940,1020,1100,1180].map((x,i)=>{const y=30+Math.abs(Math.sin((x/310)*Math.PI))*55*(x%620<310?1:0.6);return '<circle cx="'+x+'" cy="'+(y+12)+'" r="11" fill="#ffd27a" opacity=".95"/><circle cx="'+x+'" cy="'+(y+12)+'" r="26" fill="#ffc861" opacity=".18"/>'}).join('')}</svg>
<div class="table"></div>
<svg class="leaf" style="left:40px;top:60px;transform:rotate(-20deg) scale(1.3)" viewBox="0 0 40 40" width="40" height="40"><path d="M20 2 C26 10 36 12 34 22 C32 32 24 34 20 38 C16 34 8 32 6 22 C4 12 14 10 20 2Z" fill="#e0702a"/><path d="M20 6 V38" stroke="rgba(0,0,0,.18)" stroke-width="2"/></svg><svg class="leaf" style="left:1110px;top:40px;transform:rotate(30deg) scale(1.1)" viewBox="0 0 40 40" width="40" height="40"><path d="M20 2 C26 10 36 12 34 22 C32 32 24 34 20 38 C16 34 8 32 6 22 C4 12 14 10 20 2Z" fill="#c9452c"/><path d="M20 6 V38" stroke="rgba(0,0,0,.18)" stroke-width="2"/></svg><svg class="leaf" style="left:1130px;top:575px;transform:rotate(-40deg) scale(1.4)" viewBox="0 0 40 40" width="40" height="40"><path d="M20 2 C26 10 36 12 34 22 C32 32 24 34 20 38 C16 34 8 32 6 22 C4 12 14 10 20 2Z" fill="#e6a23c"/><path d="M20 6 V38" stroke="rgba(0,0,0,.18)" stroke-width="2"/></svg><svg class="leaf" style="left:470px;top:110px;transform:rotate(15deg) scale(0.9)" viewBox="0 0 40 40" width="40" height="40"><path d="M20 2 C26 10 36 12 34 22 C32 32 24 34 20 38 C16 34 8 32 6 22 C4 12 14 10 20 2Z" fill="#d9822b"/><path d="M20 6 V38" stroke="rgba(0,0,0,.18)" stroke-width="2"/></svg><svg class="leaf" style="left:90px;top:520px;transform:rotate(50deg) scale(1.0)" viewBox="0 0 40 40" width="40" height="40"><path d="M20 2 C26 10 36 12 34 22 C32 32 24 34 20 38 C16 34 8 32 6 22 C4 12 14 10 20 2Z" fill="#b8432a"/><path d="M20 6 V38" stroke="rgba(0,0,0,.18)" stroke-width="2"/></svg><svg class="leaf" style="left:520px;top:560px;transform:rotate(-10deg) scale(0.8)" viewBox="0 0 40 40" width="40" height="40"><path d="M20 2 C26 10 36 12 34 22 C32 32 24 34 20 38 C16 34 8 32 6 22 C4 12 14 10 20 2Z" fill="#e6a23c"/><path d="M20 6 V38" stroke="rgba(0,0,0,.18)" stroke-width="2"/></svg><svg class="leaf" style="left:1150px;top:300px;transform:rotate(70deg) scale(0.9)" viewBox="0 0 40 40" width="40" height="40"><path d="M20 2 C26 10 36 12 34 22 C32 32 24 34 20 38 C16 34 8 32 6 22 C4 12 14 10 20 2Z" fill="#e0702a"/><path d="M20 6 V38" stroke="rgba(0,0,0,.18)" stroke-width="2"/></svg>
<div class="pie">${hero}</div>
${withText ? `<div class="text"><div class="kicker">You're invited</div><h1>Pie<br>Night</h1>
<p>Let us know what pie you're bringing!</p></div>` : ''}
</body></html>`

const icon = (size, pad) => `<!doctype html><html><body style="margin:0;width:${size}px;height:${size}px;background:#fff4e4;display:grid;place-items:center">
<div style="width:${size - pad * 2}px;height:${size - pad * 2}px">${favicon.replace('<svg ', '<svg width="100%" height="100%" ')}</div></body></html>`

const browser = await chromium.launch({ executablePath: process.env.CHROME || undefined })
async function shot(html, w, h, out, opts = {}) {
	const page = await browser.newPage({ viewport: { width: w, height: h } })
	await page.setContent(html, { waitUntil: 'networkidle' })
	await page.evaluate(() => document.fonts.ready)
	await page.screenshot({ path: new URL(`../${out}`, import.meta.url).pathname, ...opts })
	await page.close()
}
await shot(og(true), 1200, 630, 'public/og.png')
await shot(og(false), 1200, 630, 'scripts/og-bg.png')
const bg = readFileSync(new URL('./og-bg.png', import.meta.url))
writeFileSync(new URL('../src/og-bg.ts', import.meta.url), `// Generated by scripts/render-assets.mjs: the cozy share image background (string lights, table, pie, leaves) with no text.\nexport const OG_BG_PNG_BASE64 = '${bg.toString('base64')}'\n`)
const badge = `<!doctype html><html><body style="margin:0;width:96px;height:96px;background:transparent;display:grid;place-items:center">
<div style="width:88px;height:88px;filter:brightness(0) invert(1)">${favicon.replace('<svg ', '<svg width="100%" height="100%" ')}</div></body></html>`
await shot(badge, 96, 96, 'public/badge-96.png', { omitBackground: true })
const maskable = `<!doctype html><html><body style="margin:0;width:512px;height:512px;background:#fff4e4;display:grid;place-items:center">
<div style="width:300px;height:300px">${favicon.replace('<svg ', '<svg width="100%" height="100%" ')}</div></body></html>`
await shot(maskable, 512, 512, 'public/icon-maskable-512.png')
await shot(icon(192, 16), 192, 192, 'public/icon-192.png')
await shot(icon(512, 44), 512, 512, 'public/icon-512.png')
await shot(icon(180, 14), 180, 180, 'public/apple-touch-icon.png')
// Community listing icon (.kody/icon.png): the pie on a warm evening tile, like the share image.
const listing = `<!doctype html><html><body style="margin:0;width:512px;height:512px;background:transparent">
<div style="width:512px;height:512px;border-radius:112px;overflow:hidden;position:relative;background:radial-gradient(circle at 50% 58%, rgba(255,176,90,.6), transparent 62%),linear-gradient(160deg,#6a2d16 0%,#3a170c 60%,#2a0f08 100%);display:grid;place-items:center">
<div style="position:absolute;left:0;right:0;top:40px;display:flex;justify-content:space-around;padding:0 40px">${'<i style=\"width:22px;height:22px;border-radius:50%;background:#ffd27a;box-shadow:0 0 22px 8px rgba(255,200,97,.35)\"></i>'.repeat(5)}</div>
<div style="width:400px;height:400px;margin-top:40px;filter:drop-shadow(0 18px 16px rgba(0,0,0,.45))">${favicon.replace('<svg ', '<svg width="100%" height="100%" ')}</div></div></body></html>`
await shot(listing, 512, 512, '.kody/icon.png', { omitBackground: true })
await browser.close()
console.log('rendered')