From 33c714516a211a4ebb43ef28cb274b5203e5f1d1 Mon Sep 17 00:00:00 2001 From: Noah Date: Wed, 4 Nov 2026 02:05:01 +0100 Subject: [PATCH] [chnobli] fix: scroll position at the end of the last trigger calcPosition only matched y < endY, so at exactly the last offset (the bottom of the page, where y can't get past it) no window matched and it returned -1: the scroll went to "before" and its timelines jumped back to the start. The end is included now, a zero height window gives its end position. Co-Authored-By: Claude Opus 5.5 --- chnobli/src/scroll/Scroll.ts | 6 ++++-- chnobli/src/tests/scroll.test.ts | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 chnobli/src/tests/scroll.test.ts diff --git a/chnobli/src/scroll/Scroll.ts b/chnobli/src/scroll/Scroll.ts index a8fb7ab..242b719 100644 --- a/chnobli/src/scroll/Scroll.ts +++ b/chnobli/src/scroll/Scroll.ts @@ -338,9 +338,11 @@ export default class ScrollTimeline { const startY = start.y - height * start.view; const endY = end.y - height * end.view; - if (y >= startY && y < endY) { + // the end is included, at the bottom of the page y can't get + // past the last offset + if (y >= startY && y <= endY) { const dif = endY - startY; - const x = 1 - (endY - y) / dif; + const x = dif === 0 ? 1 : 1 - (endY - y) / dif; return start.position + (end.position - start.position) * x; } diff --git a/chnobli/src/tests/scroll.test.ts b/chnobli/src/tests/scroll.test.ts new file mode 100644 index 0000000..a1aac68 --- /dev/null +++ b/chnobli/src/tests/scroll.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it } from 'vitest'; +import ScrollTimeline from '../scroll/Scroll.js'; + +function scrollWith(offsets: [number, number, number][]) { + const s = new ScrollTimeline(); + s.offsets = offsets.map(([y, view, position]) => ({ y, view, position })); + return (y: number) => (s as any).calcPosition({ y, height: 100 }); +} + +describe('scroll', () => { + it('position', () => { + // from 0 to 900, like a page that can't scroll past the end + const pos = scrollWith([ + [0, 0, 0], + [900, 0, 1], + ]); + + expect(pos(-1)).toBe(-1); + expect(pos(0)).toBe(0); + expect(pos(450)).toBe(0.5); + expect(pos(900)).toBe(1); + expect(pos(901)).toBe(2); + }); + + it('position with three offsets', () => { + const pos = scrollWith([ + [0, 0, 0], + [200, 1, 0.5], + [500, 0, 1], + ]); + + expect(pos(100)).toBe(0.5); + expect(pos(300)).toBe(0.75); + expect(pos(500)).toBe(1); + }); +});