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
78 changes: 66 additions & 12 deletions src/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,80 @@ const PALETTE: number[][] = [
const MOUTH_SEQUENCE = [0.06, 0.24, 0.48, 0.24];
const DEFAULT_FRAME_DELAY_MS = 70;

export function buildSerpentinePath(columns: number): GridPosition[] {
const DEFAULT_ZIGZAG_SEED = 0xC0FFEE;
const CONTRIBUTION_ROWS = 7;
const MIN_ZIGZAG_RUN = 2;
const MAX_ZIGZAG_RUN = 5;

/**
* Build a deterministic, connected path through every contribution cell.
* The route snakes through short column bands of varying widths instead of
* crossing the entire garden before changing rows. Odd/even bands alternate
* between descending and ascending through the seven rows.
*/
export function buildZigzagPath(
columns: number,
seed = DEFAULT_ZIGZAG_SEED,
): GridPosition[] {
if (!Number.isInteger(columns) || columns < 1) {
throw new Error("columns must be a positive integer.");
}
if (!Number.isSafeInteger(seed)) {
throw new Error("seed must be a safe integer.");
}

const random = createSeededRandom(seed);
const bands: Array<{ startColumn: number; width: number }> = [];
let nextColumn = 0;

while (nextColumn < columns) {
const remaining = columns - nextColumn;
const width = remaining <= MAX_ZIGZAG_RUN
? remaining
: MIN_ZIGZAG_RUN + Math.floor(random() * (MAX_ZIGZAG_RUN - MIN_ZIGZAG_RUN + 1));

bands.push({ startColumn: nextColumn, width });
nextColumn += width;
}

const path: GridPosition[] = [];
for (let row = 0; row < 7; row += 1) {
if (row % 2 === 0) {
for (let column = 0; column < columns; column += 1) {
path.push({ column, row });
}
} else {
for (let column = columns - 1; column >= 0; column -= 1) {
path.push({ column, row });
let startsAtTop = true;

for (const band of bands) {
const endColumn = band.startColumn + band.width - 1;

for (let rowOffset = 0; rowOffset < CONTRIBUTION_ROWS; rowOffset += 1) {
const row = startsAtTop ? rowOffset : CONTRIBUTION_ROWS - 1 - rowOffset;

if (rowOffset % 2 === 0) {
for (let column = band.startColumn; column <= endColumn; column += 1) {
path.push({ column, row });
}
} else {
for (let column = endColumn; column >= band.startColumn; column -= 1) {
path.push({ column, row });
}
}
}

startsAtTop = !startsAtTop;
}

return path;
}

function createSeededRandom(seed: number): () => number {
let state = seed >>> 0;

return () => {
state = (state + 0x6D2B79F5) | 0;
let value = state;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4_294_967_296;
};
}

/**
* Return the color level for a contribution cell in the current frame.
* Once a green cell has been eaten, preserve the square as an empty gray cell.
Expand All @@ -71,8 +125,8 @@ export function getContributionLevelForFrame(

/**
* Render an animated GIF of Commit-Man travelling through the contribution
* garden. The character follows a continuous serpentine path and removes each
* visited contribution cell to create a simple eating effect.
* 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[],
Expand Down Expand Up @@ -111,7 +165,7 @@ export function renderCommitManGif(
}

const columns = Math.ceil((firstWeekday + totalDays) / 7);
const path = buildSerpentinePath(columns);
const path = buildZigzagPath(columns);
const pitch = cellSize + gap;
const width = padding * 2 + columns * pitch - gap;
const height = padding * 2 + 7 * pitch - gap;
Expand Down
40 changes: 37 additions & 3 deletions tests/animation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import test from "node:test";
import type { ContributionDay } from "../src/contributions.js";
import {
buildSerpentinePath,
buildZigzagPath,
getCommitManEyePosition,
getContributionLevelForFrame,
renderCommitManGif,
Expand All @@ -15,7 +15,7 @@ const sampleDays: ContributionDay[] = [
];

test("creates a continuous serpentine path through every grid cell", () => {
const path = buildSerpentinePath(3);
const path = buildZigzagPath(3);
assert.equal(path.length, 21);
assert.deepEqual(path.slice(0, 6), [
{ column: 0, row: 0 },
Expand All @@ -33,6 +33,39 @@ test("creates a continuous serpentine path through every grid cell", () => {
}
});

test("builds a deterministic zigzag with varied run lengths", () => {
const columns = 17;
const seed = 20261012;
const path = buildZigzagPath(columns, seed);

assert.deepEqual(path, buildZigzagPath(columns, seed));
assert.equal(path.length, columns * 7);
assert.deepEqual(path[0], { column: 0, row: 0 });
assert.equal(
new Set(path.map(({ column, row }) => `${column}:${row}`)).size,
columns * 7,
);

for (const position of path) {
assert.ok(position.column >= 0 && position.column < columns);
assert.ok(position.row >= 0 && position.row < 7);
}

for (let index = 1; index < path.length; index += 1) {
const previous = path[index - 1]!;
const current = path[index]!;
assert.equal(
Math.abs(current.column - previous.column) + Math.abs(current.row - previous.row),
1,
`path jumps between ${previous.column}:${previous.row} and ${current.column}:${current.row}`,
);
}

const firstHorizontalRunLength = path.findIndex((position) => position.row !== 0);
assert.ok(firstHorizontalRunLength >= 2 && firstHorizontalRunLength <= 5);
assert.ok(firstHorizontalRunLength < columns, "the first run should end before crossing the entire garden");
});

test("keeps empty contribution cells gray throughout the animation", () => {
const emptyDay = sampleDays[0]!;
assert.equal(emptyDay.contributionLevel, "NONE");
Expand Down Expand Up @@ -104,5 +137,6 @@ test("rejects empty contribution data and invalid animation settings", () => {
assert.throws(() => renderCommitManGif([]), /At least one contribution day/);
assert.throws(() => renderCommitManGif(sampleDays, { cellSize: 0 }), /cellSize/);
assert.throws(() => renderCommitManGif(sampleDays, { gap: -1 }), /gap/);
assert.throws(() => buildSerpentinePath(0), /positive integer/);
assert.throws(() => buildZigzagPath(0), /positive integer/);
assert.throws(() => buildZigzagPath(3, Number.NaN), /safe integer/);
});
Loading