Skip to content
1        10        20        30        40        50        60        70        80        90        100       110       1201// The "come in" screen: name, look, and a preview. Works with touch and keyboard.2import { OPTIONS, ORDER, DEFAULT_LOOK, normalizeLook, buildCharacterCanvas } from './character.js';34const NAMES = ['Alex', 'Bea', 'Jun', 'Rafa', 'Sol', 'Dani', 'Noor', 'Lena'];56export function showJoin(root, onJoin) {7  root.hidden = false;8  root.innerHTML = `9    <div class="card">10      <h1>Gravy</h1>11      <p class="muted">Cook together, serve the table, keep everyone fed.</p>12      <div class="preview"><canvas width="96" height="144"></canvas></div>13      <label class="row"><span>Name</span>14        <input name="name" maxlength="12" autocomplete="off" value="${NAMES[Math.floor(Math.random() * NAMES.length)]}" />15      </label>16      <div class="options"></div>17      <button class="primary" type="button">Come in</button>18      <p class="error" hidden></p>19    </div>`;2021  const look = { ...DEFAULT_LOOK };22  const canvas = root.querySelector('canvas');23  const ctx = canvas.getContext('2d');24  const options = root.querySelector('.options');25  const error = root.querySelector('.error');2627  const preview = () => {28    const sheet = buildCharacterCanvas(look);29    ctx.imageSmoothingEnabled = false;30    ctx.clearRect(0, 0, canvas.width, canvas.height);31    // Frame 0 of the facing direction, scaled 4x.32    ctx.drawImage(sheet, 0, 0, 16, 24, 0, 0, 64, 96);33  };3435  for (const key of ORDER) {36    const row = document.createElement('div');37    row.className = 'row option';38    row.innerHTML = `39      <span>${OPTIONS[key].label}</span>40      <button type="button" aria-label="Previous">&lsaquo;</button>41      <b></b>42      <button type="button" aria-label="Next">&rsaquo;</button>`;43    const [prev, , value, next] = row.children;44    const update = () => {45      value.textContent = OPTIONS[key].items[look[key]].name;46      preview();47    };48    const step = (d) => {49      const n = OPTIONS[key].items.length;50      look[key] = (look[key] + d + n) % n;51      update();52    };53    prev.addEventListener('click', () => step(-1));54    next.addEventListener('click', () => step(1));55    options.appendChild(row);56    update();57  }5859  const button = root.querySelector('button.primary');60  button.addEventListener('click', async () => {61    button.disabled = true;62    error.hidden = true;63    const name = root.querySelector('input[name=name]').value.trim() || 'Friend';64    try {65      await onJoin(name, normalizeLook(look));66    } catch (err) {67      error.textContent = err.message || 'Could not come in. Try again.';68      error.hidden = false;69      button.disabled = false;70    }71  });72  preview();73}7475export function hideJoin(root) {76  root.hidden = true;77  root.innerHTML = '';78}