Skip to content

Instantly share code, notes, and snippets.

@akoGit
Created January 3, 2024 23:56
Show Gist options
  • Save akoGit/3090bc1e2de2ec9322bd8ce39df6e1bd to your computer and use it in GitHub Desktop.
Save akoGit/3090bc1e2de2ec9322bd8ce39df6e1bd to your computer and use it in GitHub Desktop.

Revisions

  1. akoGit created this gist Jan 3, 2024.
    35 changes: 35 additions & 0 deletions Pointer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    export default class Pointer {
    constructor() {
    if (instance) return instance

    this.px = 0
    this.py = 0

    instance = this
    this.init()
    }

    static getInstance() {
    if (!instance) {
    instance = new Pointer()
    }
    return instance
    }

    static get x() {
    return Pointer.getInstance().px
    }

    static get y() {
    return Pointer.getInstance().py
    }

    init() {
    document.addEventListener('pointermove', this.onPointerMove.bind(this))
    }

    onPointerMove(e) {
    this.px = e.pageX
    this.py = e.pageY
    }
    }