Find Tile Closest to Mouse Position

Hello! In short, I am wanting to find the closest tile to the current position of the mouse. I found this thread here:

https://forums.rpgmakerweb.com/index.php?threads/mouse-cursor-position.95309/

Specifically this post:

It's a bit complicated and depends on what exactly you want.

1. Do you want to get the position when the user clicks, or all the time?
2. Do you want to get the position relative to the game's window, or relative to the game's canvas?

1.
When the user clicks
(TouchInput.x and TouchInput.y)
Code:
document.addEventListener('mousedown', getMousePosition);

All the time
Code:
document.addEventListener('mousemove', getMousePosition);
document.addEventListener('mouseenter', getMousePosition); // In case the user doesn't move the mouse.

2.
Relative to the game's window

Code:
function getMousePosition(e) {
    var x = e.pageX;
    var y = e.pageY;
}

Relative to the game's canvas (TouchInput.x and TouchInput.y)
Code:
function getMousePosition(e) {
    var x = Graphics.pageToCanvasX(e.pageX);
    var y = Graphics.pageToCanvasY(e.pageY);
}


What's the difference between game window and game canvas?
By default, the window and the canvas have the same size. The window's size is adjusted to the canvas' size. This means that both are the same by default; our code can't detect if the mouse is on the window's title bar (which is the bar at the top, where the Close, Minimize, and Restore buttons are).

When you resize the window, it's possible that you'll see black bars (can also be white) either above and below, or left and right. Those bars belong to the window, but not the canvas. The canvas is where the game is rendered.


Edit:
I was a bit late, but I'll leave this around for the sake of completion.

But I'm not quite sure how to implement this code for what I need.

I need: When player clicks, get the closest tile position's x and y (and store it to variable)

Based on this post, do I just add something like this in a blank .js file?

document.addEventListener('mousedown', getMousePosition);
function getMousePosition(e) {
var x = Graphics.pageToCanvasX(e.pageX);
var y = Graphics.pageToCanvasY(e.pageY);
}

I'm sure this is wrong. Could someone please help me out? Thank you!
If you only need it to happen when you click, I think you can just use these to get the coordinates:

$gameMap.canvasToMapX(TouchInput.x);
$gameMap.canvasToMapY(TouchInput.y);
If you only need it to happen when you click, I think you can just use these to get the coordinates:

$gameMap.canvasToMapX(TouchInput.x);
$gameMap.canvasToMapY(TouchInput.y);
How do I output the variable data? Let's say I want to move an event to the mouse location. How do I tell the event what the x and y coordinates are of the closest tile to the mouse?
You can put those script lines in control variable and you get the number?

Or like with script:
$gameVariables.setValue(10, $gameMap.canvasToMapX(TouchInput.x));
E.g. Control Variables -> Script, then Set Event Location:

◆Control Variables:#0003 TargetX = $gameMap.canvasToMapX(TouchInput.x) ◆Control Variables:#0004 TargetY = $gameMap.canvasToMapY(TouchInput.y) ◆Set Event Location:This Event, ({TargetX},{TargetY})

Original thread: https://forums.rpgmakerweb.com/threads/find-tile-closest-to-mouse-position.168368/