pinchAndZoom.js 1.5 KB

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