Tips for adjusting the size of a three.js object without changing its position

I'm working on animating a sphere made up of dots, and I'm facing a challenge. I want each dot to remain on the surface of the sphere while it resizes. Currently, I am attempting to scale the mesh or geometry of a specific point on the surface, but I'm encountering an issue where the dot seems to disappear beneath the sphere.

const dotGeometry = new three.CircleGeometry(2, 5)
const vector = new three.Vector3()
vector.setFromSphericalCoords(radius, point.phi, point.theta)

// Ensuring the dot faces the target from the origin 
// to maintain the correct slope on the sphere's surface.
dotGeometry.lookAt(vector)

// Moving the geometry to the specified point.
dotGeometry.translate(vector.x, vector.y, vector.z)

const dot = new three.Mesh(dotGeometry, dotMaterial)
scene.add(dot)

{
  // Animation logic within the render function
  const fi = 3 * time * 0.001
  const r = Math.abs(Math.asin(Math.sin(fi)))
  dot.scale.set(r, r, r)
}

Answer №1

The issue with the initial method is that when you adjust the size of the "visible" part of the dot, the entire geometry of the dot is scaled, causing its position to shift as it grows.

To achieve the desired outcome more effectively, it is advisable to separate the dot into a container object and a dot geometry. By doing this, scaling the dot will occur at the origin of the container geometry, preventing any unintended movement.

The container object can then be repositioned to the desired location, allowing you to scale the dot from the specified point.

const container = new three.Group()

const vector = new three.Vector3()
vector.setFromSphericalCoords(radius, point.phi, point.theta)

container.position.x = vector.x
container.position.y = vector.y
container.position.z = vector.z

const dotGeometry = new three.CircleGeometry(2, 5)

dotGeometry.lookAt(vector)
const dot = new three.Mesh(dotGeometry, dotMaterial)

container.add(dot)
scene.add(container)

//

{
  // Animation within the render function
  const fi = 3 * time * 0.001
  const r = Math.abs(Math.asin(Math.sin(fi)))
  dot.scale.set(r, r, r)
}

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

Having trouble retrieving data from JSON using JavaScript

Hey folks, I need some help with the following code snippet: function retrieveClientIP() { $.getJSON("http://192.168.127.2/getipclient.php?callback=?", function(json) { eval(json.ip); });} This function is used to fetch the IP address of visitors. When i ...

Is it necessary to include my library dependencies in the devDependencies section if I am only planning to publish the library bundle?

When creating a bundle for a library, should the library dependencies be placed in devDependencies? I am developing an NPM library in TypeScript that relies on several dependencies, including React components. As part of the build process, we compile to J ...

The scrollIntoView feature of JavaScript is unable to scroll an element to the exact top of the scrolling

Vue is the framework I am utilizing for my current project. JavaScript: let captureElement = document.getElementById('tag' + (index - 1)); if (captureElement) { captureElement.parentNode.scrollIntoView({behavior:"smooth",block: "start", inl ...

Is there a way to enable my progressBar to be draggable so that when clicked, it adjusts to a new currentTime position

I am trying to create a seekable audio progress bar in React, but I am facing issues with the seek function not working as intended. Below is my main play buttons file where all the code related to the progress bar is located. import React, { Component } ...

Troubleshooting problems with bot roles on Discord

After spending some time coding my bot, I encountered an issue when trying to add a role to a user who didn't already have it. Everything seemed to be functioning properly until I included the member.roles.add command. Despite having every possible p ...

Increase the object name in localStorage to save information entered in input fields

For testing purposes only - do not use for production. I am experimenting with storing data from 3 different input fields (text and numbers) in localStorage. My goal is to have the values increment every time the form is submitted. However, I am encounte ...

What is the best way to include an API key in the response to an Angular client application?

I'm working on transferring my API key from NodeJS to an Angular client application using $http, but I am unclear on the approach. Below is a snippet from my NodeJS file: // http://api.openweathermap.org/data/2.5/weather var express = require(' ...

Having trouble with Discord.js version 12 and the messageReactionAdd event not triggering properly?

client.on('messageReactionAdd', (reaction, user) => { console.log('If you see this I actually work...'); }); I am having some trouble with my code. Despite setting up a simple console log, it seems like the code is not running prope ...

Looking to subtly transition from the current webpage to the next one with a fading effect?

I'm looking to create a smooth transition between web pages on my site by slowly fading out the current page and fading in the next one when a link is clicked. $(document).ready(function() { $('body').css("display","none"); $(&a ...

You must use the 'new' keyword to invoke the class constructor NextResponse | Implementing middleware in Next.js | Implementing role-based protection for routes in Next.js |

I've been working on implementing user role-based protected routes in my next.js project using middleware.js, but all of a sudden, I've encountered this issue. I'm not exactly sure why this is happening, so if anyone has a better approach to ...

Assigning an interface to a useFetch object in Vuejs 3 and Nuxtjs 3: A step-by-step guide

Need assistance with assigning an interface to a useFetch object in Vuejs 3 Interface export interface ProdutoInterface { codigo: number nome: string } Componente const { data: produto, error } = await useFetch(config.API_BASE_URL+`/produto`) I&apos ...

Managing the hovering of a mouse over an image within an isometric grid displayed on a

I have implemented an isometric grid in HTML canvas. My goal is to handle mouse hover events on the buildings within the grid. Some buildings will vary in heights. In the image below, you can see that when hovering over a tile, the highlighted area shif ...

A helpful guide on displaying validation errors in a form using React JS

After creating a form and connecting it to the server, I encountered an issue with displaying validation errors below respective input fields. The error message response in case of validation error is as follows: "message": "ValidationError: confirmPasswor ...

Javascript: Dynamically Altering Images and Text

One feature on my website is a translation script. However, I'm struggling to activate it and update the image and text based on the selected language. This is the code snippet I am currently using: <div class="btn-group"> <button type ...

Error message encountered when using CKEditor in an Angular.js application

While using ckeditor to edit content, I have included the module and linked all necessary files. However, an error is being thrown: TypeError: this[a] is undefined. As a newcomer to Angular, I'm unable to find a solution on my own. Could someone plea ...

Issue encountered when attempting to alter the action attribute of a form: receiving an error message stating 'undefined is not a function'

I am attempting to dynamically set the action attribute of a form based on the button clicked in order to navigate away from a page. Once the action is updated, the form should be submitted and the new action carried out. Below is my jQuery function: fun ...

extract non-alphanumeric characters from an array of strings using javascript

I am trying to loop over an array of strings in JavaScript and remove special characters from elements that contain them. I have been successful in achieving this with individual strings, but I am facing some challenges when working with arrays of string ...

The MUI classes in my React project differ between the local and live versions, creating inconsistency in styling

I've encountered a discrepancy in the Mui classes used in my React project between the local and live versions. For instance: Localhost MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmal ...

Using Polymer and D3: A guide to transferring JSON data from element attributes to templates

As a newcomer to polymer, I wanted to integrate D3 with it. After modifying an existing Gist, I encountered some challenges. My goal is to pass JSON from a Polymer element attribute (barData) in HTML to the polymer template. The JSON is passed as a String ...

What is the optimal method for generating numerous records across various tables using a single API request in a sequelize-node.js-postgres environment?

I need to efficiently store data across multiple separate tables in Postgres within a single API call. While I can make individual calls for each table, I am seeking advice on the best way to handle this. Any tips or suggestions would be greatly appreciate ...