Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions src/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<string, readonly string[]> = {
"+": ["...", ".#.", "###", ".#.", "..."],
"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
Expand Down Expand Up @@ -164,6 +193,44 @@ export function getContributionLevelForFrame(
return "NONE";
}


/**
* 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<string, ContributionDay>,
): 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 }];
});
}

/**
* Render an animated GIF of Commit-Man travelling through the contribution
* garden. The character follows a connected, varied-width zigzag path and
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions tests/animation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand Down
Loading