Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(geomap): coordinate rulers hanging far from the origin
Elian Doran6837fa727e69
diff --git a/apps/client/src/widgets/collections/geomap/ImageRulers.spec.ts b/apps/client/src/widgets/collections/geomap/ImageRulers.spec.tsindex 6a0f59cc2d..08eccab1dc 100644--- a/apps/client/src/widgets/collections/geomap/ImageRulers.spec.ts+++ b/apps/client/src/widgets/collections/geomap/ImageRulers.spec.ts@@ -34,6 +34,13 @@ describe("axisTicks", () => {const fine = axisTicks(0, 1, 1000, 1);expect(fine.map(({ text }) => text)).toEqual([ "0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1" ]);});++ it("ends for an axis far from zero compared to its step", () => {+ // A step of 1e-7 at 1e9 puts the first tick's index past 2^53, where adding one changes nothing.+ const ticks = axisTicks(1e9, 1e9 + 1e-6, 1000, 1);+ expect(ticks.length).toBeGreaterThan(0);+ expect(ticks.length).toBeLessThanOrEqual(12);+ }, 2000);});describe("rulerFeatures", () => {diff --git a/apps/client/src/widgets/collections/geomap/ImageRulers.tsx b/apps/client/src/widgets/collections/geomap/ImageRulers.tsxindex 76e9e4c785..b7c81e6d9f 100644--- a/apps/client/src/widgets/collections/geomap/ImageRulers.tsx+++ b/apps/client/src/widgets/collections/geomap/ImageRulers.tsx@@ -17,6 +17,8 @@ const LABEL_SPACING_PX = 90;const TICK_LENGTH_PX = 6;/** The gap between a tick's end and its label, in screen pixels. */const LABEL_GAP_PX = 3;+/** The most ticks along one axis, far more than fit on a screen at the 90-pixel spacing. */+const MAX_TICKS = 1000;/*** Coordinate rulers along an image map's edges, in the map's own coordinates (see@@ -190,8 +192,11 @@ export function axisTicks(from: number, to: number, pixels: number, screenPerImaconst ticks: { value: number; text: string }[] = [];// A hair of slack, so an edge that falls on a round value keeps its tick despite rounding.const slack = step * 1e-9;- for (let i = Math.ceil((low - slack) / step); i * step <= high + slack; i++) {- const value = Number((i * step).toFixed(decimals));+ const first = Math.ceil((low - slack) / step) * step;+ // Counted from the first tick, since far from zero `first / step + 1` can round back to itself.+ const count = Math.min(Math.floor((high + slack - first) / step), MAX_TICKS);+ for (let i = 0; i <= count; i++) {+ const value = Number((first + i * step).toFixed(decimals));// `+ 0` turns a negative zero into a plain one, which would otherwise print as "-0".ticks.push({ value, text: String(value + 0) });}