www.xbdev.net
xbdev - software development
Saturday April 19, 2025
Home | Contact | Support | JavaScript... so much power in such a few lines of code..
     
 

JavaScript...

so much power in such a few lines of code..

 


Javascript > Controlling Mouse (Mouse Input/Movement)


Simple JavaScript program that tracks mouse movement and displays both local and global coordinates in the top left corner of the screen.

This HTML file contains a `<div>` element with the id `coordinates`, which will display the coordinates. The JavaScript code listens for the `mousemove` event on the document and updates the coordinates accordingly. It calculates both the local coordinates relative to the viewport (clientX and clientY) and the global coordinates relative to the entire document (pageX and pageY), then updates the content of the `coordinatesDiv` element with these values.


<!DOCTYPE html>
<
html lang="en">
<
head>
<
meta charset="UTF-8">
<
meta name="viewport" content="width=device-width, initial-scale=1.0">
<
title>Mouse Coordinates</title>
<
style>
    
#coordinates {
        
positionfixed;
        
top10px;
        
left10px;
        
background-colorrgba(0000.5);
        
colorwhite;
        
padding5px;
        
border-radius5px;
    }
</
style>
</
head>
<
body>

<
div id="coordinates"></div>

<
script>
    
document.addEventListener("DOMContentLoaded", function() {
        const 
coordinatesDiv document.getElementById('coordinates');

        function 
updateCoordinates(event) {
            const 
localX event.clientX;
            const 
localY event.clientY;
            const 
globalX event.pageX;
            const 
globalY event.pageY;

            
coordinatesDiv.innerText = `Local: (${localX}${localY}) Global: (${globalX}${globalY})`;
        }

        
document.addEventListener('mousemove'updateCoordinates);
    });
</
script>

</
body>
</
html>


Things to Try


• Display the 'speed' of the mouse (as you move it around the screen store the past few positions and calculate an average - how many pixels between updates)
• Adding a 'div' with 'position:absolute' style - so it can be moved anywhere on screen - then add a border the div (draw a box around it and make it a bit bigger than then cursor) - then have it drawn around the mouse cursor (update it as the cursor moves around)
• In addition to the local and global mouse position - give the mouse button data (if any buttons are pressed)
















101 WebGPU Programming Projects. WebGPU Development Pixels - coding fragment shaders from post processing to ray tracing! WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Games WGSL 2d 3d interactive web-based fun learning WebGPU Compute WebGPU API - Owners WebGPU Development Cookbook - coding recipes for all your webgpu needs! WebGPU & WGSL Essentials: A Hands-On Approach to Interactive Graphics, Games, 2D Interfaces, 3D Meshes, Animation, Security and Production Kenwright graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu for dummies kenwright webgpu wgsl compute graphics all in one kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright WebGPU Shader Language Development: Vertex, Fragment, Compute Shaders for Programmers Kenwright WGSL Fundamentals book kenwright WebGPU Data Visualization Cookbook kenwright Special Effects Programming with WebGPU kenwright WebGPU Programming Guide: Interactive Graphics and Compute Programming with WebGPU & WGSL kenwright Ray-Tracing with WebGPU kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.