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
22 changes: 21 additions & 1 deletion src/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,24 @@ export function getScorePopupsForFrame(
});
}

/**
* Match Snake's commit-eating beat by extending the frame just before a green
* contribution is eaten. Alternate one and two extra frame durations so the
* pause stays brief but noticeable, without introducing nondeterminism.
*/
export function getCommitPauseFramesForFrame(
scoreEvents: readonly ScoreEvent[],
frameIndex: number,
): 0 | 1 | 2 {
const nextMoveFrame = frameIndex + 1;
const nextScoreEvent = scoreEvents.find(
(event) => event.eatenAtFrame === nextMoveFrame,
);

if (!nextScoreEvent) return 0;
return nextScoreEvent.score % 2 === 0 ? 2 : 1;
}

/**
* 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 @@ -442,9 +460,11 @@ export function renderCommitManGif(
}

const indexed = applyPalette(pixels, PALETTE);
const commitPauseFrames = getCommitPauseFramesForFrame(scoreEvents, frameIndex);

encoder.writeFrame(indexed, width, height, {
palette: PALETTE,
delay: frameDelayMs,
delay: frameDelayMs * (1 + commitPauseFrames),
...(frameIndex === 0 ? { repeat: 0 } : {}),
});
}
Expand Down
17 changes: 17 additions & 0 deletions tests/animation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
buildScoreEvents,
buildZigzagPath,
getCommitManEyePosition,
getCommitPauseFramesForFrame,
getContributionLevelForFrame,
getScorePopupsForFrame,
renderCommitManGif,
Expand Down Expand Up @@ -135,6 +136,22 @@ test("awards increasing scores only for green contribution cells", () => {
);
});

test("adds a deterministic one- or two-frame pause before eating green cells only", () => {
const scoreEvents = [
{ position: { column: 1, row: 0 }, score: 1, eatenAtFrame: 1 },
{ position: { column: 2, row: 0 }, score: 2, eatenAtFrame: 3 },
{ position: { column: 4, row: 0 }, score: 3, eatenAtFrame: 6 },
];

assert.equal(getCommitPauseFramesForFrame(scoreEvents, 0), 1);
assert.equal(getCommitPauseFramesForFrame(scoreEvents, 1), 0);
assert.equal(getCommitPauseFramesForFrame(scoreEvents, 2), 2);
assert.equal(getCommitPauseFramesForFrame(scoreEvents, 3), 0);
assert.equal(getCommitPauseFramesForFrame(scoreEvents, 5), 1);
assert.equal(getCommitPauseFramesForFrame(scoreEvents, 6), 0);
assert.equal(getCommitPauseFramesForFrame([], 0), 0);
});

test("shows floating score popups briefly with an increasing score", () => {
const events = [
{ position: { column: 1, row: 0 }, score: 1, eatenAtFrame: 1 },
Expand Down
Loading