2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2019 The noVNC Authors
4 * Licensed under MPL 2.0 or any later version (see LICENSE.txt)
7import { supportsCursorURIs, isTouchDevice } from './browser.js';
9const useFallback = !supportsCursorURIs || isTouchDevice;
11export default class Cursor {
15 this._canvas = document.createElement('canvas');
18 this._canvas.style.position = 'fixed';
19 this._canvas.style.zIndex = '65535';
20 this._canvas.style.pointerEvents = 'none';
21 // Safari on iOS can select the cursor image
22 // https://bugs.webkit.org/show_bug.cgi?id=249223
23 this._canvas.style.userSelect = 'none';
24 this._canvas.style.WebkitUserSelect = 'none';
25 // Can't use "display" because of Firefox bug #1445997
26 this._canvas.style.visibility = 'hidden';
29 this._position = { x: 0, y: 0 };
30 this._hotSpot = { x: 0, y: 0 };
32 this._eventHandlers = {
33 'mouseover': this._handleMouseOver.bind(this),
34 'mouseleave': this._handleMouseLeave.bind(this),
35 'mousemove': this._handleMouseMove.bind(this),
36 'mouseup': this._handleMouseUp.bind(this),
45 this._target = target;
48 document.body.appendChild(this._canvas);
50 const options = { capture: true, passive: true };
51 this._target.addEventListener('mouseover', this._eventHandlers.mouseover, options);
52 this._target.addEventListener('mouseleave', this._eventHandlers.mouseleave, options);
53 this._target.addEventListener('mousemove', this._eventHandlers.mousemove, options);
54 this._target.addEventListener('mouseup', this._eventHandlers.mouseup, options);
66 const options = { capture: true, passive: true };
67 this._target.removeEventListener('mouseover', this._eventHandlers.mouseover, options);
68 this._target.removeEventListener('mouseleave', this._eventHandlers.mouseleave, options);
69 this._target.removeEventListener('mousemove', this._eventHandlers.mousemove, options);
70 this._target.removeEventListener('mouseup', this._eventHandlers.mouseup, options);
72 if (document.contains(this._canvas)) {
73 document.body.removeChild(this._canvas);
80 change(rgba, hotx, hoty, w, h) {
81 if ((w === 0) || (h === 0)) {
86 this._position.x = this._position.x + this._hotSpot.x - hotx;
87 this._position.y = this._position.y + this._hotSpot.y - hoty;
88 this._hotSpot.x = hotx;
89 this._hotSpot.y = hoty;
91 let ctx = this._canvas.getContext('2d');
93 this._canvas.width = w;
94 this._canvas.height = h;
96 let img = new ImageData(new Uint8ClampedArray(rgba), w, h);
97 ctx.clearRect(0, 0, w, h);
98 ctx.putImageData(img, 0, 0);
101 this._updatePosition();
103 let url = this._canvas.toDataURL();
104 this._target.style.cursor = 'url(' + url + ')' + hotx + ' ' + hoty + ', default';
109 this._target.style.cursor = 'none';
110 this._canvas.width = 0;
111 this._canvas.height = 0;
112 this._position.x = this._position.x + this._hotSpot.x;
113 this._position.y = this._position.y + this._hotSpot.y;
118 // Mouse events might be emulated, this allows
119 // moving the cursor in such cases
120 move(clientX, clientY) {
124 // clientX/clientY are relative the _visual viewport_,
125 // but our position is relative the _layout viewport_,
126 // so try to compensate when we can
127 if (window.visualViewport) {
128 this._position.x = clientX + window.visualViewport.offsetLeft;
129 this._position.y = clientY + window.visualViewport.offsetTop;
131 this._position.x = clientX;
132 this._position.y = clientY;
134 this._updatePosition();
135 let target = document.elementFromPoint(clientX, clientY);
136 this._updateVisibility(target);
139 _handleMouseOver(event) {
140 // This event could be because we're entering the target, or
141 // moving around amongst its sub elements. Let the move handler
143 this._handleMouseMove(event);
146 _handleMouseLeave(event) {
147 // Check if we should show the cursor on the element we are leaving to
148 this._updateVisibility(event.relatedTarget);
151 _handleMouseMove(event) {
152 this._updateVisibility(event.target);
154 this._position.x = event.clientX - this._hotSpot.x;
155 this._position.y = event.clientY - this._hotSpot.y;
157 this._updatePosition();
160 _handleMouseUp(event) {
161 // We might get this event because of a drag operation that
162 // moved outside of the target. Check what's under the cursor
163 // now and adjust visibility based on that.
164 let target = document.elementFromPoint(event.clientX, event.clientY);
165 this._updateVisibility(target);
167 // Captures end with a mouseup but we can't know the event order of
168 // mouseup vs releaseCapture.
170 // In the cases when releaseCapture comes first, the code above is
173 // In the cases when the mouseup comes first, we need wait for the
174 // browser to flush all events and then check again if the cursor
175 // should be visible.
176 if (this._captureIsActive()) {
177 window.setTimeout(() => {
178 // We might have detached at this point
182 // Refresh the target from elementFromPoint since queued events
183 // might have altered the DOM
184 target = document.elementFromPoint(event.clientX,
186 this._updateVisibility(target);
192 if (this._canvas.style.visibility === 'hidden') {
193 this._canvas.style.visibility = '';
198 if (this._canvas.style.visibility !== 'hidden') {
199 this._canvas.style.visibility = 'hidden';
203 // Should we currently display the cursor?
204 // (i.e. are we over the target, or a child of the target without a
205 // different cursor set)
206 _shouldShowCursor(target) {
211 if (target === this._target) {
214 // Other part of the DOM?
215 if (!this._target.contains(target)) {
218 // Has the child its own cursor?
219 // FIXME: How can we tell that a sub element has an
220 // explicit "cursor: none;"?
221 if (window.getComputedStyle(target).cursor !== 'none') {
227 _updateVisibility(target) {
228 // When the cursor target has capture we want to show the cursor.
229 // So, if a capture is active - look at the captured element instead.
230 if (this._captureIsActive()) {
231 target = document.captureElement;
233 if (this._shouldShowCursor(target)) {
241 this._canvas.style.left = this._position.x + "px";
242 this._canvas.style.top = this._position.y + "px";
246 return document.captureElement &&
247 document.documentElement.contains(document.captureElement);