gesture.ts 632 B

123456789101112131415161718
  1. import { PointerCoords } from "./types";
  2. import { normalizeScroll } from "./scene";
  3. export function 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 function getDistance([a, b]: readonly PointerCoords[]) {
  11. return Math.hypot(a.x - b.x, a.y - b.y);
  12. }
  13. function sum<T>(array: readonly T[], mapper: (item: T) => number): number {
  14. return array.reduce((acc, item) => acc + mapper(item), 0);
  15. }