🕎 Dreidel Spin - Hanukkah Dreidel Game 🪙

Tap the screen, click or press the space bar to spin the dreidel, then tap again to stop it. Land on Gimel (ג) to win the whole pot of gelt, Hei (ה) for half, Nun (נ) for nothing. Shin (ש) costs a life, and three Shins end the game. The dreidel spins faster every round!
Gelt: 0  |  Pot: 0  |  Lives: ❤️❤️❤️  |  Best: 0
Press "Start", then tap to spin the dreidel!

Hanukkah is here 🕎 and the family is gathered around the table with a pot of chocolate gelt. Tap, click or press the space bar to spin the dreidel, then tap again to stop it. Land on Gimel (ג) to take the whole pot, Hei (ה) to take half, and Nun (נ) for nothing, but watch out for Shin (ש): it costs you a life. Every round a new candle lights on the menorah and the dreidel spins a little faster, so timing gets harder. Three Shins and the game is over.

How much gelt can you win before your third Shin? 🪙

Here is the code
CSS
.dreid-game {
    text-align: center;
}

.dreid-game .instructions {
    max-width: 500px;
    margin: 0 auto 15px;
    font-size: 1em;
    color: #555;
}

.dreid-info {
    margin-bottom: 10px;
    font-size: 1.15em;
}

.dreid-canvas {
    display: block;
    width: 100%;
    max-width: 360px;
    height: auto;
    margin: 0 auto;
    border: 3px solid #1f4e9c;
    border-radius: 12px;
    background: #0f2a5c;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    cursor: pointer;
}

.dreid-message {
    min-height: 1.5em;
    margin: 12px 0;
    font-size: 1.1em;
    font-weight: bold;
    color: #1f4e9c;
}
HTML
<div class="dreid-game">
    <h1>🕎 Dreidel Spin - Hanukkah Dreidel Game 🪙</h1>
    <div class="instructions">
        Tap the screen, click or press the space bar to spin the dreidel, then tap again to stop it. Land on Gimel (ג) to win the whole pot of gelt, Hei (ה) for half, Nun (נ) for nothing. Shin (ש) costs a life, and three Shins end the game. The dreidel spins faster every round!
    </div>
    <div class="dreid-info">Gelt: <span id="dreidScore">0</span> &nbsp;|&nbsp; Pot: <span id="dreidPot">0</span> &nbsp;|&nbsp; Lives: <span id="dreidLives">❤️❤️❤️</span> &nbsp;|&nbsp; Best: <span id="dreidBest">0</span></div>
    <canvas id="dreidCanvas" class="dreid-canvas" width="360" height="480" aria-label="A spinning dreidel and a pot of gelt"></canvas>
    <div class="dreid-message" id="dreidMessage">Press "Start", then tap to spin the dreidel!</div>
    <button class="start-btn" id="dreidStart">Start</button>
</div>
JS
const canvas = document.getElementById('dreidCanvas');
const ctx = canvas.getContext('2d');
const livesDisplay = document.getElementById('dreidLives');
const potDisplay = document.getElementById('dreidPot');
const scoreDisplay = document.getElementById('dreidScore');
const bestDisplay = document.getElementById('dreidBest');
const message = document.getElementById('dreidMessage');
const startButton = document.getElementById('dreidStart');
const hiddenScore = document.getElementById('hiddenScore');

const W = canvas.width;
const H = canvas.height;
const MAX_LIVES = 3;
const PLAYERS = 4; // you and three family members, each adds one coin per round
// The four sides of the dreidel, in the order they turn past
const FACES = [
    {letter: 'נ', name: 'Nun', color: '#8ecae6', text: 'Nun: nothing happens'},
    {letter: 'ג', name: 'Gimel', color: '#ffd166', text: 'Gimel: you take the whole pot!'},
    {letter: 'ה', name: 'Hei', color: '#95d5b2', text: 'Hei: you take half the pot'},
    {letter: 'ש', name: 'Shin', color: '#ef8a8a', text: 'Shin: you lose a life'}
];

