Learn the simple technique for creating smooth 3D object animations when clicking using Three.JS

As someone who is just beginning to explore Three.JS and 3D web development, I am eager to recreate a specific action that I saw in this video: https://www.youtube.com/watch?v=HWSTxPc8npk&feature=youtu.be&t=7s. The concept involves a group of 3D planes where clicking on one causes the surrounding planes to react and create space around the selected plane.

Currently, my initial test involves 3 planes, and my goal is to achieve a smooth animation where, upon clicking the center plane, the other planes smoothly adjust to create the illusion of being pushed rather than simply appearing and disappearing instantly.

In the future, I aim to implement a separate button for each plane, allowing users to click on a plane to create padding around it while the remaining planes in the stack adjust accordingly.

While I have looked into tools like Tween.js and CSS3D, as a beginner, I feel a bit overwhelmed. Any tutorials or tips on how to achieve this effect would be greatly appreciated!

// The JavaScript code for our project will be written here.
        window.addEventListener( 'resize', onWindowResize, false );

        function onWindowResize(){

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize( window.innerWidth, window.innerHeight );

        }
        var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

        var renderer = new THREE.WebGLRenderer();
        renderer.setSize( window.innerWidth, window.innerHeight );
        document.body.appendChild( renderer.domElement );

        var geometry = new THREE.PlaneGeometry( 3, 3, 1 );
        var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
        var plane = new THREE.Mesh( geometry, material );
        plane.rotation.y = -.7;

        var material2 = new THREE.MeshBasicMaterial( { color: 0x0000ff } );
        var material3 = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
        var plane2 = new THREE.Mesh(geometry, material2 );
        plane2.rotation.y = -.7;
        plane2.position.x = 1;

        var plane3 = new THREE.Mesh(geometry, material3);
        plane3.rotation.y = -.7;
        plane3.position.x = -1;

        scene.add( plane, plane2, plane3 );

        camera.position.z = 5;
        function render() {
            requestAnimationFrame( render );
            // cube.rotation.x += 0.1;
            // cube.rotation.y += 0.1;
            renderer.render( scene, camera );
        }
        render();

        function clickFirst() {
        TWEEN.removeAll();
        var tween = new TWEEN.Tween(plane3.position).to({x: -2}, 1000).start();
        tween.easing(TWEEN.Easing.Elastic.InOut);
            render();
        }
    </script>
    <button onclick="clickFirst();" style="background-color: white; z-index: 9999;">Click me</button>

Answer №1

Step one involves locating the two planes in question.

Step two is to ensure the planes are clickable by following these links: https://github.com/josdirksen/learning-threejs/blob/master/chapter-09/02-selecting-objects.html

Thirdly, utilize Tween.js for the transition process. Once the correct plane is selected, create a tween for the other planes to move along the same Axis:

For example:

createjs.Tween.get(plane3.position.z).to(
                plane3.position.z + 100
            , 1000, createjs.Ease.cubicOut)

Feel free to add more code for further assistance while implementing the steps.

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

Customer is unable to locate the "InitializeAuthenticationService" function

After launching my application, the browser console keeps showing me three errors that all say Could not find 'AuthenticationService.init' ('AuthenticationService' was undefined). and Microsoft.JSInterop.JSException: Could not find &apo ...

The pictures in a <div> tag are not showing up

Functionality: I have set up 2 different <div> elements with unique IDs. Each of these <div> elements will make an ajax call to fetch a specific set of images for display. To summarize, both <div> elements are invoking the same ajax met ...

How to dynamically adjust font size using Javascript

Is there a way to adjust the font size of the left-column using JavaScript? <div id="left-column"> <a href="">1</a><br> <a href="">2</a><br> <a href="">3</a><br> <a href=""> ...

React Three Fiber encountered an unexpected JSON token 'c' at position 3

I am encountering an issue while trying to load a .glb file using react-three-fiber. The error I'm receiving is as follows: Error Unexpected token c in JSON at position 3 I can't seem to figure out what mistake I am making - it seems that the co ...

