[javascript] How to get the mouse position without events (without moving the mouse)?

Riffing on @SuperNova's answer, here's an approach using ES6 classes that keeps the context for this correct in your callback:

_x000D_
_x000D_
class Mouse {_x000D_
  constructor() {_x000D_
    this.x = 0;_x000D_
    this.y = 0;_x000D_
    this.callbacks = {_x000D_
      mouseenter: [],_x000D_
      mousemove: [],_x000D_
    };_x000D_
  }_x000D_
_x000D_
  get xPos() {_x000D_
    return this.x;_x000D_
  }_x000D_
_x000D_
  get yPos() {_x000D_
    return this.y;_x000D_
  }_x000D_
_x000D_
  get position() {_x000D_
    return `${this.x},${this.y}`;_x000D_
  }_x000D_
_x000D_
  addListener(type, callback) {_x000D_
    document.addEventListener(type, this); // Pass `this` as the second arg to keep the context correct_x000D_
    this.callbacks[type].push(callback);_x000D_
  }_x000D_
_x000D_
  // `handleEvent` is part of the browser's `EventListener` API._x000D_
  // https://developer.mozilla.org/en-US/docs/Web/API/EventListener/handleEvent_x000D_
  handleEvent(event) {_x000D_
    const isMousemove = event.type === 'mousemove';_x000D_
    const isMouseenter = event.type === 'mouseenter';_x000D_
_x000D_
    if (isMousemove || isMouseenter) {_x000D_
      this.x = event.pageX;_x000D_
      this.y = event.pageY;_x000D_
    }_x000D_
_x000D_
    this.callbacks[event.type].forEach((callback) => {_x000D_
      callback();_x000D_
    });_x000D_
  }_x000D_
}_x000D_
_x000D_
const mouse = new Mouse();_x000D_
_x000D_
mouse.addListener('mouseenter', () => console.log('mouseenter', mouse.position));_x000D_
mouse.addListener('mousemove', () => console.log('mousemove A', mouse.position));_x000D_
mouse.addListener('mousemove', () => console.log('mousemove B', mouse.position));
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to mouseevent

How do you Hover in ReactJS? - onMouseLeave not registered during fast hover over When to choose mouseover() and hover() function? Pygame mouse clicking detection change cursor to finger pointer How to completely DISABLE any MOUSE CLICK How do I add a .click() event to an image? jQuery get mouse position within an element How to simulate a click by using x,y coordinates in JavaScript? How to close a web page on a button click, a hyperlink or a link button click? How to get the mouse position without events (without moving the mouse)?

Examples related to dom-events

Detecting real time window size changes in Angular 4 Does Enter key trigger a click event? What are passive event listeners? Stop mouse event propagation React onClick function fires on render How do you Hover in ReactJS? - onMouseLeave not registered during fast hover over iFrame onload JavaScript event addEventListener, "change" and option selection Automatically pass $event with ng-click? JavaScript click event listener on class