Create dimension markers on a 3D cube in Three.js by drawing dimension lines

Is it possible to create dynamic "lines" with the Cube element to represent different "Dimensions" during runtime?

I have successfully created a cube and implemented functionality for users to input dimensions and modify the cube in real time. You can view the code here: http://jsfiddle.net/9Lvk61j3/

Currently, I am looking to display the actual Dimensions so that users are aware of the length, width, and height they are adjusting.

Here is the desired end result:

Below is the HTML code snippet: HTML:

<script src="http://www.html5canvastutorials.com/libraries/three.min.js"></script>
<div id="container"></div>
<div class="inputRow clear" id="dimensionsNotRound" data-role="tooltip">
    <label class="grid-8">Dimensions (pixels):</label>
    <br/>
    <br/>
    <div> <span>Length</span>

        <input class="numeric-textbox" id="inp-length" type="text" value="100">
        <br/>
        <br/>
    </div>
    <div> <span>Width</span>

        <input class="numeric-textbox" id="inp-width" type="text" value="50">
        <br/>
        <br/>
    </div>
    <div> <span>Height</span>

        <input class="numeric-textbox" id="inp-height" type="text" value="40">
        <br/>
        <br/>
    </div>
    <button id="btn">Click me to change the Dimensions</button>

JS

For reference, here is the corresponding JSFiddle link: http://jsfiddle.net/9Lvk61j3/

If you require any additional information or have suggestions, please let me know.

Answer №1

When dealing with drawing dimensions, various challenges may arise:

  1. You could have numerous dimensions, but not all might be clearly visible:
    • some could be hidden,
    • others might seem too small if the camera is distant from the object,
    • certain dimensions may overlap with others or elements of the object,
    • and some might be observed from inconvenient angles.
  2. The text size needs to remain consistent regardless of how you maneuver the camera.

My solution addresses most of these issues: https://jsfiddle.net/mmalex/j35p1fw8/

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

var geometry = new THREE.BoxGeometry(8.15, 0.5, 12.25);
var material = new THREE.MeshPhongMaterial({
  color: 0x09f9f9,
  transparent: true,
  opacity: 0.75
});
var cube = new THREE.Mesh(geometry, material);
cube.geometry.computeBoundingBox();
root.add(cube);

var bbox = cube.geometry.boundingBox;

var dim = new LinearDimension(document.body, renderer, camera);

// define start and end point of dimension
var from = new THREE.Vector3(bbox.min.x, bbox.min.y, bbox.min.z);
var to = new THREE.Vector3(bbox.max.x, bbox.min.y, bbox.max.z);

// in which direction to "extrude" dimension away from object
var direction = new THREE.Vector3(0, 0, 1);

// request LinearDimension to create threejs node
var newDimension = dim.create(from, to, direction);

// make it cube child
cube.add(newDimension);

var animate = function() {
  requestAnimationFrame(animate);

  // we need to reposition dimension label on each camera change
  dim.update(camera);

  renderer.render(scene, camera);
};

Let's delve into helper classes now.

✔ The dimension line only displays when the camera angle isn't too sharp (more than 45°).

class FacingCamera determines the world plane best facing the camera, useful for concealing dimensions facing the camera at acute angles.

A separate fiddle to interact with class FacingCamera can be found here: https://jsfiddle.net/mmalex/56gzn8pL/

/* JavaScript code for FacingCamera class goes here */

✔ Dimension text as an HTML element styled using CSS and positioned via three.js raycasting logic.

class LinearDimension creates a linear dimension instance with arrows and text labels while managing it.

Complete implementation of LinearDimension:

