Create a dynamic animation using three.js

I am currently developing an application that optimizes pack placement in a truck using three.js for displaying 3D results. Below is the code snippet I'm working with:

My goal is to create a moving scene where boxes are displayed one by one and move towards their positions inside the truck.

If you would like to see the current 3D result, you can check it out here.

I'm struggling to figure out how to achieve this effect. Any ideas or suggestions would be greatly appreciated!

 // Your JavaScript code goes here...

Answer №1

By my understanding, you have a couple of tasks on your plate:

  1. Adjust the camera position
  2. Reveal the boxes sequentially

CAMERA ADJUSTMENT

To start off, create a global variable:

let pastTime = performance.now();

Next, craft an update() function and execute it within your animate() function:

function update() {
   const currentTime = performance.now();
   const timeElapsed = currentTime - pastTime; // calculating how much time has passed since last frame
   pastTime = currentTime;

   adjustCamera(timeElapsed);
}

The adjustCamera() function should look like this:

function adjustCamera(timeElapsed) {
    const SPEED = 10; // set as per your preference

    const CameraMoveDirection = new THREE.Vector3(1,0,0); 

    CameraMoveDirection.multiplyScalar( SPEED * timeElapsed );

    camera.position.add(CameraMoveDirection);
}

Camera movement completed.

SEQUENTIAL BOX REVEAL

You'll need a reference for your boxes. Define a variable boxes and store your objects there:

var camera, controls, scene, renderer, boxes = [];
...
// before scene.add( mesh );
mesh.visible = false ; // initially hiding all boxes
boxes.push(mesh);

In your update() function, after adjustCamera(timeElapsed), invoke showBoxes(). The question now is: when do we reveal the boxes? Let's say we want to unveil them as the camera moves past.

function showBoxes() {
   // Assuming camera is moving along X axis (like previous function). Modify as needed.
   const boxesToDisplay = boxes.filter( box => !box.visible && box.position.x < camera.position.x);
   for( let i=0 ; i < boxesToDisplay.length ; i++ ) {
      setTimeout( () => boxesToDisplay[i].visible=true, i * 1000 );
   }
  
}

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

Issues with Angular Component not detecting changes in @Input array

I'm dealing with a challenging setup where: The Parent Service (A) is imported in the Parent Component (B). Then, the Parent Component passes an array of Objects to a Child Component (C), which are referenced from the Parent Service (e.g. <child-c ...

Determine the presence of an element within a setInterval function while iterating through a for

I'm facing a challenge in validating if each element has been populated before generating a gauge. The gauge value comes from another API and requires waiting for the value to be available. Having the validation check within the For Loop doesn't ...

"Embracing the Dark: Exploring Quasar Framework with Vue3

Currently, I am utilizing Vue3 in combination with the Quasar Framework. I have successfully implemented an icon that switches my application to dark mode. However, I find the dark mode to be a bit too intense for my liking, and I would like to adjust the ...

Combining numerous words from a dataset using HTML and Javascript

I'm attempting to include a text from a button into a modal window. Currently, it is working; however, if the data consists of more than two words, only the first word is displayed. What am I doing incorrectly? ... <button type="submit" class="ob ...

Show or hide table columns easily without relying on jQuery

Within my HTML table that contains multiple columns, I aim to create a toggle feature for one specific column (in this case, the 4th column). Currently, I have accomplished this using jQuery: document.write('<a class="toggle" href="#!" data-alt="H ...

Having trouble retrieving the desired data from the JSON file

My current code is not giving me the expected results while trying to access JSON values with jQuery. Any suggestions on how I can resolve this issue? // JSON response: [{ "private": "8044553.0" }, { "governmentdocs": "98952.0" }, { "officiald ...

Ways to emphasize a distinct cell within a UI Grid by utilizing the cellClass feature

I am struggling with a requirement where I need to highlight a specific cell in a grid using its row and column numbers. However, in the current setup, when I scroll through the grid, other cells are also getting highlighted. It seems like I am not graspin ...

Tips for finding information within a table using HTML

My task involves creating a table with the option for users to search data within it. However, I have encountered an issue where I am only able to search for data in the first row of the table. <table style="width:100%" id="table"> <tr> ...

VS code shines a spotlight on Jasmine functions within Angular 6

Recently, I made the switch to Angular 6.0. However, in VS Code, all jasmine functions are being highlighted as unknown even though they work fine and the tests run successfully. How can I make intellisense recognize Jasmine? In previous versions, a worka ...

Retrieve a true value by using either Array.some or _.some

I am looking to retrieve a truthy value from the array.Some other than "true", This is my approach: let category; arduair.aqi_ranges[pollutant].some((item,index)=> { let min = item.range[0]; let max = item.range[1]; if (_.inRange(c,min,max)){ ...

Change the iframe's URL to a different one by clicking on a thumbnail

I am attempting to change the src="" attribute of an iframe when I click on the thumbnails. This is my current progress. HTML <div class="videoPopup"><span class="close">X close</span> <iframe width="600" height="400" src="" fram ...

Explain the inner workings of the setTimeout() function in JavaScript

My goal is to create a line in my code by placing points according to the line equation and adding a 100 millisecond delay before each point is displayed. However, when I try to run the code, it seems to wait for some time and then displays all the points ...

Challenges with setting up a bunyan child logger

This app consists of two files, foo.js and bar.js, both importing Logger.js to generate a child logger using bunyan. Issue: While foo.js is functioning properly, bar.js is encountering difficulty locating MyService.log defined in Logger.js. This problem s ...

Creating responsive design with multiple conditions in Vuetify/VueJS for different screen sizes of medium, large

I'm having difficulty setting conditions for breakpoints in Vue/Vuetify Essentially, I want to specify that if the array's length is divisible by 2, then the MD value should be 6, and if it's divisible by 3, then MD should be 12 <v-col c ...

Absolute positioning causes an element's height to increase

As I delve into the realm of typographical animations and seek to calculate the baseline of a font at various sizes, I've stumbled upon a peculiar discovery: It appears that the height values of elements tend to increase in an erratic manner when thei ...

TypeScript is unaware that a component receives a necessary prop from a Higher Order Component (HOC)

My component export is wrapped with a higher-order component (HOC) that adds a required prop to it, but TypeScript seems unaware that this prop has already been added by the HOC. Here's the Component: import * as React from "react"; import { withTex ...

Enhancing jQuery Rating Plugin

Currently, I am working on customizing the jQuery Bar Rating System Plugin. You can view an example of the plugin by visiting this link: . The rating system on my end will resemble Example D. Rather than having the plugin based on user input, my goal is to ...

Creating a multi-filter gallery similar to the one found in WooCommerce or eCommerce platforms involves integrating various filters to allow

Looking for a dynamic multifilter gallery similar to WooCommerce/ecommerce product filters? We have three types of filter dropdowns: COLOR, SIZE, and SHAPE. For example, if you select color: red and green, size: small, and shape: round The filtering wil ...

ThreeJs is known for effortlessly handling an abundance of vertices, far surpassing the number typically found

I came across this code snippet: function loadObject(filePath){ var loader = new THREE.OBJLoader(); loader.load( filePath, function ( object ) { child = object.children[0]; var geometry = new THREE.Geometry().fromBufferGeometry( ch ...

Get alerts from Twitter pushed to my site

Is it possible to create a web application that utilizes JavaScript to receive notifications from Twitter? I want my website app to send me notifications whenever a person I follow posts a new article. I'm having difficulty with this task and would gr ...