excalidraw/src/points.ts

31 lines
948 B
TypeScript
Raw Normal View History

2020-03-17 11:01:11 -07:00
import { Point } from "./types";
export const getSizeFromPoints = (points: readonly Point[]) => {
const xs = points.map((point) => point[0]);
const ys = points.map((point) => point[1]);
2020-03-17 11:01:11 -07:00
return {
width: Math.max(...xs) - Math.min(...xs),
height: Math.max(...ys) - Math.min(...ys),
};
};
/** @arg dimension, 0 for rescaling only x, 1 for y */
export const rescalePoints = (
2020-03-17 11:01:11 -07:00
dimension: 0 | 1,
newSize: number,
points: readonly Point[],
): Point[] => {
const coordinates = points.map((point) => point[dimension]);
const maxCoordinate = Math.max(...coordinates);
const minCoordinate = Math.min(...coordinates);
const size = maxCoordinate - minCoordinate;
const scale = size === 0 ? 1 : newSize / size;
return points.map((point): Point => {
const newCoordinate = point[dimension] * scale;
const newPoint = [...point];
newPoint[dimension] = newCoordinate;
return newPoint as unknown as Point;
});
};