Exploring Three.js Demos: Strategies for Success

Recently, I've delved into learning Three.js and stumbled upon some intriguing examples by Nat Geo. It got me thinking - is there a way to sync the animation progression with the scroll wheel? How did they seamlessly integrate WebGL elements with their website's design? In the first example, it appears that the webgl element is set in the background while regular HTML elements float over it. But the real puzzle lies in how they managed to time the animations so flawlessly.

If anyone has any insights or knowledge on the creation process of these fascinating visuals, I would greatly appreciate your input!

Check out the examples here: Nat Geo Example 1

And here as well: Nat Geo Example 2

Answer №1

Scrolling in a web browser is an interesting event.

Without delving into the specifics of the provided link, let's discuss a more generalized scenario.

Once you grasp the mechanics of this event, it becomes apparent that tracking the scroll distance on a page can be quite useful. Think of scroll distance as a trigger point, where reaching a certain point could prompt a specific action.

Imagine the following code snippet, which alters a button's background color based on different scroll thresholds:

var button = document.getElementById("myButton");
var position = 0;

function scrollHandler(e) {
  e.preventDefault();
  if (e.wheelDeltaY > 0 || position !== 0) {
    position += e.wheelDeltaY;
  }
  console.log(position);
  if (position < 500) {
    button.style.background = "";
  }
  if (position > 500) {
    button.style.background = "red";
  }
  if (position > 1000) {
    button.style.background = "green";
  }
}

button.addEventListener("mousewheel", scrollHandler);
button.addEventListener("DOMMouseScroll", scrollHandler);
<input id="myButton" type="button" value="PRESS ME!" />

Execute the code snippet and observe how hovering over the button while scrolling up or down triggers the color changes!

Answer №2

When creating animations, having access to the time is crucial.

let timeUniform = gl.getUniformLocation( prg, "uniTime" );
gl.uniform1f( timeUniform, time );

However, there's a workaround where you can use the scroll amount instead of real time. For example, if you want the full scroll of your screen to represent 7 seconds, you can incorporate this code snippet into your animation loop:

time = 7000 * screen.scrollTop / screen.scrollHeight;

Take a look at this interactive demo for a better understanding: https://jsfiddle.net/m1a9qry6/23/

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 with jQuery.cookie saving my JSON data

Being new to jQuery and JSON, I find this situation quite frustrating as it seems pretty straightforward but still doesn't work for me. My ASP.NET MVC website is using AJAX (via jQuery) to load data. Everything works fine until this point. I'm ...

What is the best location to initialize a fresh instance of the Firebase database?

Is the placement of const db = firebase.database() crucial in a cloud function script? For instance, in a file like index.ts where all my cloud functions are located, should I declare it at the top or within each individual function? const db = firebase. ...

click event on ion card

Attempting to handle a click event within an ion-card using Ionic 5.0.2 version has presented some challenges. Despite my efforts, I have not been successful in handling the event with the expected function. Here is a snippet of my code: Dynamic card list ...

How can I incorporate a standalone Vuetify component into my Nuxt.js project?

Using Vuetify with nuxt.js specifically for the dashboard layout - how can I achieve this in my nuxt.config.js file? modules: [ //['nuxt-leaflet', { /* module options */}], 'bootstrap-vue/nuxt', '@nuxtjs/ax ...

Extract a value from a JSON array without using any predefined entities

Can someone help me figure out how to access the values for 2.2.10.60 and "bank overdrafts...." from the array linked below? https://i.sstatic.net/XF3v9.png I attempted to retrieve the values using the following code: var json=chunk.toString(); ...

Dealing with all errors across all pages in Angular using a comprehensive error handling approach

I am currently using AngularJS and attempting to capture all errors across all pages. However, I am encountering an issue where it does not catch errors in some instances. For example, when I trigger a ReferenceError in one of the controllers, it is not be ...

Tips for extracting HTML entities from a string without altering the HTML tags

I need assistance with removing HTML tags from a string while preserving html entities like &nbps; & é < etc.. Currently, I am using the following method: stringWithTag = "<i> I want to keep my ->&nbsp;<- element space, bu ...

Spin the object around the world's central axis

I've encountered several different methods that claim to rotate an object around the world axis, but they all seem to rotate the object around its own axis instead of the world's. Can anyone advise on the correct approach to rotate an object arou ...

Switching ng-show value from separate controller

I'm currently trying to update my ng-show variable whenever a state changes. In my default index.html file, the code looks like this: <div id="searchBar" ng-show="searchBar" ng-controller="mainController"></div> However, since this is no ...

Region Covered by Mouse Over Does Not Extend Across Whole Div

On my website, there is an arrow located on the middle right side that, when hovered over with the mouse, reveals a sidebar. Clicking on each icon in the sidebar further extends it to reveal more content. However, the issue I am facing is that the sidebar ...

Having trouble with Node.js, JQuery, and Express.js routes returning a 404 error. Need some assistance in troubleshooting. Any suggestions on what might be causing the issue?

app.js var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = re ...

Editing JSON files - Substitute the $scope object with a new value

I am facing an issue with extracting data from an external JSON file for my application. Although most of the data loads into a DataTables table successfully, I am encountering problems with the $scope variable since it is referencing external data. Specif ...

Exploring scope in an angular service

My goal is to show a success message after clicking a button. Since I need this functionality in multiple controllers, I decided to implement it using a service. However, I am facing issues accessing the scope. index.html <div ng-controller="uploadCon ...

Is there a way to consolidate my current scroll script into a single function?

I have a script that enables smooth scrolling of contents within a div when hovered over certain anchor tags. I am considering consolidating the script into a function to eliminate the need for copying and modifying it each time I want to implement this f ...

Issue with scroll down button functionality not functioning as expected

Is there a way to create a simple scroll down button that smoothly takes you to a specific section on the page? I've tried numerous buttons, jQuery, and JavaScript methods, but for some reason, it's not working as expected. The link is set up co ...

Utilizing Webpack to Import GLB Models into Three.js

Just delving into the world of Webpack for the first time and I'm encountering some difficulties when trying to add my glb model. The model itself is fine, it's been used multiple times and I placed it in the public folder. I'm puzzled by th ...

What is the best way to send information to a different webpage?

I am in search of a solution to a rather simple query that has me puzzled. Should this question already exist elsewhere, I humbly request that you guide me to the answer. My apologies in advance if this is a duplicate. I currently have two URLs: http://12 ...

The resource was treated as an image but sent with the MIME type application/octet-stream

Upon accessing my webpage, a warning message is displayed: Resource interpreted as Image but transferred with MIME type application/octet-stream The images on my page are in JPEG format. The server writes the image bytes to an output stream and sends it ...

Leveraging kubectl.exe in conjunction with Docker and Node.js

I am currently attempting to execute my kubectl.exe file within a Docker environment. The version of kubectl is 1.26.2 and the Kustomize version is v4.5.7. Running everything successfully locally with npm start, however when trying in a docker container, ...

Unable to open new window on iOS devices using $window.open

alertPopup.then (function(res) { if(ionic.Platform.isAndroid()) { $window.open('android_link_here', '_system') } else if(ionic.Platform.isIOS()) { $window.open('ios_link_here', '_system& ...