Tips for manipulating a shape and adjusting the vertices later in three.js

Greetings and thank you in advance for your assistance! I have been facing challenges when creating shapes with the mouse using THREE.Shape. While I am able to draw and edit shapes that are centered on the canvas without any issues, I encounter problems when the shape is not at the center. It seems to be related to a local coordinates problem arising from points obtained via THREE.Raycaster.

//The vector3 'point' returned by Raycaster is used
ld.ui = 1;
const shapeVisible = new THREE.Shape();
shapeVisible.moveTo( point.x, point.y, ld.ui );
shapeVisible.lineTo( point.x, point.y, ld.ui );
const area = new THREE.ShapeGeometry(handle.shapeVisible);

//Code snippet to move a vertex
area.geometry.attributes.position.setXYZ( 0, point.x, point.y, ld.ui );
area.geometry.verticesNeedUpdate = true;

Everything functions correctly as long as the shape remains at the center of the canvas. However, when the shape is moved away from the center, there is a noticeable shift in the placement of the vertices when trying to reposition them using the following code:

//Again, 'pos' is the position returned by Raycaster 
area.position.y = pos.y;
area.position.x = pos.x;

The issue arises once the shape is moved, causing it to jump as if its origin has shifted. Subsequently, attempting to edit a vertex results in extreme displacement.

https://i.sstatic.net/H1B1F.gif

To visualize the problem, refer to this simple fiddle: Whenever you try to move the green box, it jumps to the mouse position with its bottom left corner aligned with the cursor instead of the object's center. https://jsfiddle.net/ox4q89fs/26/

 //Your JavaScript code here 
 //Your CSS code goes here 
 //Include necessary libraries and HTML elements here 

Your assistance in resolving this matter would be greatly appreciated. Thank you once again!

Answer №1

The issue you're encountering is due to the fact that the center of origin for the shape you've created is positioned at the bottom left.

When you interact with your mouse, the bottom left corner of the shape (its center of origin) is being placed where the cursor clicks.

If you adjust the center of origin for the shape to its actual center point, then when you click with your mouse, the center of the shape will align with your mouse pointer.

There are various methods to achieve this, and in this case, I utilized the following code:

var center = new THREE.Vector3();
mesh.geometry.computeBoundingBox();
mesh.geometry.boundingBox.getCenter(center);
mesh.geometry.center();
mesh.position.copy(center);

You can find more information in this post - (thanks @prisoner849 for the help!)

Feel free to check out a jsfiddle demonstration of your code with the shifted center of origin for the shape - http://jsfiddle.net/2xj8ak15/1

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

Encountering an issue: Unhandled error - Unable to access property 'parentNode' of a null value

I'm attempting to add a new div next to another div using the ComponentID of the first div as a reference. I want to place the second div parallel to the first one, like this: <div> <div id="userActivityStatusMenu-1110">Neighbor 1< ...

Steps for adding a row as the penultimate row in a table

There aren't many solutions out there that don't rely on jQuery, but I'm looking to avoid it. The row content needs to be generated dynamically. Here is my flawed approach: function addRow() { let newRow = '<tr><td>B ...

Finding mongoose in an array of objects nested within another object

