Adjusting the size of a dynamic object in Three JS

Currently, I am facing a challenge with scaling a moving object in my THREE project. Whenever I attempt to scale the object, it not only changes its size but also shifts it to a different position.

Here is the code snippet I am using to set the scale of the object:

    // initiates the scale transition
function doTriangleScale(intersection){
  var tween = new TWEEN.Tween(intersection.object.parent.scale)
  .to({
    x: scaleSize,
    y: scaleSize,
    z: scaleSize,
   }, scaleEase)
  tween.start();

  // should have a check if the user is still on the object in question, no time for this now
  setTimeout(function(){
    doTriangleScaleRevert(intersection);
  }, 2000)
}

// triggers the scale revert to default
function doTriangleScaleRevert(intersection) {
  var tween = new TWEEN.Tween(intersection.object.parent.scale)
  .to({
    x: 1, 
    y: 1, 
    z: 1 
  }, scaleEase)
  tween.start();
}

The current solution works fine when the objects are static. However, my objects are dynamic and continuously moving as per the following animation code:

scene.traverse(function (e) {
    if (e instanceof THREE.Mesh && e.name != "pyramid") {

        e.rotation.x += rotationSpeed;
        e.rotation.y += rotationSpeed;
        e.rotation.z += rotationSpeed;

        if(triangleFloatLeft){
          e.position.x += 0.01;
        }else{
          e.position.x -= 0.01;
        }
    }
});

I am seeking a method that will allow me to scale the objects from their center while they are in motion.

Any help or suggestions would be greatly appreciated!

Answer №1

Objects in Three.js scale around their origin point. For instance, when working with a cube:

var box = new THREE.BoxGeometry(1, 1, 1);

If you scale a mesh using the geometry box, it will expand around its central point. However, if you adjust the origin of the geometry:

box.applyMatrix(new THREE.Matrix4().makeTranslation(0, 0.5, 0));

Now the cube will grow upwards because the origin has been shifted to the bottom of the box.

The origin of your object may not be at its center by default. You can reposition the origin either within the model itself (if imported) or via manipulation in Three.js as demonstrated above.

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

Error encountered when using the module export in Node.js

I have a file named db.js which contains the following code: var mysql = require('mysql2'); var mysqlModel = require('mysql-model'); var appModel = mysqlModel.createConnection({ host : 'localhost', us ...

troubleshooting knockout.js if bindings

It appears that the if binding is not functioning as expected in my case. Below is a snippet of my template: <div> <span data-bind="text: name"></span> <div data-bind="if: false ">+<span data-bind="text: priceFormatted" ...

Troubleshooting: Next JS 13/14 - Server component failing to update data upon revisiting without requiring a refresh

After attempting to retrieve the most recent liked items from a server component in next, I noticed that the data only displays when manually refreshing the page. Even when I navigate away and return, the data is not refetched automatically - however, usin ...

requirements needed for Bootstrap's image slider

When using just the Carousel functionality in JavaScript, which dependencies need to be included excluding `bootstrap.js` but including necessary code for Carousel (`carousel.js`, ...)? As per the docs, the setup code is: var myCarousel = document.querySe ...

Setting up a custom filter within a route in an Express JS application

Here is an example of a JSON object: var jsonString = '[{"name":"Manchester GTUG","meetup":"First Monday of every month","tags":["gtug","google","manchester","madlab"]},{"name":"Manchester jQuery Group","meetup":"First Tuesday of every month","tags": ...

The directive in Angular compels the webpage to carry out the added HTML attribute

There is a custom directive in my code that applies the spellcheck attribute to two div elements as shown below: (function(){ 'use strict'; app.directive('spellchecker', spellchecker); spellchecker.$inject = ['$timeout&a ...

Parent component in Material-ui theme distinguishing itself from child component of the same type

I'm working on customizing the styling of a Material UI Expansion Panel, specifically to apply my theme only to the main parent panel when it is expanded, and not to any nested panels within it. For instance: https://i.sstatic.net/nBhcN.png Here&ap ...

A Comprehensive Guide to Handling Errors in Angular 4: Passing the err Object from Service to Component

Currently, I am in the process of developing a login page using Angular for the front-end and Spring Security for the back-end. Everything appears to be functioning correctly, except for handling exceptions when attempting to catch errors from the service ...

Utilizing Array properties within a JavaScript class

https://i.sstatic.net/bqEZj.png I'm encountering some issues with the first property in my JavaScript class. Specifically, I'm trying to manage tokens using Firebase in a project that involves node.js and react.js. export default class NoNotifi ...

Tips for editing bootstrap-vue table columns using non-Latin characters?

I need to create a table using the Cyrillic alphabet, but the keys of the object must be in the Latin alphabet within the program. Example : export default { data() { return { wmsFields: ['№', 'Наименование', ...

Error encountered: MongoDB failed to execute Javascript as it was unable to save a DBQuery object in the specified location within the collection.js file

When attempting to modify an existing document in a MongoDb collection, an exception is generated with the following message: javascript execution failed : can't save a DBQuery object at src/mongo/shell/collection.js The following actions are perform ...

Implementing additional states in an AngularJS app configuration with Ui-Router

Currently tackling a major application with numerous routes for its different components, my team and I have decided to break down the routes into separate files instead of cramming everything into one large file. In attempting to create a variable and im ...

Imitate the actions of a <select>, <option> using a <ul>, <li> structure

I'm currently facing a challenge with my dropdown menu. I am trying to replicate the functionality of a select and option using a ul and li. When I click on the second li, I want it to replace the first li without removing the latter. Essentially, I ...

What could be causing the getComputedStyle() method to malfunction within my Nuxt application?

In my Nuxt app, I am working with SSR mode and Vuex. On one of the pages, I have a two-column layout where the text content comes from Vuex data. Here is the code snippet for that page: <template> <div> <v-container> ...

Icon toggling menu bug

I recently designed a menu featuring an animated icon that, when clicked, opens with two columns. The issue I'm facing is that after clicking on a link in the right column (view JSFiddle), the menu disappears but requires two additional clicks on the ...

changing the default property value within an object

I'm looking to send an ajax request to a PHP file that will retrieve a user's information in JSON format. I then want to take this data and assign it to a property within an object. For example: User = { prop1: '' } I simply need to ...

Express server encounters an error when attempting to access the specified route "/"

Currently, I am facing an issue with my Node server code. It functions perfectly well when running locally, but upon deployment, I encounter the dreaded "Cannot GET /" 404 error. Strangely, all other routes in the code work without a hitch, both locally an ...

React Native application crashes on Android 12 and above when run on an emulator

Currently in the process of creating a music application named Wavelet. Listed below are my dependencies: package.json Typically, I debug on an Android 11 emulator; however, when switching to an Android 12 emulator or using my physical device running on A ...

Run a function upon clicking a button in JavaScript

Could someone please help me figure out what I am doing incorrectly in the code below? I am attempting to trigger a function when a button is clicked. The HTML: <button id="btn1">Press me!</button> <input type="button" id="btn2" value="Pr ...

Issue with react-router-dom: <Route> elements are strictly meant for configuring the router and should not be rendered on their own

I've been grappling with this issue for quite some time now, but none of my attempts have succeeded and I keep encountering the same error: < Route> elements are for router configuration only and should not be rendered Here's the snippet ...