'How can I get the mouse position on an HTML5 canvas when canvas is fullscreen?

The following code works perfectly while the canvas is not fullscreen.

    let canvas = document.querySelector('canvas');
    let ctx = canvas.getContext('2d');

    document.addEventListener('mousemove',e=>{
        let br = canvas.getBoundingClientRect();
        let mouseX = e.clientX - br.left;
        let mouseY = e.clientY - br.top;
    });

However, after canvas.requestFullscreen() variables mouseX, mouseY do no longer represent the mouse's position on the canvas.
Is there any way to get the mouse's position relative to the canvas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source