Transform an array into an array of objects

I currently have a collection of words stored in a .json file Here is an example: [ "apple", "banana", "orange" ] My objective is to create an object for each word Similar to this structure: [ { word: "appl ...

How do I navigate to the homepage in React?

I am facing an issue with my routes. When I try to access a specific URL like http://localhost:3000/examp1, I want to redirect back to the HomePage. However, whenever I type in something like http://localhost:3000/***, I reach the page but nothing is dis ...

Model is updated by checkbox only on the second click

Take a look at this Plunkr example here: http://plnkr.co/edit/hwVL3xtnD9hGJL?p=preview After clicking the checkbox for the first time, the model fails to update. Can you explain why? var app = angular.module('main', ['ngMaterial']) ...

Trouble with jQuery script causing initial word count issue in textarea upon page load

I have implemented a word count textarea jQuery script and I am trying to figure out how to update the word count onload to 2 when the text area initially displays "this example". Currently, it shows 0 words. Although I can set focus on it and move the c ...

The Next JS build process is failing to generate certain paths

Issue with Anime Database App Deployment A problem arose when I developed an anime database app using Nextjs and deployed it on Vercel. Although the build was successful and the initial page rendered properly, only a few dynamic routes displayed correctly ...

Limit the selected values to calculate a partial sum

Imagine two distinct classes called professor and student: professor.ts export class Professor { id: number name: string } student.ts import { Professor } from "./professor" export class Student { ...

implementing select2 and binding an event to it

I have a simple select2 setup where I am passing a common class in the options and trying to trigger a jQuery event on every change or click of an item in the dropdown. Here is my code snippet: <select name="transferfrom" id="transferfrom" data-placeho ...

There appears to be an issue with the onmousemove function

I am currently troubleshooting an issue with a script that I wrote for my button. Interestingly, the code works flawlessly when I test it on CodePen, but I encountered an error when I attempted to run it in VSCode. Even after trying to recreate the script ...

Encountering issues with updating subdocuments using mongoose

When attempting to update data from a subdocument using mongoose, I am encountering some issues Below is the data model: { status: 'regular', devices: [ { ip: 'deviceIp', active: true, _id: 5f4c05cb4708cf0e37a68ac0, ...

Ensure to verify the input's value by clicking the button

Upon clicking a button, I am trying to determine if there is any content in an input field. However, it seems that the check only happens once when the website first loads. Subsequent clicks of the button do not detect any new input values entered into the ...

Creating an object and setting its prototype afterwards

While exploring examples and questions online about prototypal inheritance, I noticed that most of them involve assigning prototypes to constructor functions. One common pattern is shown in the code snippet below: Object.beget = function (o) { var F = ...

Unexpected JavaScript Bug (with jQuery)

I have an interesting code snippet that captures button clicks and then hides the existing content, revealing the content of the clicked item instead. This code is part of my init.js file along with other functionalities: $(function() { $('#conta ...

Tips for concealing a dynamic DOM element using JQuery after it has been generated

My current project involves creating a form that allows users to dynamically add text fields by clicking on an "add option" button. Additionally, they should be able to remove added fields with a "remove option" link that is generated by JQuery along with ...

Are you experiencing issues with modal contents extending beyond the modal on smaller screens?

I recently installed a modal plugin called blockUI for my image uploading needs. Although I have styled and positioned everything accordingly, I am facing an issue: Whenever I resize the browser screen, switch to my iPhone, or use another screen, some con ...

Unable to clear JSP form data

While attempting to reset the form data in JSP, the code given below is run. Although the data gets reset successfully, it reappears mysteriously from an unknown source: $('input[type="text"]').val(''); ...

Obtaining Function Call Results by Querying Node.js MySQL Connection

One issue I'm encountering while trying to retrieve data from a database is the synchronization of calls. In my attempt to resolve this problem, I experimented with a demo example and used callback functions. Being relatively new to node.js, I am unce ...