From 8c0bb74ebbf780dca631e01f968f8d1bce177d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:16:54 +0300 Subject: [PATCH 1/3] Render floating score popups for eaten contributions --- src/animation.ts | 152 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/src/animation.ts b/src/animation.ts index b6a54cd..01cfb01 100644 --- a/src/animation.ts +++ b/src/animation.ts @@ -16,6 +16,16 @@ export interface GridPosition { row: number; } +export interface ScoreEvent { + readonly position: GridPosition; + readonly score: number; + readonly eatenAtFrame: number; +} + +export interface ScorePopup extends ScoreEvent { + readonly age: number; +} + const BACKGROUND = "#0d1117"; const CHARACTER = "#ffdf00"; const EYE = "#242424"; @@ -38,6 +48,25 @@ const CONTRIBUTION_ROWS = 7; const MIN_ZIGZAG_RUN = 8; const MAX_ZIGZAG_RUN = 14; +const SCORE_POPUP_DURATION_FRAMES = 5; +const SCORE_POPUP_RISE_PER_FRAME = 1; +const SCORE_POPUP_MAX_RISE = 4; +const SCORE_GLYPH_WIDTH = 3; +const SCORE_GLYPH_HEIGHT = 5; +const SCORE_GLYPHS: Record = { + "+": ["...", ".#.", "###", ".#.", "..."], + "0": ["###", "#.#", "#.#", "#.#", "###"], + "1": [".#.", "##.", ".#.", ".#.", "###"], + "2": ["###", "..#", "###", "#..", "###"], + "3": ["###", "..#", "###", "..#", "###"], + "4": ["#.#", "#.#", "###", "..#", "..#"], + "5": ["###", "#..", "###", "..#", "###"], + "6": ["###", "#..", "###", "#.#", "###"], + "7": ["###", "..#", "..#", ".#.", ".#."], + "8": ["###", "#.#", "###", "#.#", "###"], + "9": ["###", "#.#", "###", "..#", "###"], +}; + /** * Build a deterministic, connected path through every contribution cell. * Long, varied column bands reduce the rapid back-and-forth movement while @@ -169,6 +198,44 @@ export function getContributionLevelForFrame( * garden. The character follows a connected, varied-width zigzag path and * changes eaten green cells to gray while preserving the grid squares. */ +/** + * Build one score event per green contribution cell, in the order Commit-Man + * reaches those cells. Empty gray cells never award points. + */ +export function buildScoreEvents( + path: readonly GridPosition[], + cellByKey: ReadonlyMap, +): ScoreEvent[] { + const events: ScoreEvent[] = []; + let score = 0; + + path.forEach((position, eatenAtFrame) => { + const day = cellByKey.get(positionKey(position)); + if (!day || day.contributionLevel === "NONE") return; + + score += 1; + events.push({ position, score, eatenAtFrame }); + }); + + return events; +} + +/** + * Return score popups that are visible in a frame, with their age for animation. + */ +export function getScorePopupsForFrame( + events: readonly ScoreEvent[], + frameIndex: number, +): ScorePopup[] { + return events.flatMap((event) => { + const age = frameIndex - event.eatenAtFrame; + return age < 0 || age >= SCORE_POPUP_DURATION_FRAMES + ? [] + : [{ ...event, age }]; + }); +} + +/** export function renderCommitManGif( inputDays: ContributionDay[], options: CommitManAnimationOptions = {}, @@ -228,6 +295,7 @@ export function renderCommitManGif( } const visitOrder = new Map(path.map((position, index) => [positionKey(position), index])); + const scoreEvents = buildScoreEvents(path, cellByKey); const encoder = GIFEncoder(); const radius = Math.max(3, cellSize * 0.66); @@ -274,6 +342,20 @@ export function renderCommitManGif( MOUTH_SEQUENCE[frameIndex % MOUTH_SEQUENCE.length]!, ); + for (const popup of getScorePopupsForFrame(scoreEvents, frameIndex)) { + const cellX = padding + popup.position.column * pitch; + const cellY = padding + popup.position.row * pitch; + drawScorePopup( + pixels, + width, + height, + cellX + cellSize / 2, + cellY, + popup.score, + popup.age, + ); + } + const indexed = applyPalette(pixels, PALETTE); encoder.writeFrame(indexed, width, height, { palette: PALETTE, @@ -366,6 +448,76 @@ function fillCell( } } +/** + * Draw a short-lived yellow +N label that rises above the eaten contribution. + * A compact 3x5 bitmap font keeps rendering deterministic without extra assets. + */ +function drawScorePopup( + pixels: Uint8ClampedArray, + width: number, + height: number, + centerX: number, + cellTop: number, + score: number, + age: number, +): void { + const text = `+${score}`; + const scale = score < 10 ? 2 : 1; + const textWidth = + text.length * SCORE_GLYPH_WIDTH * scale + (text.length - 1) * scale; + const textHeight = SCORE_GLYPH_HEIGHT * scale; + const x = Math.round(centerX - textWidth / 2); + const rise = Math.min(age * SCORE_POPUP_RISE_PER_FRAME, SCORE_POPUP_MAX_RISE); + const y = Math.max(0, Math.round(cellTop - textHeight - 2 - rise)); + const shadow = hexToRgb(BACKGROUND); + const foreground = hexToRgb(CHARACTER); + + drawBitmapText(pixels, width, height, text, x + 1, y + 1, shadow, scale); + drawBitmapText(pixels, width, height, text, x, y, foreground, scale); +} + +function drawBitmapText( + pixels: Uint8ClampedArray, + width: number, + height: number, + text: string, + x: number, + y: number, + color: [number, number, number], + scale: number, +): void { + let cursorX = x; + + for (const character of text) { + const glyph = SCORE_GLYPHS[character]; + if (!glyph) { + cursorX += (SCORE_GLYPH_WIDTH + 1) * scale; + continue; + } + + for (let row = 0; row < glyph.length; row += 1) { + for (let column = 0; column < glyph[row]!.length; column += 1) { + if (glyph[row]![column] !== "#") continue; + + for (let offsetY = 0; offsetY < scale; offsetY += 1) { + for (let offsetX = 0; offsetX < scale; offsetX += 1) { + setPixel( + pixels, + width, + height, + cursorX + column * scale + offsetX, + y + row * scale + offsetY, + color, + ); + } + } + } + } + + cursorX += (SCORE_GLYPH_WIDTH + 1) * scale; + } +} + /** * Keep the eye on the character's upper side regardless of travel direction. * Only let it shift slightly left/right; rotating it with the heading made From 32fcdf7fbbbe94b4d6f17982aa86cddd8f2dee61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:16:56 +0300 Subject: [PATCH 2/3] Test score event order and popup lifetime --- tests/animation.test.ts | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/animation.test.ts b/tests/animation.test.ts index c5b3413..342a957 100644 --- a/tests/animation.test.ts +++ b/tests/animation.test.ts @@ -2,9 +2,11 @@ import assert from "node:assert/strict"; import test from "node:test"; import type { ContributionDay } from "../src/contributions.js"; import { + buildScoreEvents, buildZigzagPath, getCommitManEyePosition, getContributionLevelForFrame, + getScorePopupsForFrame, renderCommitManGif, } from "../src/animation.js"; @@ -104,6 +106,48 @@ test("turns an eaten green contribution cell gray without removing its square", assert.equal(getContributionLevelForFrame(greenDay, 2, 3), "NONE"); }); +test("awards increasing scores only for green contribution cells", () => { + const path = [ + { column: 0, row: 0 }, + { column: 1, row: 0 }, + { column: 2, row: 0 }, + { column: 0, row: 1 }, + ]; + const cellByKey = new Map([ + ["0:0", sampleDays[0]!], + ["1:0", sampleDays[1]!], + ["2:0", sampleDays[2]!], + ["0:1", sampleDays[0]!], + ]); + + const events = buildScoreEvents(path, cellByKey); + assert.deepEqual( + events.map(({ position, score, eatenAtFrame }) => ({ position, score, eatenAtFrame })), + [ + { position: { column: 1, row: 0 }, score: 1, eatenAtFrame: 1 }, + { position: { column: 2, row: 0 }, score: 2, eatenAtFrame: 2 }, + ], + ); +}); + +test("shows floating score popups briefly with an increasing score", () => { + const events = [ + { position: { column: 1, row: 0 }, score: 1, eatenAtFrame: 1 }, + { position: { column: 2, row: 0 }, score: 2, eatenAtFrame: 2 }, + { position: { column: 3, row: 0 }, score: 3, eatenAtFrame: 3 }, + ]; + + assert.deepEqual( + getScorePopupsForFrame(events, 2).map(({ score, age }) => ({ score, age })), + [{ score: 1, age: 1 }, { score: 2, age: 0 }], + ); + assert.deepEqual( + getScorePopupsForFrame(events, 6).map(({ score, age }) => ({ score, age })), + [{ score: 2, age: 4 }, { score: 3, age: 3 }], + ); + assert.deepEqual(getScorePopupsForFrame(events, 8), []); +}); + test("keeps Commit-Man's eye upright when moving left or right", () => { const centerX = 50; const centerY = 50; From e202bc93f28e12959575f2647371843e2fb2daab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:17:37 +0300 Subject: [PATCH 3/3] Keep renderer documentation attached to its function --- src/animation.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/animation.ts b/src/animation.ts index 01cfb01..ef0f07f 100644 --- a/src/animation.ts +++ b/src/animation.ts @@ -193,11 +193,7 @@ export function getContributionLevelForFrame( return "NONE"; } -/** - * Render an animated GIF of Commit-Man travelling through the contribution - * garden. The character follows a connected, varied-width zigzag path and - * changes eaten green cells to gray while preserving the grid squares. - */ + /** * Build one score event per green contribution cell, in the order Commit-Man * reaches those cells. Empty gray cells never award points. @@ -236,6 +232,10 @@ export function getScorePopupsForFrame( } /** + * Render an animated GIF of Commit-Man travelling through the contribution + * garden. The character follows a connected, varied-width zigzag path and + * changes eaten green cells to gray while preserving the grid squares. + */ export function renderCommitManGif( inputDays: ContributionDay[], options: CommitManAnimationOptions = {},