// Shelf Pop: game screen (crisp canvas, drag or tap to move goods, pops, slide-forward rows, hint hand, sound, daily + // levels + archive, share line + card, Fun Pass). Rules: core.js. (c) 2026 CyberMax. All rights reserved. import { GOODS, makeGame, moves as allMoves, fmt, shareText as coreShare, specOf } from './core.js'; import { dayNumber, dateOf, count, store, share, intents, download, canvasBlob, pwa, themeToggle, rr } from './cmx.js'; import * as pass from './funpass.js'; const GAME = 'shelfpop', PATH = '/games/shelfpop/'; const $ = (id) => document.getElementById(id); const cv = $('c'), ctx = cv.getContext('2d'), TAU = Math.PI * 2; const W = 360, H = 540, TOP = 70; const LOOKS = { market: { name: 'Market', tier: 'free', top: '#FFF1D6', bot: '#FFE0EC', wall: 'rgba(255,255,255,.55)', wood: '#E9A86B', wood2: '#C9824A', ink: '#3A2A1E', acc: '#FF6A8A' }, mint: { name: 'Mint', tier: 'pass', top: '#DDFBEF', bot: '#E9F6FF', wall: 'rgba(255,255,255,.55)', wood: '#9ED9C0', wood2: '#5FB596', ink: '#173A33', acc: '#FF7A59' }, peach: { name: 'Peach', tier: 'pass', top: '#FFE6DA', bot: '#FFF8D9', wall: 'rgba(255,255,255,.55)', wood: '#F2B48C', wood2: '#D88A5E', ink: '#3E2418', acc: '#E2477A' }, }; const todayIso = new Date().toISOString().slice(0, 10), todayNum = dayNumber(todayIso); pass.init({ game: GAME, name: 'Shelf Pop', perks: ['Every past Daily Shelf in the archive', 'Mint and Peach shop themes'], free: 'The Daily Shelf and every level stay free for everyone.', extra: 'The Fun Pass adds every past Daily Shelf and 2 shop themes.', preview: ['past daily shelves', '2 themes'] }); const canUse = (s) => s.tier === 'free' || pass.isPro(); let look = LOOKS[store('shelfpop:look')] && canUse(LOOKS[store('shelfpop:look')]) ? store('shelfpop:look') : 'market'; let muted = !!store('shelfpop:mute'); let level = Math.max(1, store('shelfpop:level') || 1); let mode = store('shelfpop:played') ? { kind: 'daily', date: todayIso } : { kind: 'level', n: level }; // a first visit starts on the tiny level 1 let game, last = performance.now(), endShown = false, idle = 0, fx = [], texts = [], anims = new Map(), drag = null, sel = null, shake = 0, autoBot = false, botT = 0; const install = pwa(GAME, $('install')); themeToggle($('theme')); // ------------------------------------------------------------------ sound + haptics let ac; function tone(f, d = 0.08, type = 'sine', vol = 0.07, to = 1, delay = 0) { if (muted) return; try { ac = ac || new (window.AudioContext || window.webkitAudioContext)(); const s = ac.currentTime + delay, o = ac.createOscillator(), v = ac.createGain(); o.type = type; o.frequency.setValueAtTime(f, s); o.frequency.exponentialRampToValueAtTime(Math.max(40, f * to), s + d); v.gain.setValueAtTime(0.0001, s); v.gain.exponentialRampToValueAtTime(vol, s + 0.008); v.gain.exponentialRampToValueAtTime(0.0001, s + d); o.connect(v); v.connect(ac.destination); o.start(s); o.stop(s + d + 0.03); } catch { /* no audio */ } } const NOTES = [523, 587, 659, 784, 880, 1047]; const buzz = (ms) => { try { if (navigator.vibrate) navigator.vibrate(ms); } catch { /* */ } }; const setMute = () => { $('mute').textContent = muted ? '๐Ÿ”‡' : '๐Ÿ”Š'; }; setMute(); $('mute').onclick = () => { muted = !muted; store('shelfpop:mute', muted); setMute(); }; // ------------------------------------------------------------------ crisp canvas let scale = 1; function fit() { const r = cv.getBoundingClientRect(), dpr = Math.min(3, window.devicePixelRatio || 1); const w = Math.max(1, Math.round(r.width * dpr)), h = Math.max(1, Math.round(r.height * dpr)); if (cv.width !== w || cv.height !== h) { cv.width = w; cv.height = h; } scale = cv.width / W; } new ResizeObserver(fit).observe(cv); fit(); // ------------------------------------------------------------------ layout: 2 columns of shelves, each with 3 front slots function layout(n, depth) { const rows = Math.ceil(n / 2), ch = Math.min(96 + 30 * Math.min(2, depth - 1), (H - TOP - 10) / rows), cw = 168, gap = (H - TOP - 10 - ch * rows) / 2; return Array.from({ length: n }, (_, i) => { const col = i % 2, row = Math.floor(i / 2), lone = n % 2 === 1 && i === n - 1; const x = lone ? (W - cw) / 2 : 8 + col * (cw + 8), y = TOP + gap + row * ch; return { x, y, w: cw, h: ch, base: y + ch - 22, slot: (k) => x + 30 + k * 54 }; }); } let L = []; // ------------------------------------------------------------------ goods (code-drawn, bright, readable at a glance) function good(g, k, x, y, s = 1, a = 1) { const G = GOODS[k]; g.save(); g.translate(x, y); g.scale(s, s); g.globalAlpha *= a; const ink = 'rgba(40,25,20,.35)'; g.lineWidth = 2; g.strokeStyle = ink; const circ = (cx, cy, r, c) => { g.fillStyle = c; g.beginPath(); g.arc(cx, cy, r, 0, TAU); g.fill(); g.stroke(); }; const shine = (cx, cy, r) => { g.fillStyle = 'rgba(255,255,255,.7)'; g.beginPath(); g.ellipse(cx, cy, r, r * 0.55, -0.6, 0, TAU); g.fill(); }; switch (G.id) { case 'apple': circ(0, 2, 17, G.c); g.fillStyle = '#7A4A22'; g.fillRect(-1.5, -20, 3, 8); g.fillStyle = '#4CCB7A'; g.beginPath(); g.ellipse(7, -16, 7, 3.5, -0.5, 0, TAU); g.fill(); shine(-7, -5, 5); break; case 'lemon': g.fillStyle = G.c; g.beginPath(); g.ellipse(0, 0, 20, 14, 0, 0, TAU); g.fill(); g.stroke(); g.beginPath(); g.arc(20, 0, 3, 0, TAU); g.arc(-20, 0, 3, 0, TAU); g.fill(); shine(-6, -5, 5); break; case 'grape': for (const [dx, dy] of [[-8, -8], [8, -8], [0, -8], [-4, 0], [4, 0], [-8, 0], [8, 0], [0, 8], [-4, 8], [4, 8], [0, 15]]) circ(dx, dy, 6, G.c); g.fillStyle = '#4CCB7A'; g.beginPath(); g.ellipse(4, -17, 7, 3.5, 0.3, 0, TAU); g.fill(); break; case 'carrot': g.fillStyle = G.c; g.beginPath(); g.moveTo(-9, -10); g.lineTo(9, -10); g.lineTo(0, 20); g.closePath(); g.fill(); g.stroke(); g.fillStyle = '#4CCB7A'; for (const a of [-0.5, 0, 0.5]) { g.save(); g.translate(0, -11); g.rotate(a); g.beginPath(); g.ellipse(0, -7, 3, 8, 0, 0, TAU); g.fill(); g.restore(); } break; case 'donut': g.fillStyle = '#E8B070'; g.beginPath(); g.arc(0, 0, 19, 0, TAU); g.fill(); g.stroke(); g.fillStyle = G.c; g.beginPath(); g.arc(0, -1, 16, 0, TAU); g.fill(); g.fillStyle = '#FFF5E8'; g.beginPath(); g.arc(0, 0, 6, 0, TAU); g.fill(); g.stroke(); for (const [dx, dy, c] of [[-9, -6, '#4D6BFF'], [8, -8, '#FFD23F'], [10, 4, '#4CCB7A'], [-8, 7, '#FFFFFF']]) { g.fillStyle = c; g.fillRect(dx - 2, dy - 1, 4, 2); } break; case 'milk': g.fillStyle = '#FFFFFF'; g.beginPath(); g.moveTo(-12, -8); g.lineTo(0, -20); g.lineTo(12, -8); g.lineTo(12, 18); g.lineTo(-12, 18); g.closePath(); g.fill(); g.stroke(); g.fillStyle = G.c; g.fillRect(-12, 0, 24, 10); break; case 'melon': g.fillStyle = '#4CCB7A'; g.beginPath(); g.arc(0, -6, 21, 0, Math.PI); g.closePath(); g.fill(); g.stroke(); g.fillStyle = '#FF5A6E'; g.beginPath(); g.arc(0, -6, 16, 0, Math.PI); g.closePath(); g.fill(); g.fillStyle = '#2B2340'; for (const dx of [-8, 0, 8]) { g.beginPath(); g.ellipse(dx, 0, 1.6, 2.4, 0, 0, TAU); g.fill(); } break; case 'cookie': circ(0, 0, 18, G.c); g.fillStyle = '#5A3420'; for (const [dx, dy] of [[-7, -6], [6, -8], [8, 5], [-5, 7], [0, 0]]) { g.beginPath(); g.arc(dx, dy, 2.6, 0, TAU); g.fill(); } break; case 'egg': g.fillStyle = '#FFF9EC'; g.beginPath(); g.ellipse(0, 1, 14, 18, 0, 0, TAU); g.fill(); g.stroke(); shine(-5, -7, 4); break; case 'berry': circ(0, 2, 16, G.c); g.fillStyle = '#2E3F9E'; g.beginPath(); for (let i = 0; i < 5; i++) { const a = -Math.PI / 2 + i * TAU / 5; g.lineTo(Math.cos(a) * 6, -10 + Math.sin(a) * 6); g.lineTo(Math.cos(a + 0.6) * 3, -10 + Math.sin(a + 0.6) * 3); } g.fill(); shine(-6, -3, 4); break; case 'cheese': g.fillStyle = G.c; g.beginPath(); g.moveTo(-19, 14); g.lineTo(19, 14); g.lineTo(19, -6); g.lineTo(-19, 4); g.closePath(); g.fill(); g.stroke(); g.fillStyle = '#E09A10'; for (const [dx, dy, r] of [[-8, 9, 3], [6, 6, 4], [12, 11, 2]]) { g.beginPath(); g.arc(dx, dy, r, 0, TAU); g.fill(); } break; case 'mint': g.fillStyle = G.c; g.beginPath(); g.moveTo(-12, 0); g.lineTo(-22, -8); g.lineTo(-22, 8); g.closePath(); g.moveTo(12, 0); g.lineTo(22, -8); g.lineTo(22, 8); g.closePath(); g.fill(); circ(0, 0, 13, '#FFFFFF'); g.strokeStyle = G.c; g.lineWidth = 4; g.beginPath(); g.arc(0, 0, 7, 0, 4.5); g.stroke(); break; default: circ(0, 0, 16, G.c); } g.restore(); } // ------------------------------------------------------------------ modes + stats (one small menu) const seedOf = (m) => (m.kind === 'daily' ? `daily:${m.date}` : `level:${m.n}`); const dayKey = (m) => `shelfpop:day:${m.date}`; const dayStats = (m) => store(dayKey(m)) || { best: 0, tries: 0, done: false }; function renderModes() { const el = $('modes'); el.innerHTML = ''; const items = [ { t: `Daily Shelf #${todayNum}`, on: mode.kind === 'daily' && mode.date === todayIso, go: () => setMode({ kind: 'daily', date: todayIso }) }, { t: `Level ${level}`, on: mode.kind === 'level', go: () => setMode({ kind: 'level', n: level }) }, { t: 'Archive', on: mode.kind === 'daily' && mode.date !== todayIso, lock: !pass.isPro(), go: archive }, ]; for (const it of items) { const b = document.createElement('button'); b.className = 'chip'; b.type = 'button'; b.setAttribute('aria-pressed', String(it.on)); b.innerHTML = `${it.t}${it.lock ? ' ๐Ÿ”’' : ''}`; b.onclick = it.go; el.appendChild(b); } } function archive() { if (!pass.isPro()) { pass.open('archive'); return; } const n = todayNum - 1, k = Number(prompt(`Play a past Daily Shelf: enter a number from 1 to ${n}`, String(n))); if (k >= 1 && k <= n) setMode({ kind: 'daily', date: dateOf(k) }); } function setMode(m) { mode = m; $('menu').open = false; renderModes(); start(); } const label = () => (mode.kind === 'daily' ? `#${dayNumber(mode.date)}` : `level ${mode.n}`); function hud() { $('num').textContent = label(); const s = mode.kind === 'daily' ? dayStats(mode) : { best: store(`shelfpop:lv:${mode.n}`) || 0 }; $('best').textContent = (s.best || 0).toLocaleString('en-US'); $('lvl').textContent = level; } const shopVis = (on) => { const b = $('shop-btn'); if (b) b.hidden = !on || pass.noShop(); }; // ------------------------------------------------------------------ run function start() { if (mode.kind === 'level') mode.n = level; game = makeGame(seedOf(mode), mode.kind === 'daily' ? 'daily' : mode.n); L = layout(game.board.length, game.sp.depth); fx = []; texts = []; anims = new Map(); drag = sel = null; endShown = false; idle = 0; shake = 0; last = performance.now(); for (let s = 0; s < game.board.length; s++) for (let k = 0; k < 3; k++) anims.set(`${s}:${k}`, { kind: 'in', t: -0.03 * (s * 3 + k) }); $('over').hidden = true; shopVis(false); hud(); count(GAME, 'play', mode.kind === 'daily' ? (mode.date === todayIso ? 'daily' : 'archive') : 'level'); } function finish() { endShown = true; const done = game.state === 'done'; if (done) { tone(784, 0.15, 'triangle', 0.08); tone(1047, 0.25, 'triangle', 0.08, 1, 0.12); tone(1319, 0.3, 'triangle', 0.07, 1, 0.24); } else { tone(300, 0.35, 'sawtooth', 0.05, 0.4); buzz([30, 40, 30]); shake = 8; } let newBest = false; if (mode.kind === 'daily') { const s = dayStats(mode); s.tries = (s.tries || 0) + 1; if (game.score > (s.best || 0)) { s.best = game.score; newBest = game.score > 0; } s.done = s.done || done; store(dayKey(mode), s); store('shelfpop:played', true); } else { const k = `shelfpop:lv:${mode.n}`; if (game.score > (store(k) || 0)) { store(k, game.score); newBest = true; } if (done && mode.n === level) { level++; store('shelfpop:level', level); } } hud(); renderModes(); setTimeout(() => showOver(newBest, done), done ? 600 : 900); if (done) install.offer(); } function play(a, i, c, j = -1, fromXY = null) { const ev = game.play(a, i, c, j); if (!ev) { tone(200, 0.06, 'sine', 0.03); return false; } idle = 0; sel = null; const [ts, tj] = ev.to, p = L[c]; anims.set(`${ts}:${tj}`, { kind: 'fly', t: 0, fx: fromXY ? fromXY[0] : L[a].slot(i), fy: fromXY ? fromXY[1] : L[a].base - 24 }); tone(440 + (ev.item % 6) * 40, 0.05, 'triangle', 0.05, 1.2); for (const pp of [ev.pop, ...(ev.pop2 || [])].filter(Boolean)) { const q = L[pp.shelf], col = GOODS[pp.item].c; for (let k = 0; k < 3; k++) burst(q.slot(k), q.base - 24, col, 12); texts.push({ x: q.x + q.w / 2, y: q.base - 60, t: `+${pp.pts}`, big: pp.combo >= 2 ? `${pp.combo}x combo!` : '', life: 1 }); tone(NOTES[Math.min(5, pp.combo - 1)], 0.14, 'triangle', 0.09, 1.02, 0.04); tone(NOTES[Math.min(5, pp.combo)] * 2, 0.12, 'sine', 0.05, 1, 0.1); buzz(pp.combo >= 3 ? 25 : 12); shake = Math.max(shake, pp.combo >= 3 ? 3 : 1.5); } for (const s of new Set(ev.slid)) for (let k = 0; k < 3; k++) anims.set(`${s}:${k}`, { kind: 'in', t: 0 }); if (p && !ev.pop) { /* placed */ } if (game.state !== 'run' && !endShown) finish(); return true; } function burst(x, y, c, n) { for (let i = 0; i < n; i++) { const a = Math.random() * TAU, v = 60 + Math.random() * 160; fx.push({ x, y, vx: Math.cos(a) * v, vy: Math.sin(a) * v - 70, life: 1, r: 2 + Math.random() * 3, c: i % 4 === 0 ? '#FFFFFF' : c }); } } // a good next move for the hint hand (cheap: finish a triple, else join a pair, else anything) function hintMove() { const b = game.board; let best = null, bs = -1; for (const m of allMoves(b)) { const it = b[m[0]][0][m[1]], fc = b[m[2]][0] || [], same = fc.filter((x) => x === it).length, srcSame = b[m[0]][0].filter((x) => x === it).length; const sc = same * 10 - srcSame * 4 + (fc.every((x) => x == null) ? -3 : 0); if (sc > bs) { bs = sc; best = m; } } return best; } // ------------------------------------------------------------------ loop function frame(now) { requestAnimationFrame(frame); const dt = Math.min(0.1, (now - last) / 1000); last = now; if (game) { const was = game.state; game.tick(dt); if (was === 'run' && game.state !== 'run' && !endShown) finish(); if (game.state === 'run') { idle += dt; if (autoBot && (botT += dt) > 0.35) { botT = 0; const m = (game.solution && game.solution[game.moves]) || hintMove(); if (m) play(m[0], m[1], m[2]); } } } for (const a of anims.values()) a.t += dt; for (const [k, a] of anims) if (a.t > 0.5) anims.delete(k); for (const p of fx) { p.x += p.vx * dt; p.y += p.vy * dt; p.vy += 600 * dt; p.life -= dt * 1.5; } fx = fx.filter((p) => p.life > 0); for (const t of texts) { t.y -= 36 * dt; t.life -= dt * 0.9; } texts = texts.filter((t) => t.life > 0); shake *= Math.pow(0.02, dt); if (shake < 0.2) shake = 0; draw(now); } const ease = (t) => 1 - Math.pow(1 - Math.min(1, Math.max(0, t)), 3); function draw(now) { const S = LOOKS[look]; ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, cv.width, cv.height); ctx.setTransform(scale, 0, 0, scale, 0, 0); const gr = ctx.createLinearGradient(0, 0, 0, H); gr.addColorStop(0, S.top); gr.addColorStop(1, S.bot); ctx.fillStyle = gr; ctx.fillRect(0, 0, W, H); ctx.fillStyle = 'rgba(255,255,255,.35)'; for (let i = 0; i < 9; i++) ctx.fillRect(0, 70 + i * 54, W, 2); if (shake) ctx.translate((Math.random() - 0.5) * shake, (Math.random() - 0.5) * shake); if (!game) return; const hm = game.state === 'run' && (idle > 5 || (mode.kind === 'level' && mode.n === 1 && game.moves < 4 && idle > 0.6)) ? hintMove() : null; game.board.forEach((rows, s) => { const p = L[s]; ctx.fillStyle = S.wall; rr(ctx, p.x, p.y + 4, p.w, p.h - 8, 18); ctx.fill(); if (sel && sel[0] !== s && (!rows[0] || rows[0].includes(null))) { ctx.strokeStyle = S.acc; ctx.lineWidth = 2; ctx.setLineDash([6, 6]); rr(ctx, p.x + 2, p.y + 6, p.w - 4, p.h - 12, 16); ctx.stroke(); ctx.setLineDash([]); } // back rows: smaller and softer, peeking above the front row for (let d = Math.min(rows.length - 1, 2); d >= 1; d--) { const y = p.base - 24 - (d === 1 ? 42 : 68), sc = d === 1 ? 0.62 : 0.45, al = d === 1 ? 0.6 : 0.38; rows[d].forEach((it, k) => { if (it != null) good(ctx, it, p.slot(k), y, sc, al); }); } // the plank ctx.fillStyle = S.wood; rr(ctx, p.x + 4, p.base, p.w - 8, 12, 5); ctx.fill(); ctx.fillStyle = S.wood2; ctx.fillRect(p.x + 8, p.base + 10, p.w - 16, 3); if (rows.length > 1) { ctx.fillStyle = S.ink; ctx.globalAlpha = 0.45; ctx.font = '800 11px system-ui,sans-serif'; ctx.textAlign = 'right'; ctx.fillText(`+${rows.length - 1}`, p.x + p.w - 10, p.y + 20); ctx.globalAlpha = 1; } const fr = rows[0] || []; fr.forEach((it, k) => { if (it == null) return; if (drag && drag.a === s && drag.i === k) return; let x = p.slot(k), y = p.base - 24, sc = 1; const an = anims.get(`${s}:${k}`); if (an && an.kind === 'in') { const e = ease(an.t / 0.28); sc = an.t < 0 ? 0 : 0.4 + 0.6 * e + Math.sin(Math.min(1, an.t / 0.28) * Math.PI) * 0.12; } if (an && an.kind === 'fly') { const e = ease(an.t / 0.18); x = an.fx + (x - an.fx) * e; y = an.fy + (y - an.fy) * e; } const lifted = sel && sel[0] === s && sel[1] === k; if (hm && hm[0] === s && hm[1] === k) { ctx.strokeStyle = S.acc; ctx.lineWidth = 3; ctx.globalAlpha = 0.5 + 0.5 * Math.sin(now / 160); ctx.beginPath(); ctx.arc(x, y, 25, 0, TAU); ctx.stroke(); ctx.globalAlpha = 1; } ctx.fillStyle = 'rgba(60,30,10,.12)'; ctx.beginPath(); ctx.ellipse(x, p.base - 1, 16, 3.5, 0, 0, TAU); ctx.fill(); good(ctx, it, x, y - (lifted ? 8 : 0), sc * (lifted ? 1.24 : 1.12)); }); }); // hint hand (moves from the item to the target shelf) if (hm) { const a = L[hm[0]], c = L[hm[2]], u = ((now / 1300) % 1), e = ease(u * 1.4); const x = a.slot(hm[1]) + (c.x + c.w / 2 - a.slot(hm[1])) * e, y = a.base - 10 + (c.base - 10 - (a.base - 10)) * e; ctx.globalAlpha = u > 0.85 ? (1 - u) / 0.15 : 1; ctx.font = '30px system-ui,sans-serif'; ctx.textAlign = 'center'; ctx.fillText('๐Ÿ‘†', x + 6, y + 18); ctx.globalAlpha = 1; } if (drag) good(ctx, drag.it, drag.x, drag.y - 14, 1.3); for (const q of fx) { ctx.globalAlpha = Math.max(0, Math.min(1, q.life * 1.3)); ctx.fillStyle = q.c; ctx.beginPath(); ctx.arc(q.x, q.y, q.r, 0, TAU); ctx.fill(); } ctx.globalAlpha = 1; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; for (const t of texts) { ctx.globalAlpha = Math.min(1, t.life * 1.5); ctx.font = '900 22px system-ui,-apple-system,sans-serif'; ctx.lineWidth = 5; ctx.strokeStyle = '#FFFFFF'; ctx.fillStyle = S.ink; ctx.strokeText(t.t, t.x, t.y); ctx.fillText(t.t, t.x, t.y); if (t.big) { ctx.font = '900 16px system-ui,sans-serif'; ctx.fillStyle = S.acc; ctx.strokeText(t.big, t.x, t.y - 22); ctx.fillText(t.big, t.x, t.y - 22); } } ctx.globalAlpha = 1; // HUD: score chip (left), progress dots (middle), clock (right) ctx.fillStyle = 'rgba(255,255,255,.9)'; rr(ctx, 10, 12, 112, 40, 20); ctx.fill(); ctx.fillStyle = S.ink; ctx.textAlign = 'left'; ctx.font = '900 22px system-ui,-apple-system,sans-serif'; ctx.fillText(game.score.toLocaleString('en-US'), 24, 33); const left = game.left(), low = left <= 10 && game.state === 'run'; ctx.fillStyle = 'rgba(255,255,255,.9)'; rr(ctx, W - 82, 12, 72, 40, 20); ctx.fill(); ctx.fillStyle = low ? '#E2477A' : S.ink; ctx.textAlign = 'center'; ctx.font = '900 20px system-ui,sans-serif'; ctx.fillText(fmt(Math.ceil(left)), W - 46, 33); const prog = game.popped / game.total; ctx.fillStyle = 'rgba(255,255,255,.9)'; rr(ctx, 132, 26, 136, 12, 6); ctx.fill(); ctx.fillStyle = S.acc; rr(ctx, 132, 26, Math.max(12, 136 * prog), 12, 6); ctx.fill(); if (game.moves === 0 && game.state === 'run' && !(mode.kind === 'level' && mode.n === 1)) { ctx.globalAlpha = Math.max(0, 1 - game.t / 3); ctx.fillStyle = S.ink; ctx.font = '800 15px system-ui,sans-serif'; ctx.fillText('Drag goods to a free spot ยท 3 alike pop', W / 2, 60); ctx.globalAlpha = 1; } } // ------------------------------------------------------------------ input: drag an item to a shelf, or tap item then tap shelf const toWorld = (e) => { const r = cv.getBoundingClientRect(); return [((e.clientX - r.left) / r.width) * W, ((e.clientY - r.top) / r.height) * H]; }; function itemAt(x, y) { for (let s = 0; s < L.length; s++) { const p = L[s], fr = game.board[s][0]; if (!fr) continue; for (let k = 0; k < 3; k++) if (fr[k] != null && Math.hypot(x - p.slot(k), y - (p.base - 24)) < 27) return [s, k]; } return null; } function shelfAt(x, y) { for (let s = 0; s < L.length; s++) { const p = L[s]; if (x >= p.x && x <= p.x + p.w && y >= p.y && y <= p.y + p.h) return s; } return -1; } function slotNear(s, x) { const fr = game.board[s][0] || [null, null, null]; let best = -1, bd = 1e9; for (let k = 0; k < 3; k++) if (fr[k] == null) { const d = Math.abs(L[s].slot(k) - x); if (d < bd) { bd = d; best = k; } } return best; } cv.addEventListener('pointerdown', (e) => { e.preventDefault(); if (!game || game.state !== 'run') { if (endShown) start(); return; } const [x, y] = toWorld(e), hit = itemAt(x, y); if (hit) { drag = { a: hit[0], i: hit[1], it: game.board[hit[0]][0][hit[1]], x, y, sx: x, sy: y, moved: false }; try { cv.setPointerCapture(e.pointerId); } catch { /* */ } tone(660, 0.03, 'sine', 0.03); return; } if (sel) { const s = shelfAt(x, y); if (s >= 0 && s !== sel[0]) play(sel[0], sel[1], s, slotNear(s, x)); else sel = null; } }); cv.addEventListener('pointermove', (e) => { if (!drag) return; const [x, y] = toWorld(e); drag.x = x; drag.y = y; if (Math.hypot(x - drag.sx, y - drag.sy) > 10) drag.moved = true; }); cv.addEventListener('pointerup', (e) => { if (!drag) return; const [x, y] = toWorld(e), d = drag; drag = null; const s = shelfAt(x, y); if (!d.moved || s === d.a) { // a tap: select it (tap again to drop), or move the selection here if (sel && sel[0] !== d.a) { play(sel[0], sel[1], d.a, slotNear(d.a, x)); return; } sel = sel && sel[0] === d.a && sel[1] === d.i ? null : [d.a, d.i]; return; } if (s >= 0) play(d.a, d.i, s, slotNear(s, x), [x, y - 14]); }); cv.addEventListener('pointercancel', () => { drag = null; }); document.addEventListener('keydown', (e) => { if (/^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement && document.activeElement.tagName)) return; if (e.key === 'r' || e.key === 'R') { e.preventDefault(); start(); } else if (e.key === 'm' || e.key === 'M') $('mute').click(); else if (e.key === 'h' || e.key === 'H') idle = 99; else if ((e.key === ' ' || e.key === 'Enter') && endShown && document.activeElement && document.activeElement.tagName !== 'BUTTON') { e.preventDefault(); start(); } }); // ------------------------------------------------------------------ results, share, card function shareText() { return `${coreShare(game, label())}\nCan you clear today's shelves?`; } function showOver(newBest, done) { $('over-title').textContent = done ? (newBest ? 'New best!' : 'Shelves clear!') : game.state === 'stuck' ? 'No free spot!' : 'Time!'; $('over-score').textContent = game.score.toLocaleString('en-US'); $('over-line').textContent = done ? `Cleared in ${fmt(game.t)} ${'โญ'.repeat(game.stars())}` : `${game.popped}/${game.total} popped ยท best combo x${game.bestCombo}`; $('again').textContent = done && mode.kind === 'level' ? `Level ${level} โ–ถ` : 'Play again'; $('over-grid').textContent = coreShare(game, label()); const it = intents(GAME, shareText(), PATH); $('sx').href = it.x; $('sb').href = it.bsky; $('sw').href = it.whatsapp; $('over').hidden = false; shopVis(true); pass.afterWin($('over'), 'over'); } function card() { const S = LOOKS[look], c = document.createElement('canvas'); c.width = 1080; c.height = 1350; const x = c.getContext('2d'); const gr = x.createLinearGradient(0, 0, 0, 1350); gr.addColorStop(0, S.top); gr.addColorStop(1, S.bot); x.fillStyle = gr; x.fillRect(0, 0, 1080, 1350); const kinds = Object.keys(game.popsBy).map(Number); const show = kinds.length ? kinds : [0, 1, 2]; x.save(); x.scale(3, 3); show.slice(0, 12).forEach((k, i) => good(x, k, 50 + (i % 6) * 48, 190 + Math.floor(i / 6) * 50)); x.restore(); x.fillStyle = S.wood; x.fillRect(80, 650, 920, 24); x.fillStyle = S.ink; x.textAlign = 'left'; x.font = '900 80px system-ui,sans-serif'; x.fillText('SHELF POP', 80, 150); x.globalAlpha = 0.75; x.font = '700 40px system-ui,sans-serif'; x.fillText(mode.kind === 'daily' ? `Daily Shelf #${dayNumber(mode.date)} ยท ${mode.date}` : `Level ${mode.n}`, 80, 210); x.globalAlpha = 1; x.font = '900 170px system-ui,sans-serif'; x.fillText(game.score.toLocaleString('en-US'), 80, 900); x.font = '700 48px system-ui,sans-serif'; x.fillText(game.state === 'done' ? `cleared in ${fmt(game.t)} ${'โญ'.repeat(game.stars())}` : `${game.popped}/${game.total} popped`, 80, 980); x.font = '800 44px system-ui,sans-serif'; x.fillText("Can you clear today's shelves?", 80, 1200); x.globalAlpha = 0.7; x.font = '600 32px system-ui,sans-serif'; x.fillText('Play free: cybermaxtools.com/games/shelfpop', 80, 1270); return c; } const fname = () => `shelfpop-${mode.kind === 'daily' ? mode.date : `level-${mode.n}`}.png`; $('share').onclick = async () => share(GAME, shareText(), PATH, await canvasBlob(card()), fname()); ['sx', 'sb', 'sw'].forEach((id) => $(id).addEventListener('click', () => count(GAME, 'share', { sx: 'x', sb: 'bsky', sw: 'whatsapp' }[id]))); $('card').onclick = async () => { download(await canvasBlob(card()), fname()); count(GAME, 'share', 'png'); }; $('pass-btn').onclick = () => pass.open('header'); $('again').onclick = () => { if (mode.kind === 'level') mode.n = level; start(); cv.focus(); }; document.addEventListener('visibilitychange', () => { last = performance.now(); }); function renderLooks() { const el = $('looks'); el.innerHTML = ''; for (const [id, s] of Object.entries(LOOKS)) { const b = document.createElement('button'), lock = !canUse(s); b.type = 'button'; b.title = `${s.name}${s.tier === 'pass' ? ' (Fun Pass)' : ''}`; b.setAttribute('aria-label', b.title); b.style.background = `linear-gradient(135deg, ${s.top} 50%, ${s.wood} 50%)`; b.setAttribute('aria-pressed', String(id === look)); if (lock) b.classList.add('locked'); b.onclick = () => { if (lock) { pass.open('theme'); return; } look = id; store('shelfpop:look', id); renderLooks(); }; el.appendChild(b); } } pass.onChange(() => { if (!canUse(LOOKS[look])) { look = 'market'; store('shelfpop:look', 'market'); } renderLooks(); renderModes(); }); // test + screenshot hooks (no effect on normal play) window.__shelfpop = { state: () => ({ state: game.state, score: game.score, popped: game.popped, total: game.total, moves: game.moves, left: game.left(), mode: mode.kind, level, endShown, shelves: game.board.length }), bot: (on = true) => { autoBot = on; }, solve: () => { for (const m of game.solution.slice(game.moves)) play(m[0], m[1], m[2]); }, // canvas CSS pixel positions for a move (for real drag tests) where: (s, k) => { const r = cv.getBoundingClientRect(), p = L[s]; return { x: r.left + (p.slot(k) / W) * r.width, y: r.top + ((p.base - 24) / H) * r.height, sx: r.left + ((p.x + p.w / 2) / W) * r.width, sy: r.top + ((p.base - 24) / H) * r.height }; }, next: () => game.solution[game.moves], free: (c) => (game.board[c][0] || [null, null, null]).indexOf(null), hint: hintMove, timeout: () => { game.t = game.time - 0.01; }, shareText, }; window.__cmxShot = async () => { mode = { kind: 'daily', date: todayIso }; start(); for (const m of game.solution.slice(0, 9)) play(m[0], m[1], m[2]); fx = []; texts = []; anims = new Map(); idle = 0; }; renderModes(); renderLooks(); start(); requestAnimationFrame(frame);