| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | import { supportsCursorURIs, isTouchDevice } from './browser.js'; |
| 8 | |
| 9 | const useFallback = !supportsCursorURIs || isTouchDevice; |
| 10 | |
| 11 | export default class Cursor { |
| 12 | constructor(fallback = 'none') { |
| 13 | this._target = null; |
| 14 | this._fallback = fallback === 'default' ? 'default' : 'none'; |
| 15 | |
| 16 | this._canvas = document.createElement('canvas'); |
| 17 | |
| 18 | if (useFallback) { |
| 19 | this._canvas.style.position = 'fixed'; |
| 20 | this._canvas.style.zIndex = '65535'; |
| 21 | this._canvas.style.pointerEvents = 'none'; |
| 22 | |
| 23 | |
| 24 | this._canvas.style.userSelect = 'none'; |
| 25 | this._canvas.style.WebkitUserSelect = 'none'; |
| 26 | |
| 27 | this._canvas.style.visibility = 'hidden'; |
| 28 | } |
| 29 | |
| 30 | this._position = { x: 0, y: 0 }; |
| 31 | this._hotSpot = { x: 0, y: 0 }; |
| 32 | |
| 33 | this._eventHandlers = { |
| 34 | 'mouseover': this._handleMouseOver.bind(this), |
| 35 | 'mouseleave': this._handleMouseLeave.bind(this), |
| 36 | 'mousemove': this._handleMouseMove.bind(this), |
| 37 | 'mouseup': this._handleMouseUp.bind(this), |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | attach(target) { |
| 42 | if (this._target) { |
| 43 | this.detach(); |
| 44 | } |
| 45 | |
| 46 | this._target = target; |
| 47 | |
| 48 | if (useFallback) { |
| 49 | document.body.appendChild(this._canvas); |
| 50 | |
| 51 | const options = { capture: true, passive: true }; |
| 52 | this._target.addEventListener('mouseover', this._eventHandlers.mouseover, options); |
| 53 | this._target.addEventListener('mouseleave', this._eventHandlers.mouseleave, options); |
| 54 | this._target.addEventListener('mousemove', this._eventHandlers.mousemove, options); |
| 55 | this._target.addEventListener('mouseup', this._eventHandlers.mouseup, options); |
| 56 | } |
| 57 | |
| 58 | this.clear(); |
| 59 | } |
| 60 | |
| 61 | detach() { |
| 62 | if (!this._target) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | if (useFallback) { |
| 67 | const options = { capture: true, passive: true }; |
| 68 | this._target.removeEventListener('mouseover', this._eventHandlers.mouseover, options); |
| 69 | this._target.removeEventListener('mouseleave', this._eventHandlers.mouseleave, options); |
| 70 | this._target.removeEventListener('mousemove', this._eventHandlers.mousemove, options); |
| 71 | this._target.removeEventListener('mouseup', this._eventHandlers.mouseup, options); |
| 72 | |
| 73 | if (document.contains(this._canvas)) { |
| 74 | document.body.removeChild(this._canvas); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | this._target = null; |
| 79 | } |
| 80 | |
| 81 | change(rgba, hotx, hoty, w, h) { |
| 82 | if ((w === 0) || (h === 0)) { |
| 83 | this.clear(); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | this._position.x = this._position.x + this._hotSpot.x - hotx; |
| 88 | this._position.y = this._position.y + this._hotSpot.y - hoty; |
| 89 | this._hotSpot.x = hotx; |
| 90 | this._hotSpot.y = hoty; |
| 91 | |
| 92 | let ctx = this._canvas.getContext('2d'); |
| 93 | |
| 94 | this._canvas.width = w; |
| 95 | this._canvas.height = h; |
| 96 | |
| 97 | let img = new ImageData(new Uint8ClampedArray(rgba), w, h); |
| 98 | ctx.clearRect(0, 0, w, h); |
| 99 | ctx.putImageData(img, 0, 0); |
| 100 | |
| 101 | if (useFallback) { |
| 102 | this._updatePosition(); |
| 103 | } else { |
| 104 | let url = this._canvas.toDataURL(); |
| 105 | this._target.style.cursor = 'url(' + url + ')' + hotx + ' ' + hoty + ', default'; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | clear() { |
| 110 | this._target.style.cursor = this._fallback; |
| 111 | this._canvas.width = 0; |
| 112 | this._canvas.height = 0; |
| 113 | this._position.x = this._position.x + this._hotSpot.x; |
| 114 | this._position.y = this._position.y + this._hotSpot.y; |
| 115 | this._hotSpot.x = 0; |
| 116 | this._hotSpot.y = 0; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | move(clientX, clientY) { |
| 122 | if (!useFallback) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | if (window.visualViewport) { |
| 129 | this._position.x = clientX + window.visualViewport.offsetLeft; |
| 130 | this._position.y = clientY + window.visualViewport.offsetTop; |
| 131 | } else { |
| 132 | this._position.x = clientX; |
| 133 | this._position.y = clientY; |
| 134 | } |
| 135 | this._updatePosition(); |
| 136 | let target = document.elementFromPoint(clientX, clientY); |
| 137 | this._updateVisibility(target); |
| 138 | } |
| 139 | |
| 140 | _handleMouseOver(event) { |
| 141 | |
| 142 | |
| 143 | |
| 144 | this._handleMouseMove(event); |
| 145 | } |
| 146 | |
| 147 | _handleMouseLeave(event) { |
| 148 | |
| 149 | this._updateVisibility(event.relatedTarget); |
| 150 | } |
| 151 | |
| 152 | _handleMouseMove(event) { |
| 153 | this._updateVisibility(event.target); |
| 154 | |
| 155 | this._position.x = event.clientX - this._hotSpot.x; |
| 156 | this._position.y = event.clientY - this._hotSpot.y; |
| 157 | |
| 158 | this._updatePosition(); |
| 159 | } |
| 160 | |
| 161 | _handleMouseUp(event) { |
| 162 | |
| 163 | |
| 164 | |
| 165 | let target = document.elementFromPoint(event.clientX, event.clientY); |
| 166 | this._updateVisibility(target); |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | if (this._captureIsActive()) { |
| 178 | window.setTimeout(() => { |
| 179 | |
| 180 | if (!this._target) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | target = document.elementFromPoint(event.clientX, |
| 186 | event.clientY); |
| 187 | this._updateVisibility(target); |
| 188 | }, 0); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | _showCursor() { |
| 193 | if (this._canvas.style.visibility === 'hidden') { |
| 194 | this._canvas.style.visibility = ''; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | _hideCursor() { |
| 199 | if (this._canvas.style.visibility !== 'hidden') { |
| 200 | this._canvas.style.visibility = 'hidden'; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | _shouldShowCursor(target) { |
| 208 | if (!target) { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | if (target === this._target) { |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | if (!this._target.contains(target)) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | FIXME: |
| 221 | |
| 222 | if (window.getComputedStyle(target).cursor !== 'none') { |
| 223 | return false; |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | _updateVisibility(target) { |
| 229 | |
| 230 | |
| 231 | if (this._captureIsActive()) { |
| 232 | target = document.captureElement; |
| 233 | } |
| 234 | if (this._shouldShowCursor(target)) { |
| 235 | this._showCursor(); |
| 236 | } else { |
| 237 | this._hideCursor(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | _updatePosition() { |
| 242 | this._canvas.style.left = this._position.x + "px"; |
| 243 | this._canvas.style.top = this._position.y + "px"; |
| 244 | } |
| 245 | |
| 246 | _captureIsActive() { |
| 247 | return document.captureElement && |
| 248 | document.documentElement.contains(document.captureElement); |
| 249 | } |
| 250 | } |