gesture.ts 620 B

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