Creating an animation of an object using a sine wave to move it from the center in three.js

I am attempting to recreate a fascinating effect that I found on this Dribbble link:

My goal is to animate the vertices of a plane in a similar fashion to what is showcased in the provided link. I understand that this effect is achieved through sine wave propagation, but I am struggling with commencing the movement from the central point of the plane. Currently, my code looks something like this:

(function drawFrame(ts){
  window.requestAnimationFrame(drawFrame);
  var vLength = plane.geometry.vertices.length;
  for (var i = 0; i < vLength; i++) {
    var v = plane.geometry.vertices[i];
    v.z = Math.sin(ts / 500 + (v.x * (vLength / 2)) * (v.y / (vLength / 2))) * 3 + 5;
  }

Although it somewhat works, I've noticed that in the top left and bottom right corners, the movement is directed inward towards the center of the plane instead of outward as intended. The other two corners are behaving exactly as desired.

If you're curious to see what I have so far, here's a link to my current progress: http://codepen.io/gbnikolov/pen/QwjGPg

I welcome all suggestions and ideas on how to improve this animation!

Answer №1

The function you've been searching for has been discovered and it was an enjoyable task!

(function animateFrame(timestamp){
  var center = new THREE.Vector2(0,0);
  window.requestAnimationFrame(animateFrame);
  var vertexLength = plane.geometry.vertices.length;
  for (var index = 0; index < vertexLength; index++) {
    var vertex = plane.geometry.vertices[index];
    var distance = new THREE.Vector2(vertex.x, vertex.y).sub(center);
    var size = 5.0;
    var magnitude = 2.0;
    vertex.z = Math.sin(distance.length()/size + (timestamp/500)) * magnitude;
  }
  plane.geometry.verticesNeedUpdate = true;
  renderer.render(scene, camera);
}());

A circular pattern is formed by defining a point named center as illustrated above. This serves as the starting point of the wave. By calculating the distance to this central point, we can apply the sin function to create the vertical movement. The addition of the timestamp ts introduces the element of time into the animation, while adjusting variables allows for customization of the wave's dimensions.

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

What is the process for creating static pages that can access local data within a NextJS 13 application?

I recently completed a blog tutorial and I must say, it works like a charm. It's able to generate dynamic pages from .md blog posts stored locally, creating a beautiful output. However, I've hit a roadblock while attempting what seems like a sim ...

Improving performance in Next.JS by optimizing unused JavaScript resources

Currently working on my first website using Next.js and experiencing poor performance scores after running a lighthouse test. The issue seems to be related to unused JavaScript files located in the chunk folder. I've come across suggestions to split t ...

What is the best approach for running a database query using the value selected from a form dropdown?

Currently, my application server is ColdFusion and I am using SQL Server for the database. Within a select form element on my webpage, users can choose from a list of vehicles such as Volvo S60, BMW M6, and VW Jetta. Once a user selects a vehicle, I need ...

PhoneGap - Encountering issue with sending JSON encoded data

I am currently utilizing Phonegap for establishing a connection with a server to verify a user's identity. The server requires the data to be Json encoded, and subsequently, I will receive a response from the server. var serviceURL = "http://appdev/P ...

Exploring Enumerations in Vue.js and Javascript

Is there a way to implement enums in a Vue file? I start by creating my enums in a JavaScript file const Status= Object.freeze({ Normal: "normal", Loading: "loading", Error: "error", Done: "done", }) ...

Updating the text of one option within a selected option using jQuery while iterating through each element in a function

Hey there, I have multiple rooms each with two select dropdowns. My problem is that when I choose the option with a value of 4 in the number of persons dropdown, I only want the text of the next option (the dinner select) to change under the room I selecte ...

When the server goes down, Chrome fails to play the video file whereas Firefox browser successfully plays it

In my observation, there is a peculiar behavior in Chrome. When a request is sent to the server and the browser receives media data, if the server goes down, Chrome is unable to play the media file (even though it downloaded the entire 168 MB media file ac ...

I am encountering an error when attempting to import the app from the server.js file in Express

In my server.js file, I have set up an express server and exported the app from it. //server.js require("dotenv").config(); const express = require("express"); const app = express(); const connectToDb = require("./connectToDb ...

Controller position fails to update following camera movement using teleport-controls in A-Frame

I am utilizing teleport-controls within A-Frame for my project. One issue I am facing is that after moving the camera with teleport-controls, the model of the Vive controller also moves. However, the attribute "position" of the controller element remains ...

Substituting ng-init with scope variables results in failure

Why is nothing rendering when I try to use scope instead of ng-init in my AngularJS code below? <!doctype html> <html ng-app="myApp" ng-controller="myCtrl"> <head> <title>Bookshop - Your Online Bookshop</title&g ...

Having trouble inserting JSON data into my PostgreSQL database using Node.js

Currently facing an issue while attempting to save JSON data into a database. This is the code I have: My service retrieves and caches data from an API with the specified parameter cep: const axios = require('axios'); const NodeCache = require(& ...

Using Mongoose and Node.js, employing Object.assign to duplicate the data retrieved from the database reveals supplementary information

Today I stumbled upon something intriguing, a concept that was previously unknown to me. I am seeking guidance to comprehend the reason behind this occurrence: User.findOne({email: req.body.email}, function(err, usr){ return res.json({ RAW: ...

Ways to invoke a JavaScript function using a variable

Using a Variable to Call JavaScript or jQuery Function // Declare a variable var functionName = "abc"; // Use the variable as a function call functionName(); // This will call the "abc" function // Define the "abc" function function abc() { // Funct ...

Rearranging columns in Bootstrap 4 beta across multiple containers

Sorry for any language issues in my English. This shows my regular grid: ------------------------------- | A | B | C | ------------------------------- | D (horizontal menu) | ------------------------------- Is it possible to d ...

Please display the Bootstrap Modal first before continuing

Currently, I'm facing a challenge with my JS code as it seems to continue running before displaying my Bootstrap Modal. On this website, users are required to input information and upon pressing the Save button, a function called "passTimeToSpring()" ...

Retrieve an array of items from the Firebase snapshot

Currently in the process of retrieving items from my Firebase database, I am utilizing a snapshot that is generated when the page loads. I have collected the values of each object in an array and I am now attempting to add an item from each object into a s ...

Can an AJAX request continue running even after the user has moved to a different page within the website?

Currently on my website, users are able to submit a form using ajax. The response, indicating whether the form was successfully submitted or if there was an issue, is displayed in an alert message. However, due to the asynchronous nature of this process, i ...

Is there a way for me to showcase the most recently added item above the existing one?

Is there a way to show the most recently added item at the top of the list instead of at the bottom? <div className="App"> <h2>{this.state.title}</h2> <form ref="myForm" className="myForm"> <input type="tex ...

Having trouble with displaying the modal dialog in Twitter Bootstrap 3?

There seems to be an issue with my Twitter Bootstrap modal as it is not rendering the dialog box correctly, and I'm puzzled about the reason behind this. HTML <p class="text-center btn-p"><button class="btn btn-warning" data-toggle="modal" ...

Is it possible to manually disrupt the predetermined prototype chain of Numbers, Strings, Booleans, and Arrays in JavaScript, or reassign values to do so?

Whenever we create a new variable of the number type (let's say num1), its __proto__ is set to point to the Number Object. The __proto__ of this points to Object Core, and its __proto__ ultimately ends with null, completing the Prototype chain. My go ...