Animating the continuous transformation of websites using Three.js

Recently, I came across a fascinating website called Garden-Eight that impressed me with its seamless transition between the "Home" and "About Us" sections. The transition was so smooth and continuous, without any loading screens or delays.

Upon closer inspection, it appears as though the transition occurs within one scene, yet there is a noticeable change in the URL indicating a shift in site location.

While I may not be aiming to replicate such intricate transitions, I am intrigued by the idea of creating a seemingly continuous camera movement along the x-axis as a starting point.

As an experiment, I created a brief animation using this JavaScript code:

var camera, scene, renderer, geometry, material, mesh, geometryNew, materialNew, meshNew;

init();
animate();

function init() {

    scene = new THREE.Scene();

    camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.z = 500;
    scene.add(camera);

    geometry = new THREE.CubeGeometry(200, 200, 200);
    material = new THREE.MeshNormalMaterial();
mesh = new THREE.Mesh(geometry, material);
    mesh.position.set(0,0,0);
    scene.add(mesh);
    
    geometryNew = new THREE.CubeGeometry(200, 200, 200);
materialNew = new THREE.MeshNormalMaterial();
meshNew = new THREE.Mesh( geometryNew, materialNew);
    meshNew.position.set(800,0,0);
scene.add(meshNew);

    renderer = new THREE.CanvasRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);

    document.body.appendChild(renderer.domElement);

}

function animate() {

    requestAnimationFrame(animate);
    render();

}

function render() {

    mesh.rotation.x += 0.01;
    mesh.rotation.y += 0.02;
    
    meshNew.rotation.x -= 0.01;
    meshNew.rotation.y -= 0.02;
    
    if(camera.position.x < 800)
    camera.position.x += 3

    renderer.render(scene, camera);

}

You can view the animation on this JSFiddle link.

My goal is to have the first cube displayed on the landing page and the second cube on another page. Is there a way to achieve this through asynchronous loading or perhaps a built-in method for switching from one canvas to another?

Answer №1

This particular application is built as a single-page-app (SPA), where all content resides within one HTML page, regardless of any URL changes. Some frameworks, like React, have intricate methods to achieve this functionality, which would require a comprehensive tutorial to explain.

For a more straightforward approach, I suggest exploring the window.onhashchange event to simulate an "address change" without refreshing the entire page. For example, transitioning from site.com/ to site.com/#about would activate the event, enabling you to initiate animations on the same canvas:

window.onhashchange = function() {
  if (location.hash === '#about') {
    // animate camera towards about section
  } else if (location.hash === '#work') {
    // animate camera towards work section
  } else if (location.hash === '') {
    // animate camera towards home section
  }
}

You can then modify the hash to update the URL and trigger the aforementioned event:

// Adds #about hash to the URL and triggers the event listener
location.hash = "#about";

// Removes any hash and triggers the event listener
location.hash = "";

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

Can you explain the distinction between using <router-view/> and <router-view></router-view>?

In various projects, I have encountered both of these. Are they just "syntactic sugar" or do they hold unique distinctions? ...

User Interface Components in Backbone Views

I'm considering if there is a more efficient approach to this situation. I've got some HTML that requires event handling. Query 1: Since there are no data, models, or collections associated with it, do I need a render method? Query 2: I believ ...

The NVD3 tooltip is being obscured by other divs

Attempting to make the NVD3 tooltip appear above all other divs has presented a challenge. With three charts lined up horizontally and tooltips that exceed the boundaries of their divs, adjusting the z-index creates a dilemma. Regardless of which side&apos ...

VueJS does not update values instantly as they change

