wealth/packages/island/src/organisms/lightbox/utils.ts

25 lines
788 B
TypeScript

import type { PointerEvent } from 'react';
import { ZOOM_STEPS } from './constants';
import type { LightboxPoint } from './types';
export function pointOf(event: PointerEvent): LightboxPoint {
return { x: event.clientX, y: event.clientY };
}
/** The distance between two fingers; never 0, so a pinch ratio stays finite. */
export function spread(a: LightboxPoint, b: LightboxPoint): number {
return Math.max(Math.hypot(a.x - b.x, a.y - b.y), 1);
}
/** The `ZOOM_STEPS` index closest to `scale`, where a pinch lands when it lets go. */
export function nearestZoomStep(scale: number): number {
let nearest = 0;
ZOOM_STEPS.forEach((step, index) => {
if (Math.abs(step - scale) < Math.abs(ZOOM_STEPS[nearest] - scale)) {
nearest = index;
}
});
return nearest;
}