Shift in the mouse's placement

While experimenting with drawing a box using raycast to track mouse position in the scene, I encountered an issue. Upon clicking, dragging, and releasing the mouse to create the box, I noticed that its position was shifting due to the canvas container's distance on the x and y axes.

An image below illustrates the problem:

https://i.sstatic.net/Fw8Sz.png

The red mark indicates the starting point where I clicked to begin drawing the box, while the blue one shows where I released the mouse. The shift is evident on the Y-axis as I have two rows of components at the top of the page.

Here is some sample code:

<body>

<div>
    <button class="btn btn-info" onclick="rotate_x()"><b>X</b></button>
    <button class="btn btn-info" onclick="rotate_y()"><b>Y</b></button>
    <button class="btn btn-info" onclick="rotate_z()"><b>Z</b></button>
    <button class="btn btn-info" onclick="reset_rotation()"><b>Reset</b></button>
    <input type="checkbox" name="selection" onchange="selection_mode_change()" checked><b>Selection mode</b>
</div>


<div>
    <button class="btn btn-info" onclick="rotate_x()"><b>X</b></button>
    <button class="btn btn-info" onclick="rotate_y()"><b>Y</b></button>
    <button class="btn btn-info" onclick="rotate_z()"><b>Z</b></button>
    <button class="btn btn-info" onclick="reset_rotation()"><b>Reset</b></button>
    <input type="checkbox" name="selection" onchange="selection_mode_change()" checked><b>Selection mode</b>
</div>

I'm unsure why this issue is occurring or how to resolve it. Any guidance would be appreciated. Thank you.

Answer №1

Upon closer inspection of the image, it becomes apparent that the mouse clicks are slightly off due to a Y-axis offset in screen pixels. Additionally, upon further examination, it seems that this offset matches the header of your XYZ/Selection mode. This discrepancy is a result of creating a div and setting its size to innerWidth/innerHeight, causing the div to be positioned lower than expected. Consequently, mouse clicks may not align correctly with their intended targets. Threejs registers these clicks at 40px on screen, translating them to 40px on the div.

TROUBLESHOOTING TIPS: In my experience dealing with a similar issue, I utilized JQuery to retrieve the TOP value and applied that offset to adjust my raycaster mouseclicks. In your situation, you could potentially access renderer.domElement.top (confirm by checking console.log(renderer.domElement) for a top location value) and subtract it from the mouse Y coordinate (subtracting to shift the rectangle upward).

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

Tips for dynamically updating the URL based on a query string within the URL

Here is a table generated by GridView: <table id="ctl00_centerContent_GridView1"> <tr> <th scope="col"> <!-- COLUMN INFORMATION --> </th> </tr> <tr> ...

The onmouseover function is unresponsive

I am in the process of creating a basic hud that is intended to display itself when the mouse hovers over the main panel. However, I am encountering an issue where the onmouseover and onmouseout events are not functioning as expected. Upon inspecting the ...

Enhancing Your Website Listings with Firebase: A Step-by-Step Guide to Updating

I am currently working on an admin panel for a website, utilizing Firebase as the backend database. I have successfully implemented the listings display feature, however, I'm facing an issue where clicking on a specific listing should change its statu ...

I'm puzzled as to why I have to encode the path parameter twice in order for the REST call to properly handle special characters

I have a challenge with a REST call that fetches a product's ingredients using the product name as the path variable. I allow for any character to be used, and I know I need to use encodeURIComponent(name) to encode characters such as "/" to prevent m ...

Using VueJS Single File Components can cause issues with the example code for "Custom Popups" in the Google Maps API

Trying to implement a unique Google Maps popup following the provided documentation. After copying and pasting the official Google example code into a VueJS jsFiddle, the custom marker functions correctly as intended. It remains in the designated area eve ...

Troubleshooting: Style sheets not loading on a page rendered by node