I have implemented a JS class with the following code: class Field { public Value = null; public Items = []; public UniqueKey = null; public getItems() { let items = [...this.Items]; items = items.filter((item) => { ...

Troubles encountered with switching npm registry

In my Vue 2.7 project with vuetify, I initially installed dependencies using a custom local npm registry, acting as a proxy to the default npm. As the project has expanded, I've started using git actions to deploy to a development server, with varying ...

Which is more effective: coding with just plain JavaScript and CSS, or utilizing frameworks?

As a student, is it more beneficial to focus on utilizing pure JavaScript & CSS or frameworks? And which approach is best suited for the professional field? ...

Show the current date in the "YYYY-MM-DD" format using the v-calendar component in VueJS

<DatePicker v-model="date" is-expanded is24hr :attributes="attrs" :model-config="{ type: 'string', mask: 'YYYY-MM-DD HH:mm&ap ...

Iterate through each element in the array and manipulate the corresponding data

I am trying to figure out a way to assign a unique id to each item in an array and send it to the "script.js" file for individual data processing. <script href="script.js"></sciprt> script.js: $(function(){ $('#box' ...

Problem with disabling dates on jQuery datepicker

After spending several days trying to solve this issue, I've decided to seek help. The problem at hand is disabling dates in my bootstrap datepicker using the following HTML code: <head> <!-- Required meta tags --> <meta charset="utf-8 ...

Can two writable stores in Svelte be set up to subscribe to each other simultaneously?

There is a unique scenario where two objects share data, yet have different structures. For instance, the 'Team' object has the team ID as its key. The 'Team' object includes 'name' and 'users' objects as its values ...

What is the best way to choose the following 3 elements that are siblings of a randomly positioned element?

I need assistance with selecting the next three siblings of a grid of divs on a board. Although I considered using nextUntil(), the issue is that the ids of these siblings are randomly generated upon loading, so they are not fixed. I am unsure how to solve ...

Converting CSV into an Array of Hash Tables

Currently, I am working on parsing a CSV file and creating an array of hashes. While I initially implemented this using my own code, I feel that it may not be the most efficient solution. My aim is to utilize the CSV-Parser library, but so far, I have only ...

Once the project is already created, you can integrate SASS into Angular CLI

Currently, I'm working on an Angular 4 project that was built using the Angular CLI. I'm aware that you can specify SASS as the styling option when creating a project with the Angular CLI, like this: ng new ProjectName --style=sass Now, I&apos ...

What could be the reason for the input.autocomplete feature failing to erase the existing history in my form? (HTML/JS)

const form = document.querySelector("#contact-form"); const feedback = document.querySelector(".feedback"); const patternName = /^[a-z A-Z]{3,25}$/; form.addEventListener("submit", (e) => { e.preventDefault(); const firstn ...

Express.js not redirecting to Angular route, app not starting

I have the following setup in my node.js app.js: app.use('/', routes); app.get('some_api', routes.someApi); app.use(function (req, res) { res.sendFile(path.join(__dirname, 'public', 'index.html')); }); Additio ...

Mobile page sliding mechanism

My website contains a div that is mostly off the page, but on hover it translates onto the main page. You can check out my website. However, this method doesn't work well on mobile devices. Hovering is not effective and I often have to click multipl ...

Methods for bypassing a constructor in programming

I am working on a code where I need to define a class called programmer that inherits from the employee class. The employee class constructor should have 4 parameters, and the programmer class constructor needs to have 5 parameters - 4 from the employee c ...

The shared global variable or store feature is currently not functioning as expected in Vue JS

Currently, I am in the process of developing a Simple Web application using Vue JS for educational purposes. Although I am new to Vue JS and still learning the ropes, I have encountered an issue with sharing a state variable across all components. In my a ...

Can one track the moment when a user clicks on an advertisement within a YouTube video?

Can we track when a user clicks on an advertisement within a YouTube video? We have integrated the YouTube API v2 into our website, allowing users to watch videos. The issue is that when a user clicks on an advertisement, the YouTube video pauses automat ...

Tips for determining if a key is present in local storage:

I need to set a key value, but only if it doesn't already exist. In my component1.ts file, I am assigning the key and value in the constructor. However, I want to include a condition that this action should only be taken if the key is not already pre ...