Skip to content

Build your personal knowledge base with Trilium Notes

client/image viewer: switch to crisp image rendering when scaling beyond 4x

Adorian Doranac844f75fb36
diff --git a/apps/client/src/widgets/react/ImageViewer.css b/apps/client/src/widgets/react/ImageViewer.css
index 5095bc3adc..5e5cfe866a 100644
--- a/apps/client/src/widgets/react/ImageViewer.css
+++ b/apps/client/src/widgets/react/ImageViewer.css
@@ -24,3 +24,8 @@
.image-viewer-viewport.pannable.panning {
cursor: grabbing;
}
+
+/* At high zoom, render crisp edges instead of blurred interpolation. */
+.image-viewer-viewport.tn-image-large-zoom img {
+ image-rendering: crisp-edges;
+}
diff --git a/apps/client/src/widgets/react/ImageViewer.spec.tsx b/apps/client/src/widgets/react/ImageViewer.spec.tsx
index 4b50b2a3cb..8fe8f6ad69 100644
--- a/apps/client/src/widgets/react/ImageViewer.spec.tsx
+++ b/apps/client/src/widgets/react/ImageViewer.spec.tsx
@@ -12,7 +12,7 @@ vi.mock("react-zoom-pan-pinch", () => ({
TransformComponent: (props: { children?: ComponentChildren }) => props.children
}));
-import ImageViewer from "./ImageViewer";
+import ImageViewer, { evaluateImageZoom } from "./ImageViewer";
function renderViewer(props: Parameters<typeof ImageViewer>[0]) {
const container = document.createElement("div");
@@ -51,3 +51,31 @@ describe("ImageViewer", () => {
expect(props.maxScale).toBe(8);
});
});
+
+describe("evaluateImageZoom", () => {
+ const img = (naturalWidth: number, clientWidth: number) => ({ naturalWidth, clientWidth });
+
+ it("is pannable only once zoomed past the fitted size", () => {
+ expect(evaluateImageZoom(0.5, img(800, 800)).pannable).toBe(false);
+ expect(evaluateImageZoom(1, img(800, 800)).pannable).toBe(false);
+ expect(evaluateImageZoom(1.5, img(800, 800)).pannable).toBe(true);
+ });
+
+ it("goes crisp only past 4x native for a downscaled image", () => {
+ // native 4000 fitted to 800 => 1x native at scale 5, exactly 4x native at scale 20
+ expect(evaluateImageZoom(5, img(4000, 800)).largeZoom).toBe(false);
+ expect(evaluateImageZoom(20, img(4000, 800)).largeZoom).toBe(false);
+ expect(evaluateImageZoom(21, img(4000, 800)).largeZoom).toBe(true);
+ });
+
+ it("goes crisp past 4x scale for an image shown at native size", () => {
+ // native 200 shown at 200 => exactly 4x native at scale 4
+ expect(evaluateImageZoom(4, img(200, 200)).largeZoom).toBe(false);
+ expect(evaluateImageZoom(5, img(200, 200)).largeZoom).toBe(true);
+ });
+
+ it("never goes crisp when the image is missing or not yet loaded", () => {
+ expect(evaluateImageZoom(50, null).largeZoom).toBe(false);
+ expect(evaluateImageZoom(50, img(0, 800)).largeZoom).toBe(false);
+ });
+});
diff --git a/apps/client/src/widgets/react/ImageViewer.tsx b/apps/client/src/widgets/react/ImageViewer.tsx
index e967bd8eff..538395ea15 100644
--- a/apps/client/src/widgets/react/ImageViewer.tsx
+++ b/apps/client/src/widgets/react/ImageViewer.tsx
@@ -1,7 +1,7 @@
import "./ImageViewer.css";
import { Ref } from "preact";
-import { useState } from "preact/hooks";
+import { useRef, useState } from "preact/hooks";
import { type ReactZoomPanPinchRef, TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";
interface ImageViewerProps {
@@ -15,6 +15,21 @@ interface ImageViewerProps {
apiRef?: Ref<ReactZoomPanPinchRef>;
}
+/** Beyond this multiple of the image's native resolution, switch to crisp (non-smoothed) rendering. */
+const CRISP_NATIVE_SCALE = 4;
+
+/**
+ * Derives the zoom-driven flags: whether the image is pannable (zoomed past the fitted size) and
+ * whether it's enlarged beyond {@link CRISP_NATIVE_SCALE}× its native resolution. `clientWidth` is
+ * the un-transformed fitted width, so `clientWidth * scale / naturalWidth` is the on-screen size in
+ * multiples of the image's real pixels.
+ */
+export function evaluateImageZoom(scale: number, img: { naturalWidth: number; clientWidth: number } | null) {
+ const pannable = scale > 1;
+ const nativeScale = img && img.naturalWidth > 0 ? (img.clientWidth * scale) / img.naturalWidth : 0;
+ return { pannable, largeZoom: nativeScale > CRISP_NATIVE_SCALE };
+}
+
/**
* Interactive image viewer: the image is fit to the viewport on load, then the user can zoom
* (wheel/pinch/buttons) and pan (drag). Double-clicking resets back to the fitted view.
@@ -22,6 +37,15 @@ interface ImageViewerProps {
export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.5, maxScale = 50, apiRef }: ImageViewerProps) {
const [ pannable, setPannable ] = useState(false);
const [ panning, setPanning ] = useState(false);
+ const [ largeZoom, setLargeZoom ] = useState(false);
+ const imgRef = useRef<HTMLImageElement>(null);
+
+ const wrapperClass = [
+ "image-viewer-viewport",
+ pannable && "pannable",
+ panning && "panning",
+ largeZoom && "tn-image-large-zoom"
+ ].filter(Boolean).join(" ");
return (
<TransformWrapper
@@ -33,12 +57,16 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.
wheel={{ step: 0.0085 }}
autoAlignment={{ disabled: true }}
doubleClick={{ mode: "reset" }}
- onTransform={(_ref, { scale }) => setPannable(scale > 1)}
+ onTransform={(_ref, { scale }) => {
+ const { pannable, largeZoom } = evaluateImageZoom(scale, imgRef.current);
+ setPannable(pannable);
+ setLargeZoom(largeZoom);
+ }}
onPanningStart={() => setPanning(true)}
onPanningStop={() => setPanning(false)}
>
- <TransformComponent wrapperClass={`image-viewer-viewport${pannable ? " pannable" : ""}${panning ? " panning" : ""}`} contentClass="image-viewer-content">
- <img className={imgClassName} src={src} alt={alt} />
+ <TransformComponent wrapperClass={wrapperClass} contentClass="image-viewer-content">
+ <img ref={imgRef} className={imgClassName} src={src} alt={alt} />
</TransformComponent>
</TransformWrapper>
);