What is the best way to initiate an event using the onMousewheel function?

I am currently working on a WebGL application where I have a sphere object that utilizes orbit controls for zooming in and out.

My goal is to set up an event for the mousewheel so that when I zoom on the WebGL block, the corresponding map location also zooms in or out. The issue I am facing is that my event is triggered as soon as I use the mousewheel. I am unsure if my event logic is correct and would like some feedback.

root.addEventListener('mousewheel', mousewheel, false); 

function mousewheel(event) {
    var mX = (event.wheeldetailX/width)* 2 - 1;
    var mY = (event.wheeldetailY/height)* 2 + 1;

    var WebGLZoom = new THREE.Vector3(mX, mY, 1);
    var raycaster = new THREE.Raycaster();

    raycaster.setFromCamera(WebGLZoom, camera);
    WebGLZoom.sub(camera.position);

    var mapzoom = map.getZoom();

    if (WebGLZoom.z <= 5 && WebGLZoom.z > 2) {          
        map.setZoom(17);
    } else if (WebGLZoom.z <= 2 && WebGLZoom.z > 0.0) {     
        map.setZoom(19);
    } else {                
        map.setZoom(14);
    }                               
}

Answer №1

If you want to track your mousewheel movement, you can simply utilize the wheel event.

document.addEventListener('wheel', function(event){
    console.log(event.deltaY);
}, false);

By implementing this code, a console log will appear every time the mousewheel is scrolled over the document. The deltaX and deltaY properties of the MouseWheel event provide valuable information on the exact amount of scrolling and direction.

Answer №2

To implement this functionality, the recommended event to utilize is wheel. A functional demonstration can be accessed here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting nested JSON structures from one format to another using JavaScript

I have been attempting to convert an AVRO schema into an ElasticSearch index template. Both schemas are in JSON format with some specific considerations to keep in mind during the transformation process. I initially tried using recursion to extract all nes ...

Obtain Node Information in THREE.js

I'm working on a mesh using a unique shader. In the vertex shader, I am adjusting the original position of the geometry vertices. Now, my challenge is to be able to retrieve and access these new vertex positions from outside of the shader. How can I s ...

Experiencing difficulties loading webpages while attempting to execute Routes sample code using NodeJS

As a beginner in Javascript, I am attempting to execute the example code provided in the documentation for routes. The code snippet is as follows: var Router = require('routes'); var router = new Router(); router.addRoute('/admin/*?&apos ...

Execute a PHP script upon button click without the need to refresh the page

I'm facing an issue with integrating PHP and JavaScript. Objective: To execute a .php script when the event listener of the HTML button in the .js file is triggered without causing the page to reload. Expected outcome: On clicking the button, the PH ...

What is the best way to cycle through a series of lists and retrieve the URL when a specific string is found within a class of the list

I'm new to JQuery and currently experimenting on JSFiddle to improve my skills for developing a web app I have in mind. One of the requirements I need to meet is iterating through lists on the page. If a specific class contains a certain string, I sh ...

"Attempting to use a Chrome Extension to apply inline styles is not producing

Despite my attempts to search on Google, I have been unable to find a solution. I am currently working on developing a Chrome extension with the specific goal of changing/inserting the style.display property for one or more elements. This task would norma ...

Exploring the Differences Between Three.js Animation Techniques: Skeleton/Skinning and Morphing

After conducting some tests with Morph and Skeletal animation (exported from Blender), I noticed that the skeletal files are significantly smaller compared to the morph files. While this indicates that skeletal animation may be the superior choice, I am cu ...

VueJS 3 custom Checkbox fails to update UI upon clicking

I'm attempting to implement a customized checkbox using Vue 3 and the composition API based on this example. However, despite confirming through devtools that all my props and bound data are successfully passed from the parent component to the child c ...

Are web components capable of managing changes in CSS (maybe through cssChangedCallback)?

Is there a way to control the application of CSS to a web component similar to using attributes through attributeChangedCallback. I am currently developing a couple of web components that could benefit from being styled with CSS classes. However, I requir ...

Is there a way to dynamically set a database path in Express.js depending on the user's login status?

I have a backend service that serves as the primary entry point for my web application, where I want to dynamically allocate a database path based on user login. While I understand that this solution may not be scalable in the long run, I plan to use it fo ...

Endless Loop Seamless Vertical JavaScript Experience

I've been working with an HTML table structure of data and was successful in setting up a vertical list loop using some JavaScript. However, I'm facing challenges in achieving a smooth constant vertical scroll. Currently, it goes row by row when ...

What steps should I take to prompt Postman to interact with a Web API?

I have recently installed the Postman plugin for Chrome and I am curious about how to use it to call my web API. Currently, I am making an AJAX call in JavaScript with the following code: alert("Getting security token"); // Do AJAX call to get security t ...

Ways to Randomly Flip Divs

I have developed an application where all divs flip vertically on hover. I am looking for a way to make the flipping random without requiring a hover. Any ideas on how to achieve this? .vertical.flip-container { position: relative; float: left; ma ...

What causes the PHP Web server to freeze when downloading a file using AJAX?

My current situation involves: I am working on a Debian Linux system and I am looking to integrate two reporting technologies, namely PHP-Reports and JSReport. PHP-Reports allows me to retrieve data and its corresponding sub-totals using only SQL, while ...

JavaScript mouse and touch movement events (mousemove, pointermove, touchmove) are not always accurate

I'm currently working on developing a JavaScript whiteboard and have implemented the following code: let lastTimestamp = 0; const fps = 1000/60; document.addEventListener("pointermove", moveMouse, false); function moveMouse (e) { e.preve ...

The system seems to be having trouble locating the password property, as it is returning a value of

I'm currently working on a database project using MongoDB and Node.js. I'm trying to update a specific field, but unfortunately I keep getting an error. The model I am working with is called "ListaSalas": router.post('/updatesala', fun ...

What is the best way to send file data using the form-data format in Postman with React?

When I first started working with React, I encountered the need to transmit data from the frontend to the backend using an API. The data was in the form of form-data from POSTMAN, as shown in the image below. https://i.sstatic.net/U9QMe.png I opted to us ...

What is the best way to fetch all the orders from my product schema using response.send?

This is my custom Product schema const productSchema = new mongoose.Schema({ title: { type: String, required: [true, "Title is required"] }, details: { type: String, required: [true, "Details are r ...

What are the specific extensions for email validation?

code for the form: <form class="form" name ="custRegistration" id="custRegistration" onsubmit="return submitAlbum(this)" action="download.jsp" method="post" > <p class="email"> <label for="budget">Expected Budget ...

Transform Jupiter Tessellation (JT) documents into JSON format for display in THREE.js

Looking for guidance on rendering .JT files in THREE.js. Explored different options but haven't found a solution yet: Checked various loaders in Three.js but couldn't find one for JT files. Please advise if there's something I've ov ...