Today was my first attempt at learning Node.js and creating a basic server to display a single page with a header tag. However, I encountered an error stating that the CSS file could not be loaded. This is the code I used: const express = require('ex ...

Clicking on the Node.js subscription button sends me to the local host 127.0.0.1

I'm encountering an issue where clicking the subscribe button redirects my page to 127.0.0.1 and nothing appears in the console.log. Can anyone help me understand why this is happening? HTML <!DOCTYPE html> <html lang="en"> <head&g ...

Is it possible to combine file and form data into a single API request in Angular 4 when uploading?

admin.component.ts admin.component.html Here is a snippet of the code used for uploading form data. Although I am able to post the form data to the API, I am facing difficulties with uploading the file. admin.component.ts private onFileChange(event: an ...

Utilize MaterialUI Grid to define custom styles for the ::after pseudo-element

I came across a helpful article on Stack Overflow about Flex-box and how to align the last row to the grid. I'm interested in implementing it in my project: .grid::after { content: ""; flex: auto; } However, I'm not sure how to inc ...

Content not refreshing when closing and reopening Highslide iframe

I am encountering an issue with Highslide where I am unable to reopen a popup with new content. Even though the contentId remains the same, the content is not being updated when the popup is reopened. Below is the code snippet that showcases the problem: ...

Sending JSON data along with sendFile() in Node.js and Express done right

After setting up a Node.js server with Express and routing, I am faced with the challenge of passing JSON data onto a specific page ("/users/id") while using sendFile(). While I could make an AJAX request on page load to retrieve the data separately, I am ...

Is there a term in JavaScript that denotes an object that can be serialized into JSON format?

Can you help me find a term for basic objects that accentuates their simplicity? Particularly, objects that do not reference themselves and do not have any methods or bindings (i.e. JSON-serializable). The terms I am currently using are: "flat object" " ...

Check the status of the audio source URL using JavaScript

I am currently live streaming audio to my player using the Soundcloud API. <audio></aidio> <source src="soundcloud-track-url"></source> Within my code, I have added an onerror eventListener for both the <audio> and <sourc ...

How to Shift Focus to a Component in Vue.js

Within a container, I have a form section enclosed by a component that remains hidden due to the use of v-if. Upon clicking a button, the boolean value is toggled, revealing the previously concealed component. At this point, I aim to shift focus to the ini ...

Using Electron to redirect to a different HTML file while passing GET variables

I am currently working on developing an Electron app. Within the project files, for example, I have app.html. My goal is to send GET variables like ?id=54&q=asd to the receiver.html, where I can then retrieve these id and q variables. As we all know, ...

Trouble updating Kendo DropDown in Internet Explorer

Having an issue with updating a Kendo DropDownList through a javascript function. It works fine in FireFox and Chrome, but not in Internet Explorer. @(Html.Kendo().DropDownList() .Name("myDDL") .HtmlAttributes(new { style = "width: 320px" }) . ...

Connect to the MongoDB database running on localhost using the mongoose library

I am currently learning about the MEAN stack through this helpful tutorial. However, the tutorial assumes a connection to a remote mongodb installation. I have MongoDB already set up and running on my CentOS7 localhost. To modify the mongoose connect line ...

Concealing fragments produced through manipulation of gl_PointSize

I am attempting to conceal some particles created with points so that they do not appear in certain areas of my scene. However, I am struggling to perfect the fragment shader for this task. Here is what I have thus far: uniform sampler2D tDiffuse; uniform ...

Using Typescript to remove an element from an array inside another array

I've encountered an issue while trying to remove a specific item from a nested array of items within another array. Below is the code snippet: removeFromOldFeatureGroup() { for( let i= this.featureGroups.length-1; i>=0; i--) { if( this.featureGr ...

Generate responsive elements using Bootstrap dynamically

I'm having success dynamically generating bootstrap elements in my project, except for creating a drop-down menu. ColdFusion is the language I am using to implement these div elements: <div class="panel panel-primary"><div class="panel-head ...