let lives, pot, score, round, angle, spinSpeed, state, landTimer, result, lastTime, animationId, coins, stars, flash;
let best = 0;

try {
    best = parseInt(localStorage.getItem('dreidBest'), 10) || 0;
} catch (e) {
    best = 0;
}
bestDisplay.textContent = best;

// The share buttons are rendered after this script, so look them up when needed.
function setShareButtons(display) {
    const shareButtons = document.querySelector('.share-buttons');
    if (shareButtons) {
        shareButtons.style.display = display;
    }
}

function hearts(n) {
    let s = '';
    for (let i = 0; i < MAX_LIVES; i++) {
        s += i < n ? '❤️' : '🤍';
    }
    return s;
}

function updateInfo() {
    livesDisplay.textContent = hearts(lives);
    potDisplay.textContent = pot;
    scoreDisplay.textContent = score;
    if (hiddenScore) {
        hiddenScore.value = score;
    }
}

function faceSpeed() {
    // Faces per second: the dreidel spins faster every round
    return Math.min(9, 2.2 + round * 0.45);
}

function makeStars() {
    stars = [];
    for (let i = 0; i < 40; i++) {
        stars.push({x: Math.random() * W, y: Math.random() * H * 0.55, r: Math.random() * 1.6 + 0.4, t: Math.random() * 6});
    }
}

function reset() {
    lives = MAX_LIVES;
    pot = 0;
    score = 0;
    round = 0;
    angle = Math.random() * 4;
    spinSpeed = 0;
    state = 'ready';
    landTimer = 0;
    result = null;
    coins = [];
    flash = 0;
    makeStars();
    updateInfo();
}

function startRound() {
    // Everyone adds one coin to the pot, then you spin
    pot += PLAYERS;
    round += 1;
    spinSpeed = faceSpeed();
    state = 'spinning';
    updateInfo();
    message.textContent = 'Spinning... tap to stop on Gimel (ג)!';
}

function stopSpin() {
    // The side facing you when you tap is the result
    const index = Math.round(angle) % 4;
    result = FACES[(index + 4) % 4];
    state = 'landing';
    landTimer = 0.7;
}

function settle() {
    let won = 0;
    if (result.name === 'Gimel') {
        won = pot;
    } else if (result.name === 'Hei') {
        won = Math.ceil(pot / 2);
    } else if (result.name === 'Shin') {
        lives -= 1;
    }
    if (won > 0) {
        pot -= won;
        score += won;
        flash = 1;
        for (let i = 0; i < Math.min(won, 18); i++) {
            coins.push({x: W / 2 + (Math.random() - 0.5) * 60, y: 395, vx: (Math.random() - 0.5) * 220, vy: -200 - Math.random() * 180, life: 1.2});
        }
    }
    updateInfo();
    if (lives <= 0) {
        gameOver();
        return;
    }
    state = 'ready';
    let text = result.letter + ' ' + result.text;
    if (won > 0) {
        text += ' +' + won + ' gelt.';
    }
    message.textContent = text + ' Tap to spin again.';
}

function gameOver() {
    state = 'over';
    if (score > best) {
        best = score;
        try {
            localStorage.setItem('dreidBest', best);
        } catch (e) {
            // ignore storage errors
        }
        bestDisplay.textContent = best;
    }
    message.textContent = 'Third Shin! You won ' + score + ' gelt in ' + round + ' spins. Press "Start" to play again.';
    startButton.style.display = 'inline-block';
    setShareButtons('flex');
}

function handleTap() {
    if (state === 'ready') {
        startRound();
    } else if (state === 'spinning') {
        stopSpin();
    }
}

function drawBackground(time) {
    ctx.fillStyle = '#0f2a5c';
    ctx.fillRect(0, 0, W, H);
    for (let i = 0; i < stars.length; i++) {
        const s = stars[i];
        ctx.globalAlpha = 0.5 + 0.5 * Math.sin(time * 2 + s.t);
        ctx.fillStyle = '#ffffff';
        ctx.beginPath();
        ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
        ctx.fill();
    }
    ctx.globalAlpha = 1;
    // Table
    ctx.fillStyle = '#5b3a29';
    ctx.fillRect(0, 380, W, H - 380);
    ctx.fillStyle = '#6f4733';
    ctx.fillRect(0, 380, W, 10);
}

