promotion image of download ymail app
Promoted

In Javascript How Do You Make Mouseclick Coordinates Global?

I'm writing a javascript where I want the x and y coordinates of a mouseclick to be global, so that I can use them in any of the functions I define in the script.

The problem is that to get the coordinates in the first place, I have to first make a mousedown event, and then define a function within that event. I have to immediately define the function that will get the coordinates based on the previous event.

So the x and y coordinates are only defined within that function and not globally scoped.

How do I make it global? Or how do I make it accessible from any other part of the script?

3 Answers

Relevance
  • 2 months ago
    Favorite Answer

    Define global variables for the mouse status, eg. x, y, button state.

    Then update those from within the mousedown & mouseup etc. functions.

    The rest of your code can access the global storage at any time.

    • Commenter avatarLogin to reply the answers
  • Chris
    Lv 7
    2 months ago

    This has nothing to do with events or mousedown or whatever. All you need is a bunch of variables you can access from anywhere, and all you need to do is to declare them outside any function.

    let mx, my; // some global variables

    function whatever() {

      mx = 4; // set mx

    }

    function doStuff() {

      console.log(mx); // read mx

    }

    • Commenter avatarLogin to reply the answers
  • Anonymous
    2 months ago

    The documentary Tiger King can answer this

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.