🥂 Champagne Pour - New Year's Eve Game 🎆

Fill each glass to the gold line before midnight! Hold the mouse, your finger or the space bar to pour, and let go to stop. The bubbles keep rising for a moment after you stop, so release early. Overflow and the round ends.
Glasses: 0  |  Points: 0  |  Best: 0
Press "Start", then hold to pour!

It's ten minutes to midnight and every guest needs a glass of bubbly 🍾. Hold the mouse, your finger or the space bar to pour, and let go to stop: the champagne still in the air keeps landing for a moment, so release just before the gold line. Every glass filled brings the clock a minute closer to the New Year, but the bottle pours faster and the line gets thinner. Miss the line three times, or spill over the rim, and the party is over.

How many glasses can you pour before the countdown? 🎆

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

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

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

.chp-canvas {
    display: block;
    width: 100%;
    max-width: 360px;
    height: auto;
    margin: 0 auto;
    border: 3px solid #b8892b;
    border-radius: 14px;
    background: #14183a;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    cursor: pointer;
}

.chp-message {
    min-height: 1.6em;
    margin: 12px auto 10px;
    font-size: 1.1em;
    font-weight: bold;
}
HTML
<div class="chp-game">
    <h1>🥂 Champagne Pour - New Year's Eve Game 🎆</h1>
    <div class="instructions">
        Fill each glass to the gold line before midnight! Hold the mouse, your finger or the space bar to pour, and let go to stop. The bubbles keep rising for a moment after you stop, so release early. Overflow and the round ends.
    </div>
    <div class="chp-info">Glasses: <span id="chpScore">0</span> &nbsp;|&nbsp; Points: <span id="chpPoints">0</span> &nbsp;|&nbsp; Best: <span id="chpBest">0</span></div>
    <canvas id="chpCanvas" class="chp-canvas" width="360" height="420" aria-label="A champagne glass to fill to the line"></canvas>
    <div class="chp-message" id="chpMessage">Press "Start", then hold to pour!</div>
    <button class="start-btn" id="chpStart">Start</button>
