import { describe, expect, it } from "vitest"; import { clampOffset, clampView, dotPhase, dotPitch, graphZoom, keyedView, leastScale, toggledView, zoomAt, dotArea, dotPath, } from "./viewMath"; const viewport = { width: 390, height: 700 }; const tall = { width: 830, height: 4300 }; describe("the graph's zoom (FZ-8449)", () => { it("zooms out no further than the whole graph on screen, and never in past 1:1", () => { const least = leastScale(tall, viewport); expect(least).toBeCloseTo(Math.min(390 / 830, 700 / 4300)); expect(tall.width * least).toBeLessThanOrEqual(viewport.width + 1e-6); expect(tall.height * least).toBeLessThanOrEqual(viewport.height + 1e-6); expect(leastScale({ width: 300, height: 600 }, viewport)).toBe(1); expect(leastScale({ width: 400, height: 100_000 }, viewport)).toBe(graphZoom.floor); const view = { scale: 1, x: 0, y: 0 }; expect(zoomAt(view, 0.01, { x: 0, y: 0 }, tall, viewport).scale).toBeCloseTo(least); expect(zoomAt(view, 3, { x: 0, y: 0 }, tall, viewport).scale).toBe(1); }); it("keeps the point under the fingers where it is", () => { const view = { scale: 1, x: -200, y: -1500 }; const focal = { x: 180, y: 320 }; const next = zoomAt(view, 0.5, focal, tall, viewport); const before = { x: (focal.x - view.x) / view.scale, y: (focal.y - view.y) / view.scale }; const after = { x: (focal.x - next.x) / next.scale, y: (focal.y - next.y) / next.scale }; expect(after.x).toBeCloseTo(before.x); expect(after.y).toBeCloseTo(before.y); }); it.each([1, 0.6, 0.3, 0.17])( "stops a pan at the outermost apps at scale %s, never past them into empty space", (scale) => { for (const [x, y] of [ [10_000, 10_000], [-10_000, -10_000], [-123, -456], ]) { const view = clampView({ scale, x, y }, tall, viewport); const wide = tall.width * view.scale; const high = tall.height * view.scale; if (wide > viewport.width) { expect(view.x).toBeLessThanOrEqual(0); expect(view.x + wide).toBeGreaterThanOrEqual(viewport.width); } else expect(view.x).toBeCloseTo((viewport.width - wide) / 2); if (high > viewport.height) { expect(view.y).toBeLessThanOrEqual(0); expect(view.y + high).toBeGreaterThanOrEqual(viewport.height); } else expect(view.y).toBeCloseTo((viewport.height - high) / 2); } }, ); it("centres a graph smaller than the screen on that axis", () => { expect(clampOffset(-50, 200, 390)).toBe(95); expect(clampOffset(50, 800, 390)).toBe(0); expect(clampOffset(-900, 800, 390)).toBe(-410); }); it("double-tap from 1:1 shows the whole graph, and from anywhere zoomed out comes back to 1:1 at the tap", () => { const out = toggledView({ scale: 1, x: 0, y: -900 }, { x: 100, y: 100 }, tall, viewport); expect(out.scale).toBeCloseTo(leastScale(tall, viewport)); const tap = { x: 120, y: 400 }; const back = toggledView({ scale: 0.4, x: 10, y: -300 }, tap, tall, viewport); expect(back.scale).toBe(1); const under = { x: (tap.x - 10) / 0.4, y: (tap.y + 300) / 0.4 }; expect(back.x).toBeCloseTo(Math.min(0, Math.max(viewport.width - tall.width, tap.x - under.x))); expect(back.y).toBeCloseTo( Math.min(0, Math.max(viewport.height - tall.height, tap.y - under.y)), ); }); it("zooms with + and - on a keyboard and comes back to 1:1 on 0", () => { const start = { scale: 1, x: 0, y: 0 }; const out = keyedView("-", start, tall, viewport)!; expect(out.scale).toBeCloseTo(1 / graphZoom.step); expect(keyedView("+", out, tall, viewport)!.scale).toBeCloseTo(1); expect(keyedView("+", start, tall, viewport)!.scale).toBe(1); expect(keyedView("0", { scale: 0.3, x: 0, y: 0 }, tall, viewport)!.scale).toBe(1); expect(keyedView("a", start, tall, viewport)).toBeUndefined(); }); it("thins the dot grid as it zooms out, so dots stay apart on screen", () => { const pitch = 22; for (const scale of [1, 0.8, 0.5, 0.3, 0.17, 0.1]) { const spaced = dotPitch(pitch, scale); expect(spaced * scale).toBeGreaterThanOrEqual(pitch / 2); expect(spaced * scale).toBeLessThanOrEqual(pitch); expect(Math.log2(spaced / pitch) % 1).toBe(0); } }); it("puts the first dot on the canvas's own grid, one step at most before the screen's edge", () => { const pitch = 22; for (const [offset, scale] of [ [0, 1], [-345.5, 1], [-1200, 0.4], [37, 0.2], ]) { const spaced = dotPitch(pitch, scale); const phase = dotPhase(offset, scale, pitch, spaced); expect(phase).toBeLessThan(1e-9); expect(phase).toBeGreaterThanOrEqual(-spaced * scale - 1e-9); const content = (phase - offset) / scale - pitch / 2; expect(Math.abs(content / spaced - Math.round(content / spaced))).toBeLessThan(1e-6); } }); }); const canvas = { width: 900, height: 5000 }; const pitch = 22; const chunk = pitch * 4; const dots = (path: string) => [...path.matchAll(/M([-\d.]+) ([-\d.]+)h0\.01/g)].map((match) => ({ x: Number(match[1]), y: Number(match[2]), })); describe("the graph's dot grid (FZ-8138)", () => { it.each([ { x: 0, y: 0 }, { x: 130, y: 2400 }, { x: canvas.width - viewport.width, y: canvas.height - viewport.height }, ])("covers the screen and a margin round it wherever the canvas is panned to (%o)", (offset) => { const area = dotArea(offset, viewport, canvas, chunk); expect(area.x).toBeLessThanOrEqual(Math.max(0, offset.x - chunk / 2)); expect(area.y).toBeLessThanOrEqual(Math.max(0, offset.y - chunk / 2)); expect(area.x + area.width).toBeGreaterThanOrEqual( Math.min(canvas.width, offset.x + viewport.width + chunk), ); expect(area.y + area.height).toBeGreaterThanOrEqual( Math.min(canvas.height, offset.y + viewport.height + chunk), ); expect(area.x + area.width).toBeLessThanOrEqual(canvas.width); expect(area.y + area.height).toBeLessThanOrEqual(canvas.height); }); it("keeps the same area for small pans, so a scroll does not redraw it every frame", () => { const first = dotArea({ x: 0, y: 1000 }, viewport, canvas, chunk); const areas = new Set(); for (let y = 1000; y < 1000 + chunk / 2; y += 3) areas.add(JSON.stringify(dotArea({ x: 0, y }, viewport, canvas, chunk))); expect(areas.size).toBeLessThanOrEqual(2); expect(areas.has(JSON.stringify(first))).toBe(true); }); it("puts every dot on the canvas's own grid, so dots line up as the area moves", () => { for (const offset of [ { x: 0, y: 0 }, { x: 200, y: 1300 }, ]) { const area = dotArea(offset, viewport, canvas, chunk); const placed = dots(dotPath(area, pitch)); expect(placed.length).toBeGreaterThan(100); for (const dot of placed) { const x = area.x + dot.x - pitch / 2; const y = area.y + dot.y - pitch / 2; expect(Math.abs(x / pitch - Math.round(x / pitch))).toBeLessThan(1e-6); expect(Math.abs(y / pitch - Math.round(y / pitch))).toBeLessThan(1e-6); expect(dot.x).toBeGreaterThanOrEqual(0); expect(dot.y).toBeGreaterThanOrEqual(0); expect(dot.x).toBeLessThan(area.width); expect(dot.y).toBeLessThan(area.height); } } }); it("draws dots only, no lines", () => { const path = dotPath({ x: 0, y: 0, width: 200, height: 200 }, pitch); expect(path.replace(/M[-\d.]+ [-\d.]+h0\.01/g, "")).toBe(""); }); });