gesture.ts 543 B

123456789101112131415
  1. import { PointerCoords } from "./types";
  2. export const getCenter = (pointers: Map<number, PointerCoords>) => {
  3. const allCoords = Array.from(pointers.values());
  4. return {
  5. x: sum(allCoords, (coords) => coords.x) / allCoords.length,
  6. y: sum(allCoords, (coords) => coords.y) / allCoords.length,
  7. };
  8. };
  9. export const getDistance = ([a, b]: readonly PointerCoords[]) =>
  10. Math.hypot(a.x - b.x, a.y - b.y);
  11. const sum = <T>(array: readonly T[], mapper: (item: T) => number): number =>
  12. array.reduce((acc, item) => acc + mapper(item), 0);