</div>
JS
(function () {
    var canvas = document.getElementById('chpCanvas');
    var ctx = canvas.getContext('2d');
    var scoreEl = document.getElementById('chpScore');
    var pointsEl = document.getElementById('chpPoints');
    var bestEl = document.getElementById('chpBest');
    var messageEl = document.getElementById('chpMessage');
    var startBtn = document.getElementById('chpStart');

    var W = canvas.width;
    var CX = W / 2;
    var TOP = 120;       // rim of the glass
    var BOTTOM = 340;    // bottom of the bowl
    var HEIGHT = BOTTOM - TOP;
    var LIVES = 3;

    // idle → ready (waiting for the first hold) → pouring → settling → result → ready … → over
    var state = 'idle';
    var glasses = 0;
    var points = 0;
    var lives = LIVES;
    var best = 0;
    var level = 0;        // 0 = empty, 1 = rim
    var target = 0.7;
    var tolerance = 0.08;
    var rate = 0.35;      // fill per second while pouring
    var inFlight = 0;     // champagne already falling when the pour stops
    var holding = false;
    var lastTime = 0;
    var resultTimer = 0;
    var flash = '';
    var bubbles = [];
    var stars = [];

    for (var s = 0; s < 40; s++) {
        stars.push({x: Math.random() * W, y: Math.random() * 100, r: Math.random() * 1.6 + 0.4});
    }

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

    function setHidden(value) {
        var hidden = document.getElementById('hiddenScore');
        if (hidden) hidden.value = value;
    }

    function showShare(show) {
        // The share buttons sit after the game in the page: look them up when needed.
        var share = document.querySelector('.share-buttons');
        if (share) share.style.display = show ? 'flex' : 'none';
    }

    // Half width of the flute at a fill fraction (0 = bottom, 1 = rim).
    function halfWidth(f) {
        return 22 + 34 * Math.pow(Math.max(0, f), 0.75);
    }

    function yAt(f) {
        return BOTTOM - f * HEIGHT;
    }

    function newGlass() {
        level = 0;
        inFlight = 0;
        bubbles = [];
        target = 0.55 + Math.random() * 0.33;
        tolerance = Math.max(0.035, 0.08 - glasses * 0.004);
        rate = Math.min(1.15, 0.35 + glasses * 0.05);
        state = 'ready';
        messageEl.textContent = 'Glass ' + (glasses + 1) + ': hold to pour!';
    }

    function clockText() {
        // Ten minutes to midnight, one minute per glass filled.
        var minutes = 50 + glasses;
        if (minutes >= 60) return 'HAPPY NEW YEAR! 🎆';
        return '23:' + (minutes < 10 ? '0' : '') + minutes;
    }

    function startPour() {
        if (state === 'ready') {
            state = 'pouring';
        }
        if (state === 'pouring') holding = true;
    }

    function stopPour() {
        if (state === 'pouring' && holding) {
            holding = false;
            // What is already in the air still lands: release a little early.
            inFlight = rate * 0.22;
            state = 'settling';
        }
        holding = false;
    }

    function judge() {
        var diff = Math.abs(level - target);
        if (diff <= tolerance) {
            var gained = Math.round(100 * (1 - diff / tolerance)) + 10;
            glasses++;
            points += gained;
            setHidden(points);
            flash = diff < tolerance * 0.25 ? 'Perfect! +' + gained : 'Cheers! +' + gained;
        } else {
            lives--;
            flash = level < target ? 'Too low!' : 'Too much!';
        }
        scoreEl.textContent = glasses;
        pointsEl.textContent = points;
        messageEl.textContent = flash + '  ' + hearts();
        if (lives <= 0) {
            endGame('Out of glasses!');
            return;
        }
        state = 'result';
        resultTimer = 1.0;
    }

    function hearts() {
        var out = '';
        for (var i = 0; i < lives; i++) out += '🥂';
        return out;
    }

    function endGame(reason) {
        state = 'over';
        holding = false;
        if (points > best) {
            best = points;
            bestEl.textContent = best;
            try {
                localStorage.setItem('chpBest', String(best));
            } catch (e) {
                // Storage may be blocked: the best score just won't be kept.
            }
        }
        messageEl.textContent = reason + ' ' + glasses + (glasses === 1 ? ' glass, ' : ' glasses, ') + points + ' points.';
        startBtn.textContent = 'Play again';
        startBtn.style.display = 'inline-block';
        showShare(true);
    }

    function startGame() {
        glasses = 0;
        points = 0;
        lives = LIVES;
        flash = '';
        scoreEl.textContent = '0';
        pointsEl.textContent = '0';
        setHidden(0);
        showShare(false);
        startBtn.style.display = 'none';
        newGlass();
    }

    function update(dt) {
        if (state === 'pouring' && holding) {
            level += rate * dt;
        } else if (state === 'settling') {
            var land = Math.min(inFlight, rate * 1.6 * dt);
            level += land;
            inFlight -= land;
            if (inFlight <= 0.0005) judge();
        } else if (state === 'result') {
            resultTimer -= dt;
            if (resultTimer <= 0) newGlass();
        }

        if ((state === 'pouring' || state === 'settling') && level >= 1) {
            level = 1;
            endGame('Spilled over the rim!');
        }

        // Bubbles rise inside the champagne.
        if (level > 0.03 && Math.random() < dt * 25) {
            var f = Math.random() * level * 0.6;
            bubbles.push({f: f, dx: (Math.random() - 0.5) * 1.6, r: Math.random() * 2 + 1});
        }
        for (var i = bubbles.length - 1; i >= 0; i--) {
            bubbles[i].f += dt * 0.35;
            if (bubbles[i].f > level) bubbles.splice(i, 1);
        }
    }

    function glassPath(fTop) {
        ctx.beginPath();
        var steps = 24;
        for (var i = 0; i <= steps; i++) {
            var f = fTop * i / steps;
            ctx.lineTo(CX - halfWidth(f), yAt(f));
        }
        for (var j = steps; j >= 0; j--) {
            var g = fTop * j / steps;
            ctx.lineTo(CX + halfWidth(g), yAt(g));
        }
        ctx.closePath();
    }

    function draw() {
        ctx.clearRect(0, 0, W, canvas.height);

        // Night sky and clock.
        ctx.fillStyle = '#ffffff';
        for (var s = 0; s < stars.length; s++) {
            ctx.globalAlpha = 0.4 + 0.6 * Math.abs(Math.sin(lastTime / 900 + s));
            ctx.beginPath();
            ctx.arc(stars[s].x, stars[s].y, stars[s].r, 0, Math.PI * 2);
            ctx.fill();
        }
        ctx.globalAlpha = 1;
        ctx.fillStyle = '#f5d27a';
        ctx.font = 'bold 26px Helvetica, Arial, sans-serif';
        ctx.textAlign = 'center';
        ctx.fillText(clockText(), CX, 48);

        // Bottle and stream while pouring.
        if (state === 'pouring' && holding || state === 'settling') {
            ctx.fillStyle = '#1f5c3a';
            ctx.fillRect(CX - 10, 62, 20, 30);
            ctx.fillStyle = '#d6b24a';
            ctx.fillRect(CX - 10, 62, 20, 8);
            ctx.strokeStyle = 'rgba(247, 222, 140, 0.9)';
            ctx.lineWidth = state === 'settling' ? 2 : 4;
            ctx.beginPath();
            ctx.moveTo(CX, 92);
            ctx.lineTo(CX, yAt(level));
            ctx.stroke();
        }

        // Champagne.
        if (level > 0) {
            ctx.save();
            glassPath(level);
            var grad = ctx.createLinearGradient(0, yAt(level), 0, BOTTOM);
            grad.addColorStop(0, '#fbe7a1');
            grad.addColorStop(1, '#e3b54a');
            ctx.fillStyle = grad;
            ctx.fill();
            ctx.clip();
            ctx.fillStyle = 'rgba(255,255,255,0.7)';
            for (var b = 0; b < bubbles.length; b++) {
                var bf = bubbles[b].f;
                ctx.beginPath();
                ctx.arc(CX + bubbles[b].dx * halfWidth(bf) * 0.5, yAt(bf), bubbles[b].r, 0, Math.PI * 2);
                ctx.fill();
            }
            ctx.restore();
            // Foam on top.
            ctx.fillStyle = 'rgba(255, 250, 235, 0.95)';
            ctx.fillRect(CX - halfWidth(level) + 2, yAt(level) - 4, halfWidth(level) * 2 - 4, 5);
        }

        // Target band.
        if (state !== 'idle') {
            var yHi = yAt(Math.min(1, target + tolerance));
            var yLo = yAt(target - tolerance);
            ctx.fillStyle = 'rgba(245, 210, 122, 0.18)';
            ctx.fillRect(CX - 80, yHi, 160, yLo - yHi);
            ctx.strokeStyle = '#f5d27a';
            ctx.lineWidth = 2;
            ctx.setLineDash([6, 5]);
            ctx.beginPath();
            ctx.moveTo(CX - 80, yAt(target));
            ctx.lineTo(CX + 80, yAt(target));
            ctx.stroke();
            ctx.setLineDash([]);
        }

        // The glass itself.
        glassPath(1);
        ctx.strokeStyle = 'rgba(220, 230, 255, 0.85)';
        ctx.lineWidth = 3;
        ctx.stroke();
        ctx.beginPath();
        ctx.moveTo(CX, BOTTOM + 2);
        ctx.lineTo(CX, BOTTOM + 52);
        ctx.moveTo(CX - 40, BOTTOM + 56);
        ctx.quadraticCurveTo(CX, BOTTOM + 46, CX + 40, BOTTOM + 56);
        ctx.stroke();

        if (state === 'result' || state === 'over') {
            ctx.fillStyle = '#ffffff';
            ctx.font = 'bold 22px Helvetica, Arial, sans-serif';
            ctx.fillText(state === 'over' ? 'Game over' : flash, CX, 100);
        }
        if (state === 'idle') {
            ctx.fillStyle = '#ffffff';
            ctx.font = '18px Helvetica, Arial, sans-serif';
            ctx.fillText('Press Start', CX, 100);
        }
    }

    function loop(now) {
        var dt = lastTime ? Math.min(0.05, (now - lastTime) / 1000) : 0;
        lastTime = now;
        update(dt);
        draw();
        requestAnimationFrame(loop);
    }

    canvas.addEventListener('mousedown', function (e) {
        e.preventDefault();
        startPour();
    });
    window.addEventListener('mouseup', stopPour);
    canvas.addEventListener('touchstart', function (e) {
        e.preventDefault();
        startPour();
    }, {passive: false});
    canvas.addEventListener('touchend', function (e) {
        e.preventDefault();
        stopPour();
    }, {passive: false});
    canvas.addEventListener('touchcancel', stopPour);
    window.addEventListener('keydown', function (e) {
        if (e.code === 'Space' && state !== 'idle' && state !== 'over') {
            e.preventDefault();
            if (!e.repeat) startPour();
        }
    });
    window.addEventListener('keyup', function (e) {
        if (e.code === 'Space') stopPour();
    });
    startBtn.addEventListener('click', startGame);

    requestAnimationFrame(loop);
})();