How to get the mouse position in html

Knowing mouse position is essential if we want to apply some wonderful effect in our web application

We can get the mouse position from mousemove event.

In the example code we are using mousemove event. This will helps us go get the mouse coordinate in real-time

In the code you will get the mouse position in the browser console as we console.log the real-time coordinated.


<!DOCTYPE html>

<body>

<script>

    function currentMousePosition(event) {
        console.log(event.clientX, event.clientY);
    }

    document.addEventListener('mousemove', currentMousePosition);
</script>
</body>
</html>



    
    
    

In our first example code we got the mouse position. Now apply some wonderful effect in this..


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mouse Particle Effect</title>
    <style>
        .particle {
            position: absolute;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: #f00;
            pointer-events: none;
        }
    </style>
</head>
<body>
    <script>
        function FrequencyManagement(func, delay) {
            let lastCalled = 0;
            return function(...args) {
                const now = new Date().getTime();
                if (now - lastCalled < delay) {
                    return;
                }
                lastCalled = now;
                func(...args);
            };
        }

        function createParticle(x, y) {
            const particle = document.createElement('div');
            particle.classList.add('particle');
            particle.style.left = `${x}px`;
            particle.style.top = `${y}px`;
            particle.style.backgroundColor = `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255})`;
            const angle = Math.random() * Math.PI * 2;
            const speed = Math.random() * 5 + 2;
            const vx = Math.cos(angle) * speed;
            const vy = Math.sin(angle) * speed;

            let distance = 0;

            const updateInterval = setInterval(() => {
                x += vx;
                y += vy;
                distance += Math.sqrt(vx * vx + vy * vy);
                particle.style.left = `${x}px`;
                particle.style.top = `${y}px`;

                if (x < 150 || x > window.innerWidth - 150 || y < window.pageYOffset + 150 || y > window.innerHeight + window.pageYOffset - 150 || distance >= 100) {
                    clearInterval(updateInterval);
                    particle.remove();
                }
            }, 30);

            document.body.appendChild(particle);
        }

        function handleMouseMovement(event) {
            const x = event.clientX + window.pageXOffset;
            const y = event.clientY + window.pageYOffset;

            createParticle(x, y);
        }

        document.addEventListener('mousemove', FrequencyManagement(handleMouseMovement, 10));
    </script>
</body>
</html>

    
    
    

Get the code from github https://github.com/01one/web-browser-mouse-coordinate

Related Posts

How to HTML open link in new in tab in browser
July 3, 2024

We can add this target=”_blank” in the <a> tag to open link in new tab Example code <!DOCTYPE html> <head> </head> <body> &lt!– Opens in the same tab –> <a href=”https://en.wikipedia.org/wiki/Main_Page”>Visit Wikipedia (same tab)</a> <br> &lt!– Opens in a new tab –> <a href=”https://en.wikipedia.org/wiki/Main_Page” target=”_blank”>Visit Wikipedia (new tab)</a> </body> </html> Copy Code

Change button text with jQuery
July 3, 2024

Its essential to change t he button text in some applications . For example Like button. If the current is “Like”, after click on the button it the button text will be “Liked” jquery makes process efficient. <!DOCTYPE html> <html> <head> <script src=”https://code.jquery.com/jquery-3.7.1.min.js”></script> </head> <body> <button class=”like-button”>Like</button> <script> $(document).ready(function(){ $(‘.like-button’).click(function(){ var buttonText = $(this).text(); if […]

How to prevent text selection in html
April 28, 2024

Its necessary to prevent text selection when the text is ui feature, to make the webpage more professional. We can prevent the text from selecting by using this in css You cannot select this text You can select this text example code