By double clicking, three.js has the ability to dynamically create objects

My website features stunning 3D graphics created using three.js.

There are numerous square boxes on the site, and users have the ability to add even more boxes.

By double-clicking anywhere on the scene, a box will be dynamically added at that specific point (the mouse cursor's location).

Check out my code snippet below:

// Your code goes here

Take note of the function onClickScene(event).

In this function, I set up:

Your code explanation goes here

Upon double-clicking the scene, the x and y values of the mouse point are logged in the console. These values depend on the position of the scene.

However, despite logging the correct values, the object is still being created at the same initial position when double-clicked.

I am striving to adjust this behavior so that the object appears at the exact location where the scene was double-clicked.

Any suggestions or solutions would be greatly appreciated!

Thank you.

Answer №1

When you assign window plane coordinates, the object is created at the same position. To ensure accurate positioning, consider raycasting from the mouse position to a plane using Raycaster.

( event.clientX / window.innerWidth ) * 2 - 1

Keep in mind that these are coordinates on a 2D window, not within your 3D scene. A helpful example can be found here: Interactive Voxel Painter.

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

Exploring the functionality of AngularJS routing on a webpage

Testing the routing functionality of AngularJS while creating a web page. The index.html will contain links to Home, Courses, and Students. Clicking on each link will load its respective HTML using AngularJS routing. The student information is stored in ...

The identification number is not used to update Mongo DB

When attempting to use the MongoDB driver in Node.js to update a document, I encountered an issue where the log indicated that the update was successful, but the data did not reflect the changes. Specifically, despite trying to update the document using it ...

Difficulty with two-dimensional arrays in Angular and Typescript

I am currently stuck trying to assign values to a 2-dimensional object array in Angular/Typescript. I have noticed that the last assignment seems to override the previous ones, but I cannot pinpoint why this is happening. Could someone please review my cod ...

Issues related to the performance of React, Redux, and three.js

UPDATE: After identifying the issue, I have narrowed it down to this small gist and this jsfiddle. In my real-time 3D game based on three.js, I intended to utilize Redux for state management. Despite creating a simple prototype for testing purposes, even ...

The selected attribute does not function properly with the <option> tag in Angular

Currently, I am faced with a challenge involving dropdowns and select2. My task is to edit a product, which includes selecting a category that corresponds to the product. However, when I open the modal, the selected group/category is not displayed in the d ...

Struggling to connect API from React and Node using a proxy server

I have encountered a troubleshooting issue with React and Node that I am struggling to resolve. The problem lies in the communication between my node server and coinmarketcap API. While I successfully receive data from both endpoints (all coins and individ ...

Regular expression for extracting all JavaScript class names and storing them in an array

In my quest to create a straightforward regex, I aim to spot all class names within a file. The catch is that it should identify them even if there's no space preceding the curly bracket. For example: class newClass {...} This should result in ...

Activate the Select List to Appear Only When a Search is Conducted

Currently, I have implemented the react-select API in order to provide language options for selection. <Select isMulti name="langs" options={langOptions} defaultValue={{ value: "English", label: "English", nativeNam ...

What causes the image to not appear at the center bottom of the page when using IE for loading?

Why does the loading image not appear at the center bottom of the page in IE? This function loads content when the page is loaded and also loads content when scrolled to the bottom. When you load the page index.php, you will see the loading image at the ...

"Displaying Undefined Content when an Incorrect Value is Added, but functioning correctly with the right

I am attempting to populate the HTML table below with data from a JSON file: When I set var i = "0";, I receive undefined, but changing it to var i = "dv"; results in nothing being displayed. I am uncertain of the error I am making and what is causing th ...

AngularJS encounters an issue while attempting to print a PDF file

I am encountering an issue with printing a PDF file that was generated using reportViewer in my Web API. The browser displays an error when attempting to open the PDF file. Below is the code snippet from the controller in my Web API: // ...generate candi ...

Toggle visibility of various items in a to-do list, displaying only one item at a time with the use of JavaScript

I am currently working on a web project using the Laravel framework. I am struggling with implementing a feature where only the title of each to-do item is displayed, and when clicked, it should reveal the corresponding content. However, I have encountered ...

Having trouble updating the sequelize-cli configuration to a dynamic configuration

Encountering an issue while attempting to switch the sequelize-cli configuration to dynamic configuration, following the instructions in the documentation. I have created the .sequelizerc-file in the project's root directory and set up the path to con ...

Looking to retrieve a cookie within Vue Router in Vue 3? Utilize the following approach within your router's `index.js

Scenario: Developing a Vue3 app with express as the API backend. Express utilizes express-sessions to create a server-side session that is transmitted to the browser and received in subsequent requests. I am in the process of implementing a route guard to ...

Error: Unable to locate OBJLoader in ThreeJS/Angular

Trying to incorporate ThreeJS into Angular, specifically using the OBJLoader to display .obj files. However, encountering this error message: "export 'OBJLoader' (imported as 'THREE') was not found in 'three' Below is the ...

Is it possible for an object filter to allow the object to pass through without a designated name?

In the javascript code below, I have managed to achieve the desired outcome by returning the 3rd and 4th objects in objectsArray since they both have the maximum distance. However, I am curious if there is a way to avoid repeating the name of the array whe ...

Tips for automatically closing one sub menu while selecting another sub menu

I am working on a code snippet to manage the menu functionality. My goal is to ensure that when I click to open one submenu, any other currently open submenus should automatically close. ;(function($) { // Ensuring everything is loaded properly $ ...

What steps do I need to take in order to create a histogram with perfect symmetry?

I am looking to create a unique JavaScript program that can generate a symmetric histogram resembling the image provided below: This program will prompt the user to input the number of bars they want to display and specify the character used to draw the b ...

Iterating over objects within a nested array using ng-repeat

My back-end is sending me an array of string arrays (Java - CXF-RS). The length of each array within the string arrays ranges from 1 to n. In order to display this data on my front-end, I am utilizing ng-repeat. Everything is functioning correctly with o ...

Configuring Azure Storage CORS settings for embedding video content

In my project, I am working on creating a panorama tour that includes additional content such as videos using three.js and React. The videos I am using are stored on a private Azure storage, where I generate SAS tokens for specific video files. When atte ...