Here is the MongoDB JSON document I am working with: { categoryId: '1', categoryName: 'Outdoors Equipments', items: [ { itemId: '1', itemName: 'Camping T ...

Alter the background image of a DIV based on the selected menu item

I am working on a project that involves a div element with the class "jumbotron" which currently has a background image applied to it. Additionally, I have a menu consisting of 6 items that I would like to interact with. My goal is to dynamically change ...

What is the best approach for rendering content to a page in ReactJS when a user clicks on a dynamic URL?

One challenge I am currently tackling is how to direct users to a specific page when they click on a dynamic URL. Specifically, within my "product_list" API data, there exists a key called "url". Upon clicking this "url", the user should be redirected to a ...

Transfer all files to a different computer on the local area network using Grunt

Is it possible to transfer all files from one directory to another computer on a LAN using Grunt? For instance, I would like to copy all files from my directory to \192.168.1.10\c$\Projects\TestFolder. How can I achieve this? Thank yo ...

Is there a way to convert Firebase JSON into a JavaScript object? If so, what is the method to

I am currently working on using the kimono web scraper in conjunction with Firebase to obtain data stored as JSON. To convert the JSON to XML, I am utilizing a JavaScript library which allows me to create a variable from the JSON file (an example is shown ...

Avoiding repetitive rendering of child components in ReactJS

In my React project, I have a parent component that contains 3 children components, structured like this: var Parent = React.createClass({ render: function() { return (<div> <C1/> <C2/> <C3/> ...

The use of WebSockets in conjunction with text encoding techniques

After reviewing the material, I came across this information: The WebSocket API allows for the use of a DOMString object, which is transmitted in UTF-8 format, or it can also accept an ArrayBuffer, ArrayBufferView, or Blob objects for binary data transf ...

Stop displaying AJAX requests in the console tab of Firebug, similar to how Twitter does it

I'm looking for a way to prevent my AJAX calls from being logged in Firebug's Console tab, similar to how Twitter does it. When using Twitter search, you'll see a live update feed showing "5 tweets since you searched." Twitter sends periodic ...

methods for retrieving data from a Laravel controller within an imported JavaScript file

My current approach involves using ajax to update a table based on the user's input. $('#user').change(function(){ var user= $("#user").val(); if (user !='None'){ $.ajax({ type: "GET", url: '/getUserAccounts/&a ...

Could someone kindly clarify the workings of this jQuery script for me?

I found this code online, but I'm struggling to comprehend its functionality. In order to make any edits for my needs, I need to understand how it operates. The purpose of this script is to close a panel with an upward slide animation when the "x" but ...

Shiny silver finish using three.js technology

I have recently started exploring three.js and am attempting to replicate a cube that looks like this: https://i.sstatic.net/CO9Eq.jpg One aspect I'm struggling with is creating the material for my cube. The material appears to be a silver polished fi ...

Rails encountered a syntax error while attempting to parse JSON data, due to an unexpected character found at line 2, column 1

I'm a beginner when it comes to using AJAX in Ruby on Rails. What I'm trying to achieve is that when a user clicks on a link with the class .link (redirecting to an external site, for example www.google.de), it should send data - specifically the ...

Tips for fixing connection issues when trying to connect to MongoDB on AWS Cloud9

I've been following a tutorial from 'Web Dev Simplified' using the AWS-Cloud9 Ubuntu environment and everything was going smoothly until I encountered an issue while trying to connect to MongoDB. The database appeared to install correctly us ...

Access the file and execute JavaScript code concurrently

Can I simultaneously link to a file as noticias.php and call a JavaScript function? <a href="javascript:toggleDiv('novidades');" class="linktriangulo"></a> The toggleDiv function in my noticias.php file: function toggleDiv(divId) { ...

Catching the Selenium NoSuchElementError in JavaScript is impossible

Update: It's puzzling why this was marked as answered since the linked questions don't address the issue at hand and do not involve Javascript. My objective is to detect this error, rather than prevent it, given that methods like invisibilityOfEl ...

Leveraging AJAX to assist in PHP for data parsing

I am currently exploring the world of AJAX and grappling with a unique situation. I am in the process of developing an HTML app that will be integrated into a mobile application using PhoneGap. The main objective of my project is for the HTML page to con ...

I'm having trouble with using setInterval() or the increment operator (i+=) while drawing on a canvas in Javascript. Can anyone help me out? I'm new

I am looking to create an animation where a square's stroke is drawn starting from the clicked position on a canvas. Currently, I am facing an issue where the values of variables p & q are not updating as expected when drawing the square at the click ...

Determine whether any property within the object is currently null

I am working with an array of objects called data, each object in the array having multiple properties, some of which may have null values. https://i.sstatic.net/hc5O3.png My goal is to filter through this array and eliminate any object that contains a p ...