Bedstemors Flæskesteg: animated step-by-step Danish roast (Advent of AI Slop Day 4)
This commit is contained in:
commit
7774e3aa1b
16 files changed
+2255
No files matched your search
+334
@@ -0,0 +1,334 @@
|
||||
import './style.css';
|
||||
import { STEPS, INGREDIENTS, formatIngredient, type Step } from './steps';
|
||||
import { drawScene } from './scene';
|
||||
|
||||
const STORE_KEY = 'aos-day4-flaeskesteg';
|
||||
const DEMO_SPEED = 60; // demo mode: 1 real second = 1 recipe minute
|
||||
|
||||
interface Saved {
|
||||
servings: number;
|
||||
demo: boolean;
|
||||
current: number;
|
||||
done: boolean[];
|
||||
}
|
||||
|
||||
function loadSaved(): Saved {
|
||||
const fallback: Saved = {
|
||||
servings: 4,
|
||||
demo: true,
|
||||
current: 0,
|
||||
done: STEPS.map(() => false),
|
||||
};
|
||||
try {
|
||||
const raw = localStorage.getItem(STORE_KEY);
|
||||
if (!raw) return fallback;
|
||||
const parsed = JSON.parse(raw) as Partial<Saved>;
|
||||
return {
|
||||
servings: typeof parsed.servings === 'number' ? Math.min(10, Math.max(1, Math.round(parsed.servings))) : fallback.servings,
|
||||
demo: typeof parsed.demo === 'boolean' ? parsed.demo : fallback.demo,
|
||||
current: typeof parsed.current === 'number' && parsed.current >= 0 && parsed.current < STEPS.length ? parsed.current : 0,
|
||||
done: Array.isArray(parsed.done) && parsed.done.length === STEPS.length
|
||||
? parsed.done.map((v) => v === true) : fallback.done,
|
||||
};
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
const state = loadSaved();
|
||||
|
||||
// timer: at most one running countdown, tied to a step index
|
||||
const timer = { step: -1, remaining: 0, running: false, finished: false };
|
||||
|
||||
function persist(): void {
|
||||
localStorage.setItem(STORE_KEY, JSON.stringify({
|
||||
servings: state.servings, demo: state.demo, current: state.current, done: state.done,
|
||||
}));
|
||||
}
|
||||
|
||||
const app = document.getElementById('app');
|
||||
if (!app) throw new Error('missing #app');
|
||||
|
||||
app.innerHTML = `
|
||||
<header>
|
||||
<h1>Bedstemors Flæskesteg</h1>
|
||||
<p class="sub">Grandma's roast pork with crackling, rødkål and brunede kartofler — ten steps,
|
||||
one animated kitchen, and timers that actually run. The crackling must <em>crack</em>, not chew.</p>
|
||||
</header>
|
||||
|
||||
<div id="controls">
|
||||
<label class="ctl">Servings
|
||||
<input id="servings" type="range" min="1" max="10" step="1" />
|
||||
<span id="servings-val"></span>
|
||||
</label>
|
||||
<label class="ctl toggle">Demo timers (60×)
|
||||
<input id="demo" type="checkbox" />
|
||||
</label>
|
||||
<button id="reset" class="ghost">Reset kitchen</button>
|
||||
<div id="progress-wrap">
|
||||
<div id="progress-bar"></div>
|
||||
<span id="progress-text"></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="stage">
|
||||
<section id="left">
|
||||
<canvas id="scene" width="640" height="440"></canvas>
|
||||
<div id="now">
|
||||
<div id="now-title"></div>
|
||||
<div id="timer-box">
|
||||
<span id="timer-display">--:--</span>
|
||||
<button id="timer-btn">Start timer</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="ingredients">
|
||||
<h2>Indkøb — shopping list</h2>
|
||||
<ul id="ing-list"></ul>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section id="steps-col">
|
||||
<ol id="steps"></ol>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<footer>Bedstemors recipe, more or less. Which means it is yours now. <span class="da">Velbekomme!</span></footer>
|
||||
|
||||
<div id="win" hidden>
|
||||
<div id="win-card">
|
||||
<h2>Middagen er klar!</h2>
|
||||
<p>Ten steps, one roast, zero shortcuts. The crackling cracked — Grandma would nod.</p>
|
||||
<button id="win-close">Back to the kitchen</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
const canvas = document.getElementById('scene') as HTMLCanvasElement;
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) throw new Error('canvas 2d unavailable');
|
||||
const DPR = Math.min(2, window.devicePixelRatio || 1);
|
||||
canvas.width = 640 * DPR;
|
||||
canvas.height = 440 * DPR;
|
||||
|
||||
const servingsInput = document.getElementById('servings') as HTMLInputElement;
|
||||
const servingsVal = document.getElementById('servings-val')!;
|
||||
const demoInput = document.getElementById('demo') as HTMLInputElement;
|
||||
const resetBtn = document.getElementById('reset')!;
|
||||
const progressBar = document.getElementById('progress-bar')!;
|
||||
const progressText = document.getElementById('progress-text')!;
|
||||
const nowTitle = document.getElementById('now-title')!;
|
||||
const timerDisplay = document.getElementById('timer-display')!;
|
||||
const timerBtn = document.getElementById('timer-btn') as HTMLButtonElement;
|
||||
const timerBox = document.getElementById('timer-box')!;
|
||||
const ingList = document.getElementById('ing-list')!;
|
||||
const stepsOl = document.getElementById('steps') as HTMLOListElement;
|
||||
const win = document.getElementById('win')!;
|
||||
const winClose = document.getElementById('win-close')!;
|
||||
|
||||
const stepEls: { li: HTMLLIElement; check: HTMLInputElement }[] = [];
|
||||
|
||||
function fmt(sec: number): string {
|
||||
const s = Math.max(0, Math.ceil(sec));
|
||||
const m = Math.floor(s / 60);
|
||||
const r = s % 60;
|
||||
return `${String(m).padStart(2, '0')}:${String(r).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
function stepCard(step: Step, i: number): HTMLLIElement {
|
||||
const li = document.createElement('li');
|
||||
li.className = 'step';
|
||||
const head = document.createElement('div');
|
||||
head.className = 'step-head';
|
||||
const num = document.createElement('span');
|
||||
num.className = 'num';
|
||||
num.textContent = String(i + 1);
|
||||
const ttl = document.createElement('div');
|
||||
ttl.className = 'ttl';
|
||||
const strong = document.createElement('strong');
|
||||
strong.textContent = step.title;
|
||||
const da = document.createElement('span');
|
||||
da.className = 'da';
|
||||
da.textContent = step.danish;
|
||||
ttl.append(strong, da);
|
||||
head.append(num, ttl);
|
||||
if (step.minutes > 0) {
|
||||
const badge = document.createElement('span');
|
||||
badge.className = 'mins';
|
||||
badge.textContent = step.minutes >= 60 ? `${(step.minutes / 60).toFixed(step.minutes % 60 === 0 ? 0 : 1)} h` : `${step.minutes} min`;
|
||||
head.append(badge);
|
||||
}
|
||||
const check = document.createElement('input');
|
||||
check.type = 'checkbox';
|
||||
check.className = 'done';
|
||||
check.checked = state.done[i] === true;
|
||||
check.title = 'Mark step done';
|
||||
head.append(check);
|
||||
|
||||
const body = document.createElement('p');
|
||||
body.className = 'step-text';
|
||||
body.textContent = step.text;
|
||||
|
||||
li.append(head, body);
|
||||
li.addEventListener('click', (e) => {
|
||||
if ((e.target as HTMLElement).closest('.done')) return;
|
||||
selectStep(i);
|
||||
});
|
||||
check.addEventListener('change', () => {
|
||||
state.done[i] = check.checked;
|
||||
li.classList.toggle('is-done', check.checked);
|
||||
if (check.checked && state.done.every(Boolean)) showWin();
|
||||
persist();
|
||||
updateProgress();
|
||||
});
|
||||
li.classList.toggle('is-done', check.checked);
|
||||
return li;
|
||||
}
|
||||
|
||||
STEPS.forEach((step, i) => {
|
||||
const li = stepCard(step, i);
|
||||
const check = li.querySelector('.done') as HTMLInputElement;
|
||||
stepEls.push({ li, check });
|
||||
stepsOl.append(li);
|
||||
});
|
||||
|
||||
function selectStep(i: number): void {
|
||||
state.current = i;
|
||||
stepEls.forEach((el, j) => el.li.classList.toggle('active', j === i));
|
||||
stepEls[i]!.li.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
||||
updateNow();
|
||||
persist();
|
||||
}
|
||||
|
||||
function updateNow(): void {
|
||||
const step = STEPS[state.current]!;
|
||||
nowTitle.innerHTML = `<strong>${step.title}</strong> <span class="da">${step.danish}</span>`;
|
||||
if (step.minutes === 0) {
|
||||
timerBox.classList.add('no-timer');
|
||||
timerDisplay.textContent = 'no timer';
|
||||
timerBtn.disabled = true;
|
||||
timerBtn.textContent = 'eyeball it';
|
||||
} else {
|
||||
timerBox.classList.remove('no-timer');
|
||||
if (timer.step !== state.current) {
|
||||
timer.step = state.current;
|
||||
timer.remaining = step.minutes * 60;
|
||||
timer.running = false;
|
||||
timer.finished = false;
|
||||
}
|
||||
timerBtn.disabled = false;
|
||||
updateTimerUi();
|
||||
}
|
||||
}
|
||||
|
||||
function updateTimerUi(): void {
|
||||
const step = STEPS[state.current]!;
|
||||
if (step.minutes === 0) return;
|
||||
if (timer.finished) {
|
||||
timerDisplay.textContent = 'færdig!';
|
||||
timerBtn.textContent = 'Restart';
|
||||
} else {
|
||||
timerDisplay.textContent = fmt(timer.remaining);
|
||||
timerBtn.textContent = timer.running ? 'Pause' : 'Start timer';
|
||||
}
|
||||
timerDisplay.classList.toggle('running', timer.running);
|
||||
timerDisplay.classList.toggle('demo', state.demo);
|
||||
}
|
||||
|
||||
timerBtn.addEventListener('click', () => {
|
||||
const step = STEPS[state.current]!;
|
||||
if (step.minutes === 0) return;
|
||||
if (timer.finished) {
|
||||
timer.remaining = step.minutes * 60;
|
||||
timer.finished = false;
|
||||
}
|
||||
timer.running = !timer.running;
|
||||
updateTimerUi();
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
if (!timer.running) return;
|
||||
timer.remaining -= 0.2 * (state.demo ? DEMO_SPEED : 1);
|
||||
if (timer.remaining <= 0) {
|
||||
timer.remaining = 0;
|
||||
timer.running = false;
|
||||
timer.finished = true;
|
||||
stepEls[timer.step]?.li.classList.add('ding');
|
||||
setTimeout(() => stepEls[timer.step]?.li.classList.remove('ding'), 4000);
|
||||
}
|
||||
updateTimerUi();
|
||||
}, 200);
|
||||
|
||||
function updateProgress(): void {
|
||||
const n = state.done.filter(Boolean).length;
|
||||
progressBar.style.width = `${(n / STEPS.length) * 100}%`;
|
||||
progressText.textContent = `${n} / ${STEPS.length} steps`;
|
||||
}
|
||||
|
||||
function updateIngredients(): void {
|
||||
servingsVal.textContent = String(state.servings);
|
||||
ingList.replaceChildren(...INGREDIENTS.map((ing) => {
|
||||
const li = document.createElement('li');
|
||||
li.textContent = formatIngredient(ing, state.servings);
|
||||
const tag = document.createElement('span');
|
||||
tag.className = 'ing-step';
|
||||
tag.textContent = `step ${ing.step}`;
|
||||
li.append(tag);
|
||||
return li;
|
||||
}));
|
||||
}
|
||||
|
||||
servingsInput.value = String(state.servings);
|
||||
demoInput.checked = state.demo;
|
||||
servingsInput.addEventListener('input', () => {
|
||||
state.servings = Number(servingsInput.value);
|
||||
updateIngredients();
|
||||
persist();
|
||||
});
|
||||
demoInput.addEventListener('change', () => {
|
||||
state.demo = demoInput.checked;
|
||||
updateTimerUi();
|
||||
persist();
|
||||
});
|
||||
resetBtn.addEventListener('click', () => {
|
||||
localStorage.removeItem(STORE_KEY);
|
||||
location.reload();
|
||||
});
|
||||
|
||||
winClose.addEventListener('click', () => { win.hidden = true; });
|
||||
|
||||
function showWin(): void {
|
||||
win.hidden = false;
|
||||
const colors = ['#b8232f', '#d9a441', '#5a8f5a', '#f6ecd6', '#6d3560'];
|
||||
for (let i = 0; i < 70; i++) {
|
||||
const c = document.createElement('i');
|
||||
c.className = 'confetti';
|
||||
c.style.left = `${Math.random() * 100}%`;
|
||||
c.style.background = colors[i % colors.length]!;
|
||||
c.style.animationDuration = `${2 + Math.random() * 2.5}s`;
|
||||
c.style.animationDelay = `${Math.random() * 1.2}s`;
|
||||
c.style.transform = `rotate(${Math.random() * 360}deg)`;
|
||||
win.append(c);
|
||||
setTimeout(() => c.remove(), 6000);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'ArrowRight') selectStep(Math.min(STEPS.length - 1, state.current + 1));
|
||||
if (e.key === 'ArrowLeft') selectStep(Math.max(0, state.current - 1));
|
||||
});
|
||||
|
||||
// render loop
|
||||
let start = performance.now();
|
||||
function frame(now: number): void {
|
||||
const t = (now - start) / 1000;
|
||||
ctx!.save();
|
||||
ctx!.scale(DPR, DPR);
|
||||
drawScene(ctx!, 640, 440, STEPS[state.current]!.phase, t, timer.running);
|
||||
ctx!.restore();
|
||||
requestAnimationFrame(frame);
|
||||
}
|
||||
|
||||
selectStep(state.current);
|
||||
updateIngredients();
|
||||
updateProgress();
|
||||
requestAnimationFrame(frame);
|
||||
Reference in new issue
Block a user