This code will give me the x and y value of the click in pixels:
document.getElementById("game").addEventListener("click", function(event) {
console.log(event.clientX, event.clientY);
});
But how do I get the X and Y values comparitive to VH (Viewport Height) and VW (Viewport Width) (I want to get the value like how in CSS you can set an element to be 20vh [20% of viewport height] etc etc)
You first need to get the Viewport Width (vw) by using
window.innerWidthand Viewport Height (vh) by usingwindow.innerHeight.Then, divide the
event.clientXbyvwand multiply by100to get value in percentages. Same logic to get Y co-ordinates, i.e. divide theevent.clientYbyvhand multiply by100to get value in percentages.See my demo below: (Click on Game to get the co-ordinates)