Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): make a wheel notch the same fraction of the view at any scale

Elian Doran61770d612603
diff --git a/apps/client/src/widgets/react/ImageViewer.tsx b/apps/client/src/widgets/react/ImageViewer.tsx
index 29bde1c4c4..d849ddfa3b 100644
--- a/apps/client/src/widgets/react/ImageViewer.tsx
+++ b/apps/client/src/widgets/react/ImageViewer.tsx
@@ -143,7 +143,7 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.
maxScale={maxScale}
centerOnInit
centerZoomedOut
- wheel={{ step: 0.0085 }}
+ wheel={zoom.wheel}
autoAlignment={{ disabled: true }}
doubleClick={{ mode: "reset" }}
onTransform={zoom.onTransform}
diff --git a/apps/client/src/widgets/react/zoom_pan.spec.ts b/apps/client/src/widgets/react/zoom_pan.spec.ts
index c098548658..4ef5937a4c 100644
--- a/apps/client/src/widgets/react/zoom_pan.spec.ts
+++ b/apps/client/src/widgets/react/zoom_pan.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { zoomStep } from "./zoom_pan";
+import { wheelStep, zoomStep } from "./zoom_pan";
describe("zoomStep", () => {
it("is the increment react-zoom-pan-pinch needs for a ×1.2 step, at any scale", () => {
@@ -19,3 +19,21 @@ describe("zoomStep", () => {
expect(zoomedIn - zoomStep(zoomedIn, "out")).toBeCloseTo(3);
});
});
+
+describe("wheelStep", () => {
+ it("makes a notch the same tenth of the view at any scale", () => {
+ // The library adds `step * |deltaY|` to the scale, so a notch is asserted where it lands.
+ const notch = (scale: number, deltaY = 100) => scale + wheelStep(scale) * deltaY;
+
+ expect(notch(0.5)).toBeCloseTo(0.55);
+ expect(notch(8)).toBeCloseTo(8.8);
+
+ // The library's own default step would have put that first notch on 2.0 — a diagram fitted
+ // at 50% jumping past 200% on one scroll.
+ expect(0.5 + 0.015 * 100).toBe(2);
+ expect(notch(0.5)).toBeLessThan(0.6);
+
+ // A trackpad reports a fraction of a notch, and moves the view by that same fraction.
+ expect(notch(1, 10)).toBeCloseTo(1.01);
+ });
+});
diff --git a/apps/client/src/widgets/react/zoom_pan.ts b/apps/client/src/widgets/react/zoom_pan.ts
index 957680febb..6b1d2f0f30 100644
--- a/apps/client/src/widgets/react/zoom_pan.ts
+++ b/apps/client/src/widgets/react/zoom_pan.ts
@@ -38,6 +38,7 @@ export function useZoomPanPinch({ minScale, maxScale, resetOn, onScaleChange }:
ref,
scale,
onTransform,
+ wheel: { step: wheelStep(scale) },
canZoomIn: scale < maxScale * (1 - ZOOM_LIMIT_TOLERANCE),
canZoomOut: scale > minScale * (1 + ZOOM_LIMIT_TOLERANCE),
zoomIn: () => ref.current?.zoomIn(zoomStep(currentScale(ref), "in")),
@@ -48,6 +49,13 @@ export function useZoomPanPinch({ minScale, maxScale, resetOn, onScaleChange }:
/** What one press of a zoom step multiplies the scale by. */
const ZOOM_STEP = 1.2;
+/**
+ * What one wheel notch multiplies the scale by — gentler than a press, a notch being easy to repeat
+ * and easy to overshoot with.
+ */
+const WHEEL_STEP = 1.1;
+/** The `deltaY` a wheel notch reports where it reports a whole one; a trackpad reports far less. */
+const WHEEL_NOTCH_DELTA = 100;
/**
* How near a bound counts as being on it. The zoom animation reaches its target by adding the whole
* difference back to the scale it started from, which can land a float's width short of the bound.
@@ -67,6 +75,22 @@ export function zoomStep(scale: number, direction: "in" | "out") {
return direction === "in" ? scale * (ZOOM_STEP - 1) : scale * (1 - 1 / ZOOM_STEP);
}
+/**
+ * The `wheel.step` that makes a notch a {@link WHEEL_STEP}, at the scale the content is drawn at.
+ *
+ * react-zoom-pan-pinch adds `step * |deltaY|` to the current scale (see `handleWheelZoom`), so its
+ * own default of 0.015 is a flat +1.5 a notch: from a diagram fitted at 0.5 one notch reaches 2.0,
+ * while at 8 the same notch is barely a twelfth of what is on screen. Scaling the step by the scale
+ * it applies to keeps a notch the same fraction of the view wherever it is taken from.
+ *
+ * The scale is the one last reported through `onTransform`, so a scroll fast enough to outrun a
+ * render takes a notch or two sized for where it just was. The error is a fraction of one notch and
+ * corrects itself on the next.
+ */
+export function wheelStep(scale: number) {
+ return (scale * (WHEEL_STEP - 1)) / WHEEL_NOTCH_DELTA;
+}
+
/** The scale the instance is at now, read as a button is pressed rather than when it was drawn. */
function currentScale(ref: RefObject<ReactZoomPanPinchRef>) {
return ref.current?.instance?.state?.scale ?? 1;
diff --git a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx
index 68c48eb85e..b2a6685848 100644
--- a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx
+++ b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx
@@ -151,6 +151,7 @@ export default function SvgSplitEditor({ ntxId, note, attachmentTitle, renderSvg
maxScale={MAX_ZOOM}
centerOnInit
centerZoomedOut
+ wheel={zoom.wheel}
doubleClick={{ mode: "reset" }}
onTransform={zoom.onTransform}
>