|
| 1 | +(function() { |
| 2 | + // Phantom Limb |
| 3 | + // ------------ |
| 4 | + // http://viewinglens.com/phantom-limb |
| 5 | + // https://github.com/brian-c/phantom-limb |
| 6 | + // brian.carstensen@gmail.com |
| 7 | + |
| 8 | + "use strict"; |
| 9 | + |
| 10 | + // Default configuration |
| 11 | + var config = { |
| 12 | + style: true, |
| 13 | + startOnLoad: true |
| 14 | + }; |
| 15 | + |
| 16 | + // Apply overrides |
| 17 | + for (var param in window.phantomLimbConfig) { |
| 18 | + config[param] = window.phantomLimbConfig[param]; |
| 19 | + } |
| 20 | + |
| 21 | + // Keep track of whether the mouse is down. |
| 22 | + var mouseIsDown = false; |
| 23 | + |
| 24 | + // A Finger is a representation on the screen. |
| 25 | + // It keeps track of its position and the node that it's over. |
| 26 | + function Finger() { |
| 27 | + this.node = document.createElement('span'); |
| 28 | + this.node.classList.add('_phantom-limb_finger'); |
| 29 | + this.place(); |
| 30 | + } |
| 31 | + |
| 32 | + Finger.prototype = { |
| 33 | + node: null, |
| 34 | + |
| 35 | + x: NaN, |
| 36 | + y: NaN, |
| 37 | + |
| 38 | + target: null, |
| 39 | + |
| 40 | + place: function() { |
| 41 | + document.body.appendChild(this.node); |
| 42 | + }, |
| 43 | + |
| 44 | + hide: function() { |
| 45 | + this.node.style.display = 'none'; |
| 46 | + }, |
| 47 | + |
| 48 | + show: function() { |
| 49 | + this.node.style.display = ''; |
| 50 | + }, |
| 51 | + |
| 52 | + move: function(x, y) { |
| 53 | + if (isNaN(x) || isNaN(y)) { |
| 54 | + this.hide(); |
| 55 | + this.target = null; |
| 56 | + } else { |
| 57 | + this.show(); |
| 58 | + |
| 59 | + this.node.style.left = x + 'px'; |
| 60 | + this.node.style.top = y + 'px'; |
| 61 | + |
| 62 | + this.x = x; |
| 63 | + this.y = y; |
| 64 | + |
| 65 | + if (!mouseIsDown) this.target = document.elementFromPoint(x, y); |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + // We'll instantiate the fingers when we start. |
| 71 | + var fingers = []; |
| 72 | + |
| 73 | + // Create a synthetic event from a real event and a finger. |
| 74 | + function createMouseEvent(eventName, originalEvent, finger) { |
| 75 | + var e = document.createEvent('MouseEvent'); |
| 76 | + |
| 77 | + e.initMouseEvent(eventName, true, true, |
| 78 | + originalEvent.view, originalEvent.detail, |
| 79 | + finger.x || originalEvent.screenX, finger.y || originalEvent.screenY, |
| 80 | + finger.x || originalEvent.clientX, finger.y || originalEvent.clientY, |
| 81 | + originalEvent.ctrlKey, originalEvent.shiftKey, |
| 82 | + originalEvent.altKey, originalEvent.metaKey, |
| 83 | + originalEvent.button, finger.target || originalEvent.relatedTarget |
| 84 | + ); |
| 85 | + |
| 86 | + e.synthetic = true; |
| 87 | + |
| 88 | + // Set this so we can match shared targets later. |
| 89 | + e._finger = finger; |
| 90 | + |
| 91 | + return e; |
| 92 | + } |
| 93 | + |
| 94 | + var startDistance = NaN; |
| 95 | + var startAngle = NaN; |
| 96 | + |
| 97 | + // Given a mouse event, fire a touch event for each finger. |
| 98 | + // Add the appropriate touch-specific event properties. |
| 99 | + function fireTouchEvents(eventName, originalEvent) { |
| 100 | + // All touch events, including "touchend". |
| 101 | + var events = []; |
| 102 | + var gestures = []; |
| 103 | + |
| 104 | + // For each finger with a target, create a touch event. |
| 105 | + fingers.forEach(function(finger) { |
| 106 | + if (!finger.target) return; |
| 107 | + |
| 108 | + // Convert "ontouch*" properties and attributes to listeners. |
| 109 | + var onEventName = 'on' + eventName; |
| 110 | + |
| 111 | + if (onEventName in finger.target) { |
| 112 | + console.warn('Converting `' + onEventName + '` property to event listener.', finger.target); |
| 113 | + finger.target.addEventListener(eventName, finger.target[onEventName], false); |
| 114 | + delete finger.target[onEventName]; |
| 115 | + } |
| 116 | + |
| 117 | + if (finger.target.hasAttribute(onEventName)) { |
| 118 | + console.warn('Converting `' + onEventName + '` attribute to event listener.', finger.target); |
| 119 | + var handler = new Function('event', finger.target.getAttribute(onEventName)); |
| 120 | + finger.target.addEventListener(eventName, handler, false); |
| 121 | + finger.target.removeAttribute(onEventName); |
| 122 | + } |
| 123 | + |
| 124 | + // Set up a new event with the coordinates of the finger. |
| 125 | + var touch = createMouseEvent(eventName, originalEvent, finger); |
| 126 | + |
| 127 | + events.push(touch); |
| 128 | + }); |
| 129 | + |
| 130 | + // Figure out scale and rotation. |
| 131 | + if (events.length > 1) { |
| 132 | + var x = events[0].pageX - events[1].pageX; |
| 133 | + var y = events[0].pageY - events[1].pageY; |
| 134 | + |
| 135 | + var distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); |
| 136 | + var angle = Math.atan2(x, y) * (180 / Math.PI); |
| 137 | + |
| 138 | + var gestureName = 'gesturechange'; |
| 139 | + |
| 140 | + if (eventName === 'touchstart') { |
| 141 | + gestureName = 'gesturestart'; |
| 142 | + startDistance = distance; |
| 143 | + startAngle = angle; |
| 144 | + } |
| 145 | + |
| 146 | + if (eventName === 'touchend') gestureName = 'gestureend'; |
| 147 | + |
| 148 | + events.forEach(function(event) { |
| 149 | + var gesture = createMouseEvent(gestureName, event, event._finger); |
| 150 | + gestures.push(gesture); |
| 151 | + }); |
| 152 | + |
| 153 | + events.concat(gestures).forEach(function(event) { |
| 154 | + event.scale = distance / startDistance; |
| 155 | + event.rotation = startAngle - angle; |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + // Loop through the events array and fill in each touch array. |
| 160 | + events.forEach(function(touch) { |
| 161 | + touch.touches = events.filter(function(e) { |
| 162 | + return ~e.type.indexOf('touch') && e.type !== 'touchend'; |
| 163 | + }); |
| 164 | + |
| 165 | + touch.changedTouches = events.filter(function(e) { |
| 166 | + return ~e.type.indexOf('touch') && e._finger.target === touch._finger.target; |
| 167 | + }); |
| 168 | + |
| 169 | + touch.targetTouches = touch.changedTouches.filter(function(e) { |
| 170 | + return ~e.type.indexOf('touch') && e.type !== 'touchend'; |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + // Then fire the events. |
| 175 | + events.concat(gestures).forEach(function(event, i) { |
| 176 | + event.identifier = i; |
| 177 | + event._finger.target.dispatchEvent(event); |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | + // Prevent all mousedown event from doing anything. |
| 182 | + // We'll fire one manually at touchend. |
| 183 | + function phantomTouchStart(e) { |
| 184 | + if (e.synthetic) return; |
| 185 | + |
| 186 | + mouseIsDown = true; |
| 187 | + |
| 188 | + e.preventDefault(); |
| 189 | + e.stopPropagation(); |
| 190 | + |
| 191 | + fireTouchEvents('touchstart', e); |
| 192 | + } |
| 193 | + |
| 194 | + // The center between two fingers |
| 195 | + var centerX = NaN; |
| 196 | + var centerY = NaN; |
| 197 | + |
| 198 | + // Set each finger's position target. |
| 199 | + // Pressing alt engages the second finger. |
| 200 | + // Pressing shift locks the second finger's position relative to the first's. |
| 201 | + function moveFingers(e) { |
| 202 | + // We'll use this if the second is locked with the first. |
| 203 | + var changeX = e.clientX - fingers[0].x || 0; |
| 204 | + var changeY = e.clientY - fingers[0].y || 0; |
| 205 | + |
| 206 | + // The first finger just follows the mouse. |
| 207 | + fingers[0].move(e.clientX, e.clientY); |
| 208 | + |
| 209 | + // TODO: Determine modifier keys independent of mouse movement. |
| 210 | + |
| 211 | + if (e.altKey) { |
| 212 | + // Reset the center. |
| 213 | + if (!centerX && !centerY) { |
| 214 | + centerX = innerWidth / 2; |
| 215 | + centerY = innerHeight / 2; |
| 216 | + } |
| 217 | + |
| 218 | + // Lock the center with the first finger. |
| 219 | + if (e.shiftKey) { |
| 220 | + centerX += changeX; |
| 221 | + centerY += changeY; |
| 222 | + } |
| 223 | + |
| 224 | + var secondX = centerX + (centerX - e.clientX); |
| 225 | + var secondY = centerY + (centerY - e.clientY); |
| 226 | + |
| 227 | + fingers[1].move(secondX, secondY); |
| 228 | + } else { |
| 229 | + // Disengage the second finger. |
| 230 | + fingers[1].move(NaN, NaN); |
| 231 | + |
| 232 | + // Reset the center next time the alt key is held. |
| 233 | + centerX = NaN; |
| 234 | + centerY = NaN; |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + // Prevent all mousemove events from firing. |
| 239 | + // We'll fire one (and only one) manually at touchend. |
| 240 | + function phantomTouchMove(e) { |
| 241 | + if (e.synthetic) return; |
| 242 | + |
| 243 | + e.preventDefault(); |
| 244 | + e.stopPropagation(); |
| 245 | + |
| 246 | + moveFingers(e); |
| 247 | + |
| 248 | + if (mouseIsDown) { |
| 249 | + fireTouchEvents('touchmove', e); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + // Prevent all mouseup events from firing. |
| 254 | + // We'll fire one manually at touchend. |
| 255 | + function phantomTouchEnd(e) { |
| 256 | + if (e.synthetic) return; |
| 257 | + |
| 258 | + mouseIsDown = false; |
| 259 | + |
| 260 | + e.preventDefault(); |
| 261 | + e.stopPropagation(); |
| 262 | + |
| 263 | + fireTouchEvents('touchend', e); |
| 264 | + |
| 265 | + fingers.forEach(function(finger) { |
| 266 | + if (!finger.target) return; |
| 267 | + |
| 268 | + // Mobile Safari moves all the mouse event to fire after the touchend event. |
| 269 | + finger.target.dispatchEvent(createMouseEvent('mouseover', e, finger)); |
| 270 | + finger.target.dispatchEvent(createMouseEvent('mousemove', e, finger)); |
| 271 | + finger.target.dispatchEvent(createMouseEvent('mousedown', e, finger)); |
| 272 | + |
| 273 | + // TODO: These two only fire if content didn't change. How can we tell? |
| 274 | + finger.target.dispatchEvent(createMouseEvent('mouseup', e, finger)); |
| 275 | + finger.target.dispatchEvent(createMouseEvent('click', e, finger)); |
| 276 | + }); |
| 277 | + } |
| 278 | + |
| 279 | + // Prevent clicks. We'll handle them manually. |
| 280 | + function phantomClick(e) { |
| 281 | + if (e.synthetic) return; |
| 282 | + |
| 283 | + e.preventDefault(); |
| 284 | + e.stopPropagation(); |
| 285 | + } |
| 286 | + |
| 287 | + // Not entirely proud of this, but I can't serve CSS from GitHub |
| 288 | + // and I want the bookmarklet to be as simple as possible. |
| 289 | + var defaultCSS = ([ |
| 290 | + '._phantom-limb,', |
| 291 | + '._phantom-limb a {', |
| 292 | + 'cursor: none !important;', |
| 293 | + '}', |
| 294 | + '._phantom-limb_finger {', |
| 295 | + 'background: rgba(128, 128, 128, 0.5);', |
| 296 | + 'border: 2px solid rgb(128, 128, 128);', |
| 297 | + 'border-radius: 50%;', |
| 298 | + 'display: none;', |
| 299 | + 'height: 44px;', |
| 300 | + 'margin: -22px 0 0 -22px;', |
| 301 | + 'pointer-events: none;', |
| 302 | + 'position: fixed;', |
| 303 | + 'width: 44px;', |
| 304 | + 'z-index: 999999' + |
| 305 | + '}', |
| 306 | + '._phantom-limb ._phantom-limb_finger {', |
| 307 | + 'display: block;', |
| 308 | + '}' |
| 309 | + ]).join('\n'); |
| 310 | + |
| 311 | + if (config.style) { |
| 312 | + var styleTag = document.createElement('style'); |
| 313 | + styleTag.id = '_phantom-limb_default-style'; |
| 314 | + styleTag.innerHTML = defaultCSS; |
| 315 | + document.querySelector('head').appendChild(styleTag); |
| 316 | + } |
| 317 | + |
| 318 | + // On/off switch |
| 319 | + function start() { |
| 320 | + if (fingers.length === 0) fingers.push(new Finger(), new Finger()); |
| 321 | + |
| 322 | + document.addEventListener('mousedown', phantomTouchStart, true); |
| 323 | + document.addEventListener('mousemove', phantomTouchMove, true); |
| 324 | + document.addEventListener('mouseup', phantomTouchEnd, true); |
| 325 | + document.addEventListener('click', phantomClick, true); |
| 326 | + |
| 327 | + document.documentElement.classList.add('_phantom-limb'); |
| 328 | + } |
| 329 | + |
| 330 | + function stop() { |
| 331 | + document.removeEventListener('mousedown', phantomTouchStart, true); |
| 332 | + document.removeEventListener('mousemove', phantomTouchMove, true); |
| 333 | + document.removeEventListener('mouseup', phantomTouchEnd, true); |
| 334 | + document.removeEventListener('click', phantomClick, true); |
| 335 | + |
| 336 | + document.documentElement.classList.remove('_phantom-limb'); |
| 337 | + } |
| 338 | + |
| 339 | + // Detect keyup, exit when esc. |
| 340 | + function phantomKeyUp(e) { |
| 341 | + if (e.keyCode === 27) { |
| 342 | + if (document.documentElement.classList.contains('_phantom-limb')) { |
| 343 | + stop(); |
| 344 | + } else { |
| 345 | + start(); |
| 346 | + } |
| 347 | + } |
| 348 | + } |
| 349 | + |
| 350 | + var phantomLimb = { |
| 351 | + start: start, |
| 352 | + stop: stop |
| 353 | + }; |
| 354 | + |
| 355 | + if (typeof define === 'function' && define.amd) define(phantomLimb); |
| 356 | + if (typeof module !== 'undefined') module.exports = phantomLimb; |
| 357 | + window.phantomLimb = phantomLimb; |
| 358 | + |
| 359 | + document.addEventListener('keyup', phantomKeyUp, false); |
| 360 | + |
| 361 | + if (config.startOnLoad) addEventListener('DOMContentLoaded', start, false); |
| 362 | +}()); |
0 commit comments