Tips for adjusting the Point based on the alignment of the line

In my current project, I am utilizing React THREEJS along with various libraries like DREI and Meshline to create lines and text elements. I am currently working on drawing a line and need to display the length label next to it. Here is an example of what I am aiming for:

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

Now, I am looking to implement a feature that allows me to adjust the position of the label based on the orientation of the line. For instance:

  • A vertical line should have movement options along the X-axis (Left/Right)
  • A horizontal line should have movement options along the Y-axis (Top/Bottom)

I have already calculated the middle position of the vector points P1 and P2:

  const P1 = new THREE.Vector3(-300.79, -296.96, 0);
  const P2 = new THREE.Vector3(101.49,  -8.43, 0);

  const disX = (P2.x + P1.x) / 2;
  const disY = (P2.y + P1.Y) / 2;

  const P3 = new THREE.Vector3(disX, disY, 0);

Any suggestions on how I can achieve this task effectively would be greatly appreciated!

Answer №1

When given the coordinates of two points, you can find the normal vector (norm) to the direction vector (dir) with norm(-dir.y, dir.x);

Here is a sample code snippet (not the final solution):

body{
  overflow: hidden;
  margin: 0;
}
<script async src="https://ga.jspm.io/npm:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5336207e3e3c37263f367e203b3a3e2013627d657d60">[email protected]</a>/dist/es-module-shims.js" crossorigin="anonymous"></script>
<script type="importmap">
  {
    "imports": {
      "three": "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccb8a4bea9a98cfce2fdf9ffe2fc">[email protected]</a>/build/three.module.js",
      "three/addons/": "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56223e2433331666786763657866">[email protected]</a>/examples/jsm/"
    }
  }
</script>
...
// Remaining code snippet included as provided
...
renderer.setAnimationLoop((_) => {
  renderer.render(scene, camera);
});

</script>

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 possibilities of utilizing a Kendo grid within an ASP.NET Web API

I am currently using the open source edition of Kendo Web with the Kendo UI Web on ASP.NET MVC 4. My Kendo grid contains the following JavaScript code: <script> $(document).ready(function () { $("#grid").kendoGrid({ dataSource: ...

Ajax polling ceases the polling process when an internet connection is lost

My current setup involves a continuous ajax polling cycle where messages are pulled, processed, a wait period occurs, and then the process is repeated. Everything runs smoothly until a user disconnects from the internet, whether it be due to a simple actio ...

Firestore - Safely removing a large number of documents without running into concurrency problems

I've encountered issues with my scheduled cloud function for deleting rooms in a Firestore-based game that were either created 5 minutes ago and are not full, or have finished. Here's the code I'm using: async function deleteExpiredRooms() ...

Having trouble with running 'npm init -y' in Windows? Here's a workaround for the VS

An error occurred when trying to initialize npm with the command "npm init -y". The name "WEB DEV" was deemed invalid. For a detailed log of this issue, please refer to: C:\Users\User\AppData\Roaming\npm-cache_logs\2020-12-05 ...

Enable the option to customize the icon of the recently activated sidebar menu item

I need help with changing the arrow icon of a menu-item only when it is clicked, without affecting all other menu-items. In the image below, you can see that currently, clicking on any menu-item changes all the arrows. Attached Image //sidebarMenu jQu ...

Creating a Kendo UI grid within a Kendo UI window and utilizing Knockout JS bindings

I'm encountering an unusual issue while working with Kendo UI and Knockout JS libraries. When attempting to display a kendo grid within a kendo window, the rows inside the grid are being duplicated. Below is a snippet of the code I'm using: JS: ...

The Service Worker seems to be neglecting to serve the sub-pages at

I've successfully implemented a service worker on my website. The home page loads correctly from the Service Worker cache, and when I visit previously viewed 'posts' while online in Chrome, they load and show as 'from ServiceWorker&apos ...

Issues encountered with JavaScript when trying to use the jQuery API

I'm trying to fetch random quotes using a Mashape API, but when I click the button, nothing happens. I've included the JS and HTML code below. Can anyone spot an issue with the JS code? The quote is not displaying in the div. Thank you! $(' ...

I am looking to update a WordPress site every 10 seconds to keep it current

Currently, I am working on a plugin and my goal is to automatically refresh the widget every 10 seconds. Can someone guide me on how to achieve this? I need to figure out a way to call the widget function at regular intervals in order to refresh the conte ...

Preserve the status of expanded nodes within the jqGrid tree grid

How can I make sure the Expanded State of my JQTree Grid is persistent? I want it to retain its previous expanded state even after reloading the grid. Can anyone guide me on how to achieve this? Thank you in advance! ...

Script executing single run only

I'm currently working on a side menu that slides open and closed from the left with the press of a button. I have successfully implemented the CSS and HTML code, but I am facing issues with the JavaScript. The functionality works flawlessly at first: ...

Enhancing a custom component with a transition-group component

I have a unique component that needs to include a list using v-for beneath it. <rearrangeable> <div v-for="item in items">...</div> </rearrangeable> I'm attempting to incorporate a <transition-group> element for addi ...

Automatically refreshing controller functionality in CodeIgniter

Greetings everyone, I'm looking for a way to automatically refresh a controller function every 5 seconds. Currently, I am using header('Refresh: 10.2'); within the controller function like this: public function delete() { heade ...

The quickest method to modify the anchor element's class depending on the query string

Requirement - I aim to accomplish this task using only pure JavaScript without any additional libraries, although I am open to using one if necessary. I need to assign an extra class to anchor elements that contain a query string of 'xyz=negate' ...

What is the best way to include two class names within a single div using Next.js?

Struggling to include two different CSS classes into a single div element, I encountered some issues. For reference, here is a screenshot: https://i.stack.imgur.com/UuCBV.png https://i.stack.imgur.com/sHNwq.png My code snippet looks like this: blog.js ...

How can I set up flat-pickr for date and time input with Vue.js?

I'm currently working with flat-pickr. Let's dive into the configuration part of it. I have a start date field and an end date field. My goal is to make it so that when a time is selected in the start date field, it defaults to 12h AM, and when a ...

Getting the WebElement object by manually clicking an element while in an active WebDriver Session

I am currently developing a Java Swing application for managing object repositories in Selenium scripts. This application will launch a WebDriver instance and allow users to manually navigate to the desired element for inspection. My goal is to capture th ...

Guidelines for naming classes in scss when working with Next.js

Is it possible to use CSS naming conventions in SCSS files with Next.js and then import them into JavaScript using a different convention? // login.module.scss file: .login-button { // some scss styling } // Login.js file: import styles from './ ...

Can we selectively execute certain tests in Cypress using support/index.js?

I need to selectively run certain tests from a pool of 50 test files located in the integration folder. Specifically, I only want 10 of them to execute. In an attempt to achieve this, I am trying to configure the selection process within the support/index. ...

Most effective method for sending information from an HTTP server to a browser client

What is the most effective method for transferring data from the server side to the client when the client is a web browser? The server side is developed in Java, while the client side uses HTML, JavaScript, and AJAX. The communication protocol being uti ...