function drawMenorah(time) {
    // A small menorah lit with one candle per spin, up to eight
    const baseX = W / 2;
    const y = 60;
    ctx.fillStyle = '#d4a017';
    ctx.fillRect(baseX - 100, y + 30, 200, 5);
    ctx.fillRect(baseX - 3, y + 30, 6, 30);
    ctx.fillRect(baseX - 30, y + 58, 60, 6);
    // Candles are added from the right, one per spin; the shamash (middle) is always lit
    const lit = Math.min(8, round);
    const order = [8, 7, 6, 5, 3, 2, 1, 0];
    for (let i = 0; i < 9; i++) {
        const x = baseX - 96 + i * 24;
        const top = i === 4 ? y - 8 : y;
        ctx.fillStyle = '#f1f1f1';
        ctx.fillRect(x - 3, top, 6, y + 30 - top);
        const on = i === 4 || order.indexOf(i) < lit && order.indexOf(i) !== -1;
        if (on) {
            const flicker = 1 + Math.sin(time * 12 + i) * 0.15;
            ctx.fillStyle = '#ffb703';
            ctx.beginPath();
            ctx.ellipse(x, top - 7, 3.5, 7 * flicker, 0, 0, Math.PI * 2);
            ctx.fill();
        }
    }
}

function drawDreidel(time) {
    const cx = W / 2;
    const cy = 262;
    let wobble = 0;
    if (state === 'spinning') {
        wobble = Math.sin(time * 9) * 0.05;
    } else if (state === 'landing') {
        wobble = Math.sin(landTimer * 30) * 0.18 * landTimer;
    }
    ctx.save();
    ctx.translate(cx, cy);
    ctx.rotate(wobble);
    // Shadow
    ctx.fillStyle = 'rgba(0,0,0,0.35)';
    ctx.beginPath();
    ctx.ellipse(0, 132, 50, 9, 0, 0, Math.PI * 2);
    ctx.fill();
    // Handle
    ctx.fillStyle = '#c9a227';
    ctx.fillRect(-8, -112, 16, 32);
    // Two visible sides: their widths follow the rotation, like a turning cube
    const frac = angle - Math.floor(angle);
    const index = Math.floor(angle) % 4;
    const a = frac * Math.PI / 2;
    const w1 = Math.cos(a) * 140;
    const w2 = Math.sin(a) * 140;
    const left = -(w1 + w2) / 2;
    drawSide(left, w1, FACES[(index + 4) % 4], frac);
    drawSide(left + w1, w2, FACES[(index + 5) % 4], 1 - frac);
    // Point
    ctx.fillStyle = '#c9a227';
    ctx.beginPath();
    ctx.moveTo(left, 50);
    ctx.lineTo(left + w1 + w2, 50);
    ctx.lineTo(0, 135);
    ctx.closePath();
    ctx.fill();
    ctx.restore();
}

function drawSide(x, w, face, shade) {
    if (w < 1) {
        return;
    }
    ctx.fillStyle = '#1f4e9c';
    ctx.fillRect(x, -80, w, 130);
    ctx.fillStyle = 'rgba(0,0,0,' + (shade * 0.45).toFixed(2) + ')';
    ctx.fillRect(x, -80, w, 130);
    ctx.strokeStyle = '#c9a227';
    ctx.lineWidth = 3;
    ctx.strokeRect(x, -80, w, 130);
    ctx.save();
    ctx.translate(x + w / 2, -15);
    ctx.scale(w / 140, 1);
    ctx.fillStyle = face.color;
    ctx.font = 'bold 76px Arial, sans-serif';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText(face.letter, 0, 0);
    ctx.restore();
}

