react-zoom-pan-pinch
Version:
Zoom and pan html elements in easy way.
50 lines (43 loc) • 1.49 kB
text/typescript
import { StateType } from "models";
/** Drop binary float noise on scale (e.g. 1.5000000000000002) without clipping real zoom values. */
export const roundScaleForTransform = (scale: number): number =>
Number.parseFloat(scale.toFixed(8));
export const getTransformStyles = (
x: number,
y: number,
scale: number,
): string => {
// Standard translate prevents blurry svg on the safari
const s = roundScaleForTransform(scale);
return `translate(${x}px, ${y}px) scale(${s})`;
};
export const getMatrixTransformStyles = (
x: number,
y: number,
scale: number,
): string => {
// The shorthand for matrix does not work for Safari hence the need to explicitly use matrix3d
// Refer to https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix
const a = scale;
const b = 0;
const c = 0;
const d = scale;
const tx = x;
const ty = y;
return `matrix3d(${a}, ${b}, 0, 0, ${c}, ${d}, 0, 0, 0, 0, 1, 0, ${tx}, ${ty}, 0, 1)`;
};
export const getCenterPosition = (
scale: number,
wrapperComponent: HTMLDivElement,
contentComponent: HTMLDivElement,
): StateType => {
const contentWidth = contentComponent.offsetWidth * scale;
const contentHeight = contentComponent.offsetHeight * scale;
const centerPositionX = (wrapperComponent.offsetWidth - contentWidth) / 2;
const centerPositionY = (wrapperComponent.offsetHeight - contentHeight) / 2;
return {
scale,
positionX: centerPositionX,
positionY: centerPositionY,
};
};