refactor(web): expose scaled image dimensions from AdaptiveImage

Change-Id: Iae105fb749525739ba8df5b944a73ea66a6a6964
refactor/adaptive-image-dimensions
Min Idzelis 2026-06-03 03:55:18 +00:00
parent 92841f311f
commit a29dc703b6
2 changed files with 24 additions and 9 deletions

View File

@ -74,6 +74,8 @@
onError?: () => void; onError?: () => void;
ref?: HTMLDivElement; ref?: HTMLDivElement;
imgRef?: HTMLImageElement; imgRef?: HTMLImageElement;
imgNaturalSize?: Size;
imgScaledSize?: Size;
backdrop?: Snippet; backdrop?: Snippet;
overlays?: Snippet; overlays?: Snippet;
}; };
@ -82,6 +84,10 @@
ref = $bindable(), ref = $bindable(),
// eslint-disable-next-line no-useless-assignment // eslint-disable-next-line no-useless-assignment
imgRef = $bindable(), imgRef = $bindable(),
// eslint-disable-next-line no-useless-assignment
imgNaturalSize = $bindable(),
// eslint-disable-next-line no-useless-assignment
imgScaledSize = $bindable(),
asset, asset,
sharedLink, sharedLink,
objectFit = 'contain', objectFit = 'contain',
@ -149,10 +155,22 @@
return { width: 1, height: 1 }; return { width: 1, height: 1 };
}); });
$effect(() => {
imgNaturalSize = imageDimensions;
});
const scaledDimensions = $derived.by(() => {
const scaleFn = objectFit === 'cover' ? scaleToCover : scaleToFit;
return scaleFn(imageDimensions, container);
});
$effect(() => {
imgScaledSize = scaledDimensions;
});
const { insetInlineStart, top, displayWidth, displayHeight, rasterWidth, rasterHeight, rasterScale } = $derived.by( const { insetInlineStart, top, displayWidth, displayHeight, rasterWidth, rasterHeight, rasterScale } = $derived.by(
() => { () => {
const scaleFn = objectFit === 'cover' ? scaleToCover : scaleToFit; const { width, height } = scaledDimensions;
const { width, height } = scaleFn(imageDimensions, container);
if (maxRasterPixels === 0) { if (maxRasterPixels === 0) {
return { return {
insetInlineStart: (container.width - width) / 2 + 'px', insetInlineStart: (container.width - width) / 2 + 'px',

View File

@ -13,7 +13,7 @@
import { SlideshowLook, SlideshowState, slideshowStore } from '$lib/stores/slideshow.store'; import { SlideshowLook, SlideshowState, slideshowStore } from '$lib/stores/slideshow.store';
import { handlePromiseError } from '$lib/utils'; import { handlePromiseError } from '$lib/utils';
import { canCopyImageToClipboard, copyImageToClipboard } from '$lib/utils/asset-utils'; import { canCopyImageToClipboard, copyImageToClipboard } from '$lib/utils/asset-utils';
import { getNaturalSize, scaleToFit, type Size } from '$lib/utils/container-utils'; import type { Size } from '$lib/utils/container-utils';
import { handleError } from '$lib/utils/handle-error'; import { handleError } from '$lib/utils/handle-error';
import { getOcrBoundingBoxes } from '$lib/utils/ocr-utils'; import { getOcrBoundingBoxes } from '$lib/utils/ocr-utils';
import { getBoundingBox, type BoundingBox } from '$lib/utils/people-utils'; import { getBoundingBox, type BoundingBox } from '$lib/utils/people-utils';
@ -67,13 +67,9 @@
height: containerHeight, height: containerHeight,
}); });
const overlaySize = $derived.by((): Size => { let scaledDimensions = $state<Size>({ width: 0, height: 0 });
if (!assetViewerManager.imgRef || !visibleImageReady) {
return { width: 0, height: 0 };
}
return scaleToFit(getNaturalSize(assetViewerManager.imgRef), { width: containerWidth, height: containerHeight }); const overlaySize = $derived(visibleImageReady ? scaledDimensions : { width: 0, height: 0 });
});
const highlightedBoxes = $derived(getBoundingBox(assetViewerManager.highlightedFaces, overlaySize)); const highlightedBoxes = $derived(getBoundingBox(assetViewerManager.highlightedFaces, overlaySize));
const isHighlighting = $derived(highlightedBoxes.length > 0); const isHighlighting = $derived(highlightedBoxes.length > 0);
@ -235,6 +231,7 @@
onReady?.(); onReady?.();
}} }}
bind:imgRef={assetViewerManager.imgRef} bind:imgRef={assetViewerManager.imgRef}
bind:imgScaledSize={scaledDimensions}
bind:ref={adaptiveImage} bind:ref={adaptiveImage}
> >
{#snippet backdrop()} {#snippet backdrop()}