pinchAndZoom.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pinchOrZoom = void 0;
  4. var ds = function (ab) { return (ab[0] <= 0 && ab[1] >= 0) || (ab[0] >= 0 && ab[1] <= 0); };
  5. var sign = function (x) { return (x < 0 ? -1 : 1); };
  6. var pinchOrZoom = function (event, cache) {
  7. if (!event.changedTouches) {
  8. return false;
  9. }
  10. if (event.touches.length === 2) {
  11. var oldPoints = [cache[event.touches[0].identifier], cache[event.touches[1].identifier]];
  12. var newPoints = [event.touches[0], event.touches[1]];
  13. if (oldPoints[0] && oldPoints[1]) {
  14. // Calculate the difference between the start and move coordinates
  15. var diffx = [oldPoints[0].clientX - newPoints[0].clientX, oldPoints[1].clientX - newPoints[1].clientX];
  16. var diffy = [oldPoints[0].clientY - newPoints[0].clientY, oldPoints[1].clientY - newPoints[1].clientY];
  17. console.log(diffx, diffy);
  18. if (ds(diffx) || ds(diffy)) {
  19. return {
  20. action: 'zoom',
  21. };
  22. }
  23. var mx = Math.max(Math.abs(diffx[0]), Math.abs(diffx[1]));
  24. var my = Math.max(Math.abs(diffy[0]), Math.abs(diffy[1]));
  25. return {
  26. action: 'pinch',
  27. coords: [mx * sign(diffx[0]), my * sign(diffx[1])],
  28. };
  29. }
  30. }
  31. Array.from(event.changedTouches).forEach(function (touch) { return (cache[touch.identifier] = touch); });
  32. return {
  33. action: 'move',
  34. coords: [event.changedTouches[0].clientX, event.changedTouches[0].clientY],
  35. };
  36. };
  37. exports.pinchOrZoom = pinchOrZoom;