function drawPot() {
    // The pot of gelt on the table
    const x = W / 2;
    const y = 430;
    ctx.fillStyle = '#3d2a1e';
    ctx.beginPath();
    ctx.ellipse(x, y, 70, 22, 0, 0, Math.PI * 2);
    ctx.fill();
    const shown = Math.min(pot, 24);
    for (let i = 0; i < shown; i++) {
        const cx = x - 50 + (i % 8) * 14;
        const cy = y - 4 - Math.floor(i / 8) * 7;
        drawCoin(cx, cy, 8);
    }
    ctx.fillStyle = '#ffffff';
    ctx.font = 'bold 16px Arial, sans-serif';
    ctx.textAlign = 'center';
    ctx.fillText('Pot: ' + pot + ' gelt', x, y + 40);
}

function drawCoin(x, y, r) {
    ctx.fillStyle = '#e9b949';
    ctx.beginPath();
    ctx.arc(x, y, r, 0, Math.PI * 2);
    ctx.fill();
    ctx.strokeStyle = '#a87b14';
    ctx.lineWidth = 1.5;
    ctx.stroke();
}

function drawOverlay() {
    if (state === 'ready' && round === 0) {
        ctx.fillStyle = 'rgba(0,0,0,0.45)';
        ctx.fillRect(60, 6, W - 120, 34);
        ctx.fillStyle = '#ffffff';
        ctx.font = 'bold 18px Arial, sans-serif';
        ctx.textAlign = 'center';
        ctx.fillText('Tap to spin!', W / 2, 29);
    }
    if (flash > 0) {
        ctx.fillStyle = 'rgba(255, 209, 102, ' + (flash * 0.35).toFixed(2) + ')';
        ctx.fillRect(0, 0, W, H);
    }
}

function loop(now) {
    const dt = Math.min(0.05, (now - lastTime) / 1000);
    lastTime = now;
    const time = now / 1000;

    if (state === 'spinning') {
        angle += spinSpeed * dt;
    } else if (state === 'landing') {
        // Ease onto the chosen side
        const target = Math.round(angle);
        angle += (target - angle) * Math.min(1, dt * 10);
        landTimer -= dt;
        if (landTimer <= 0) {
            angle = Math.round(angle);
            settle();
        }
    } else if (state === 'ready' && round === 0) {
        angle += 0.4 * dt;
    }
    if (flash > 0) {
        flash = Math.max(0, flash - dt * 1.5);
    }
    for (let i = coins.length - 1; i >= 0; i--) {
        const c = coins[i];
        c.vy += 600 * dt;
        c.x += c.vx * dt;
        c.y += c.vy * dt;
        c.life -= dt;
        if (c.life <= 0) {
            coins.splice(i, 1);
        }
    }

    drawBackground(time);
    drawMenorah(time);
    drawDreidel(time);
    drawPot();
    for (let i = 0; i < coins.length; i++) {
        drawCoin(coins[i].x, coins[i].y, 7);
    }
    drawOverlay();

    if (state !== 'idle') {
        animationId = requestAnimationFrame(loop);
    }
}

function startGame() {
    if (animationId) {
        cancelAnimationFrame(animationId);
    }
    reset();
    if (hiddenScore) {
        hiddenScore.value = 0;
    }
    setShareButtons('none');
    startButton.style.display = 'none';
    message.textContent = 'Tap the dreidel to spin it!';
    lastTime = performance.now();
    animationId = requestAnimationFrame(loop);
}

canvas.addEventListener('touchstart', function (e) {
    e.preventDefault();
    handleTap();
}, {passive: false});

canvas.addEventListener('mousedown', function () {
    handleTap();
});

document.addEventListener('keydown', function (e) {
    if (e.code === 'Space' && (state === 'ready' || state === 'spinning') && startButton.style.display === 'none') {
        e.preventDefault();
        handleTap();
    }
});

startButton.addEventListener('click', startGame);

// Draw a still dreidel before the first game
state = 'idle';
lives = MAX_LIVES;
pot = 0;
score = 0;
round = 0;
angle = 0.2;
coins = [];
flash = 0;
makeStars();
drawBackground(0);
drawMenorah(0);
drawDreidel(0);
drawPot();