/* JavaScript code for LinearDimension class goes 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

Output a message to the Java console once my Selenium-created Javascript callback is triggered

My journey with Javascript has led me to mastering callback functions and grasping the concept of 'functional programming'. However, as a newcomer to the language, I struggle to test my syntax within my IntelliJ IDE. Specifically, I am working on ...

Tips for utilizing JavaScript getElementByClassName to retrieve all the elements within a ul without having to specify the class name in each li

Looking to tidy up my HTML/CSS. Is there a way to keep this JavaScript functioning without needing to add the class name to every li element within the ul? Any suggestions on how to improve the visual appeal and readability of the HTML code? const Profi ...

The presence of #xfbml=1 is leading to some issues

Within my website, I am utilizing two distinct mechanisms incorporating both xfbml and fbjs: An FB:Like tag for individual entries The FB object for Facebook logins with FB connect The issue arises when "all.js" is included on the page, as the login scr ...

What is the best way to manage variables when iterating over an object of objects in a loop that contains inner asynchronous code?

I am working on a Node/Express server with a GET route where I need to send back a response to the client. The response consists of an array of objects, each with a 'url' property that contains a base64 encoded image retrieved using the 'bas ...

Looping through NavItems component using JavaScript or Angular

My Angular project includes a navbar component with an app sidebar that has a navItems attribute. Below is the content of my navBar: <app-header style="background-color : #e65100;" [fixed]="true" [navbarBrandFull]="{ src: &a ...

Integrating tooltips on Dimple.js line charts

A simplified network-style chart has been created using Dimple's line plot as the foundation. For example, please refer to this link: http://jsfiddle.net/cc1gpt2o/ myChart.addCategoryAxis("x", "Entity"); myChart.addCategoryAxis("y", "Entity").add ...

Develop a built-in Button for Liking posts

Currently, I am encountering an issue with the JavaScript SDK where I am unable to create a built-in Like button. After researching, I came across this helpful resource: https://developers.facebook.com/docs/opengraph/actions/builtin/likes/ The solution pr ...

Having trouble integrating a custom dialog with DirtyForms plugin

I have successfully implemented DirtyForms, a plugin that triggers a confirmation dialog when a user tries to navigate away from a page. The base functionality of the plugin is working as intended. Now, I am trying to integrate jQuery UI's Dialog for ...

Positioning a designated div directly above a designated spot on the canvas

I'm grappling with a challenge in my game where the canvas handles most of the animation, but the timer for the game resides outside the canvas in a separate div. I've been struggling to center the timer around the same focal point as the squares ...

Require assistance in understanding JSON data in the form of a variable

Apologies for any language barriers, but I have encountered a problem that I need help with. I am trying to create a highchart from an AJAX call. The following code works perfectly: chartOptions = { chart: { renderTo: 'grafica1', type: 'ar ...

Exporting modules for import such as Grid and Grid.Item

Is there a way to utilize two components in the following format: <Container> <Container.Item>X</Container.Item> <Container.Item>Y</Container.Item> </Container> Can you provide instructions on how to export these c ...

What are some alternative methods for concealing certain content in my gallery without using java script?

Currently, I am utilizing a JavaScript "show more, hide less" button on my page. However, I am facing an issue where I can't use the same button multiple times on the same page. I tried changing the ID, but it didn't work for me. I suspect I may ...

Guide to automatically dismiss calendar popup after selecting a date

After selecting a date, how can I hide the calendar? I am currently utilizing Date-time-picker by DanyelYKPan. Is there a specific function that I should be using? Here is the code snippet: <div class="col-3"> <div class="form-group calenderF ...

Anticipating the conclusion of a pending GET request

Currently, I am developing a system to create user accounts. There is a function in place that checks for the existence of a username and returns false if it doesn't exist. However, when another function calls this username checker, it automatically ...

Parsing JSON data using multilevel iteration in JavaScript is a complex yet

{ "id":0, "item":[ { "id":"0-", "text":"BlueWing", "userdata":[ { "name":"cid", "content":"10377" } ], "item":[ { "id" ...

What is the method to delete an image in JavaScript after a specific period of time has passed since its creation?

I need help figuring out how to make an image disappear on my website after 2 seconds. I've searched online for solutions, but haven't come across anything helpful. function rockImage() { var img = document.createElement("img"); img.src ...

Button react-native press following textInput within scroll view aware of keyboard movements

I'm currently facing an issue where I have a TextInput and a button nested inside a KeyboardAwareScrollView. The goal is for the user to input some text and then tap the button created using TouchableOpacity, which should send the inputted text forwar ...

The hover effect becomes disabled once the slider is toggled

My table has a feature where I can change the background color and other elements using a slider. However, I'm facing an issue where the hover effect on the table stops working when I toggle the slider on or off. I attempted to solve this with a funct ...

AngularJS: Recommendations for structuring code to dynamically update the DOM in response to AJAX requests

Within Angular's documentation, there is a straightforward example provided on their website: function PhoneListCtrl($scope, $http) { $http.get('phones/phones.json').success(function(data) { $scope.phones = data; }); $scope.order ...

Efficiently adding values to a variable with the forEach method

I'm encountering an issue with displaying data from a JSON file similar to the one below: Currently, "checked" is set to null. I aim to update it to true within the steps array using a forEach loop. JSON data snippet: { "id": 4, "process": { ...