From 4b322c4b4414a9f11a88c342f3875c8b54910214 Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Wed, 18 Oct 2017 17:35:15 -0700 Subject: [PATCH] register cursor event listeners on canvas, not window --- src/components/cursor.js | 23 +++++++++++++---------- tests/components/cursor.test.js | 4 ++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/components/cursor.js b/src/components/cursor.js index 485ed8ff114..520d35644be 100644 --- a/src/components/cursor.js +++ b/src/components/cursor.js @@ -150,22 +150,25 @@ module.exports.Component = registerComponent('cursor', { }); el.removeEventListener('raycaster-intersection', this.onIntersection); el.removeEventListener('raycaster-intersection-cleared', this.onIntersectionCleared); - window.removeEventListener('mousemove', this.onMouseMove); - window.removeEventListener('touchstart', this.onMouseMove); - window.removeEventListener('touchmove', this.onMouseMove); - window.removeEventListener('resize', this.updateCanvasBounds); + canvas.removeEventListener('mousemove', this.onMouseMove); + canvas.removeEventListener('touchstart', this.onMouseMove); + canvas.removeEventListener('touchmove', this.onMouseMove); + canvas.removeEventListener('resize', this.updateCanvasBounds); }, updateMouseEventListeners: function () { + var canvas; var el = this.el; - window.removeEventListener('mousemove', this.onMouseMove); - window.removeEventListener('touchstart', this.onMouseMove); - window.removeEventListener('touchmove', this.onMouseMove); + + canvas = el.sceneEl.canvas; + canvas.removeEventListener('mousemove', this.onMouseMove); + canvas.removeEventListener('touchstart', this.onMouseMove); + canvas.removeEventListener('touchmove', this.onMouseMove); el.setAttribute('raycaster', 'useWorldCoordinates', false); if (this.data.rayOrigin !== 'mouse') { return; } - window.addEventListener('mousemove', this.onMouseMove, false); - window.addEventListener('touchstart', this.onMouseMove, false); - window.addEventListener('touchmove', this.onMouseMove, false); + canvas.addEventListener('mousemove', this.onMouseMove, false); + canvas.addEventListener('touchstart', this.onMouseMove, false); + canvas.addEventListener('touchmove', this.onMouseMove, false); el.setAttribute('raycaster', 'useWorldCoordinates', true); this.updateCanvasBounds(); }, diff --git a/tests/components/cursor.test.js b/tests/components/cursor.test.js index 3a02fd9040e..bb9f3dd4173 100644 --- a/tests/components/cursor.test.js +++ b/tests/components/cursor.test.js @@ -363,7 +363,7 @@ suite('cursor', function () { event.clientX = 5; event.clientY = 5; el.setAttribute('cursor', 'rayOrigin', 'mouse'); - window.dispatchEvent(event); + el.sceneEl.canvas.dispatchEvent(event); process.nextTick(function () { var raycaster = el.getAttribute('raycaster'); assert.notEqual(raycaster.direction.x, 0); @@ -375,7 +375,7 @@ suite('cursor', function () { var event = new CustomEvent('touchstart'); event.touches = {item: function () { return {clientX: 5, clientY: 5}; }}; el.setAttribute('cursor', 'rayOrigin', 'mouse'); - window.dispatchEvent(event); + el.sceneEl.canvas.dispatchEvent(event); process.nextTick(function () { var raycaster = el.getAttribute('raycaster'); assert.notEqual(raycaster.direction.x, 0);