2020-03-08 19:25:16 -07:00
|
|
|
import { PointerCoords } from "./types";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { normalizeScroll } from "./scene";
|
2020-02-21 08:17:20 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const getCenter = (pointers: Map<number, PointerCoords>) => {
|
2020-03-08 19:25:16 -07:00
|
|
|
const allCoords = Array.from(pointers.values());
|
2020-02-21 08:17:20 -05:00
|
|
|
return {
|
2020-03-23 13:05:07 +02:00
|
|
|
x: normalizeScroll(sum(allCoords, (coords) => coords.x) / allCoords.length),
|
|
|
|
y: normalizeScroll(sum(allCoords, (coords) => coords.y) / allCoords.length),
|
2020-02-21 08:17:20 -05:00
|
|
|
};
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-02-21 08:17:20 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const getDistance = ([a, b]: readonly PointerCoords[]) =>
|
|
|
|
Math.hypot(a.x - b.x, a.y - b.y);
|
2020-02-21 08:17:20 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const sum = <T>(array: readonly T[], mapper: (item: T) => number): number =>
|
|
|
|
array.reduce((acc, item) => acc + mapper(item), 0);
|