|
| 1 | +(function () { |
| 2 | + const canvas = document.getElementById("bevy-canvas"); |
| 3 | + |
| 4 | + const emitTouchGesture = (gesture) => { |
| 5 | + window.dispatchEvent( |
| 6 | + new CustomEvent("vizmat-touch-gesture", { |
| 7 | + detail: gesture, |
| 8 | + }), |
| 9 | + ); |
| 10 | + }; |
| 11 | + |
| 12 | + const toVec = (touch) => ({ x: touch.clientX, y: touch.clientY }); |
| 13 | + |
| 14 | + const touchPanState = { |
| 15 | + one: null, |
| 16 | + two: { |
| 17 | + active: false, |
| 18 | + midpoint: null, |
| 19 | + distance: null, |
| 20 | + }, |
| 21 | + }; |
| 22 | + |
| 23 | + const getMidpoint = (a, b) => ({ |
| 24 | + x: (a.x + b.x) * 0.5, |
| 25 | + y: (a.y + b.y) * 0.5, |
| 26 | + }); |
| 27 | + |
| 28 | + const getDistance = (a, b) => { |
| 29 | + const dx = a.x - b.x; |
| 30 | + const dy = a.y - b.y; |
| 31 | + return Math.hypot(dx, dy); |
| 32 | + }; |
| 33 | + |
| 34 | + const clearTouchState = () => { |
| 35 | + touchPanState.one = null; |
| 36 | + touchPanState.two = { |
| 37 | + active: false, |
| 38 | + midpoint: null, |
| 39 | + distance: null, |
| 40 | + }; |
| 41 | + }; |
| 42 | + |
| 43 | + if (canvas) { |
| 44 | + canvas.style.touchAction = "none"; |
| 45 | + |
| 46 | + const pointerState = new Map(); |
| 47 | + |
| 48 | + const removePointer = (event) => { |
| 49 | + if (event.pointerType !== "touch") return; |
| 50 | + if (!event.currentTarget || !pointerState.has(event.pointerId)) return; |
| 51 | + pointerState.delete(event.pointerId); |
| 52 | + |
| 53 | + if (pointerState.size === 0) { |
| 54 | + clearTouchState(); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (pointerState.size === 1) { |
| 59 | + touchPanState.one = [...pointerState.values()][0]; |
| 60 | + touchPanState.two = { |
| 61 | + active: false, |
| 62 | + midpoint: null, |
| 63 | + distance: null, |
| 64 | + }; |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const points = [...pointerState.values()]; |
| 69 | + touchPanState.two = { |
| 70 | + active: true, |
| 71 | + midpoint: getMidpoint(points[0], points[1]), |
| 72 | + distance: getDistance(points[0], points[1]), |
| 73 | + }; |
| 74 | + }; |
| 75 | + |
| 76 | + const updateTwoPointer = (a, b) => { |
| 77 | + if (!touchPanState.two.active) { |
| 78 | + touchPanState.two = { |
| 79 | + active: true, |
| 80 | + midpoint: getMidpoint(a, b), |
| 81 | + distance: getDistance(a, b), |
| 82 | + }; |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const midpoint = getMidpoint(a, b); |
| 87 | + const distance = getDistance(a, b); |
| 88 | + const panDx = midpoint.x - touchPanState.two.midpoint.x; |
| 89 | + const panDy = midpoint.y - touchPanState.two.midpoint.y; |
| 90 | + const scaleDelta = touchPanState.two.distance > 0 |
| 91 | + ? (distance - touchPanState.two.distance) / touchPanState.two.distance |
| 92 | + : 0; |
| 93 | + |
| 94 | + if (panDx !== 0 || panDy !== 0 || scaleDelta !== 0) { |
| 95 | + emitTouchGesture({ |
| 96 | + gesture: "TwoFinger", |
| 97 | + x: panDx, |
| 98 | + y: panDy, |
| 99 | + scale_delta: scaleDelta, |
| 100 | + }); |
| 101 | + touchPanState.two.midpoint = midpoint; |
| 102 | + touchPanState.two.distance = distance; |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + canvas.addEventListener( |
| 107 | + "pointerdown", |
| 108 | + (event) => { |
| 109 | + if (event.pointerType !== "touch") return; |
| 110 | + if (!event.target) return; |
| 111 | + const point = toVec(event); |
| 112 | + pointerState.set(event.pointerId, point); |
| 113 | + event.currentTarget.setPointerCapture(event.pointerId); |
| 114 | + const x = point.x; |
| 115 | + const y = point.y; |
| 116 | + emitTouchGesture({ |
| 117 | + gesture: "OneFingerDown", |
| 118 | + x, |
| 119 | + y, |
| 120 | + scale_delta: 0, |
| 121 | + }); |
| 122 | + touchPanState.two = { |
| 123 | + active: false, |
| 124 | + midpoint: null, |
| 125 | + distance: null, |
| 126 | + }; |
| 127 | + event.preventDefault(); |
| 128 | + }, |
| 129 | + { passive: false }, |
| 130 | + ); |
| 131 | + |
| 132 | + canvas.addEventListener( |
| 133 | + "pointermove", |
| 134 | + (event) => { |
| 135 | + if (event.pointerType !== "touch") return; |
| 136 | + if (!pointerState.has(event.pointerId)) return; |
| 137 | + |
| 138 | + const point = toVec(event); |
| 139 | + pointerState.set(event.pointerId, point); |
| 140 | + |
| 141 | + if (pointerState.size === 1) { |
| 142 | + if (!touchPanState.one) { |
| 143 | + touchPanState.one = point; |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + const x = point.x; |
| 148 | + const y = point.y; |
| 149 | + emitTouchGesture({ |
| 150 | + gesture: "OneFingerMove", |
| 151 | + x, |
| 152 | + y, |
| 153 | + scale_delta: 0, |
| 154 | + }); |
| 155 | + } else if (pointerState.size >= 2) { |
| 156 | + const points = [...pointerState.values()]; |
| 157 | + const a = points[0]; |
| 158 | + const b = points[1]; |
| 159 | + updateTwoPointer(a, b); |
| 160 | + } |
| 161 | + |
| 162 | + event.preventDefault(); |
| 163 | + }, |
| 164 | + { passive: false }, |
| 165 | + ); |
| 166 | + |
| 167 | + canvas.addEventListener( |
| 168 | + "pointerup", |
| 169 | + removePointer, |
| 170 | + { passive: false }, |
| 171 | + ); |
| 172 | + canvas.addEventListener("pointercancel", removePointer, { |
| 173 | + passive: false, |
| 174 | + }); |
| 175 | + canvas.addEventListener("pointerleave", removePointer, { |
| 176 | + passive: false, |
| 177 | + }); |
| 178 | + canvas.addEventListener("pointerout", removePointer, { passive: false }); |
| 179 | + canvas.addEventListener("pointerlostcapture", removePointer); |
| 180 | + } |
| 181 | +})(); |
0 commit comments