Challenges encountered during the updating of nodes in 3D force graphs

I am currently developing an application where users can utilize a dat.gui menu to customize their Three.js animation. Specifically, I am implementing the 3d-force-graph library.

Whenever a user makes a change, the following steps are taken:

  1. Swap out the old .json file used for their animation with the new one required to display their selection.
  2. Execute a function called vertexControl that adjusts the nodes of their new animation (e.g., changing node colors).

Here's a simple example:

const settings = {
    num : 5,
}
const gui = new dat.GUI();
gui.add(settings, 'num', 1, 10).step(1).onChange( function update(n) { 
    Graph.jsonUrl('new_json'+ n.toString() +'.json'); //built-in method of 3d-force-graph for updating the .json file
    vertexControl() //Once Graph is updated, adjust the vertices
}

The issue: The vertexControl function runs before the Graph object is updated with the new .json file. Consequently, the desired changes to the nodes do not take effect.

Attempted solutions: I have tried using the .refresh() method provided by 3d-force-graph, but it doesn't seem to work in my scenario. Additionally, I experimented with callbacks and other approaches from StackOverflow discussions related to executing functions sequentially. Unfortunately, these methods were unsuccessful. Resorting to a cumbersome setTimeout workaround to guess when the Graph is updated works, but it seems impractical for such a basic task. There must be a more logical way to achieve this, right?

Answer №1

One issue arises because the function Graph.jsonUrl() needs to asynchronously load the JSON, while vertexControl() is executed immediately before the JSON has completed loading. The force-graph documentation does not offer a built-in solution for handling callbacks upon JSON completion. One workaround could involve manually loading the JSON using THREE.FileLoader in order to trigger a custom callback after the file has finished loading:

// Utilize FileLoader to independently load JSON files
var fileLoader = new THREE.FileLoader();
const settings = {
    num : 5,
}
const gui = new dat.GUI();
gui.add(settings, 'num', 1, 10).step(1).onChange( function update(n) {
    fileLoader.load(
        'new_json'+ n.toString() +'.json',

        // Execute callback once JSON loading is complete
        function(response) {
            // Parse response text into a JSON object
            const newData = JSON.parse(response);

            // Send resulting object to the Graph
            Graph.graphData(newData);   

            // Proceed with VertexControl functionality
            vertexControl();
        }
    );
}

The first parameter of fileLoader.load is the file path, while the second parameter designates the callback function to execute AFTER the file finishes loading. This approach allows you to update your graph data and then call vertexControl immediately without any delays.

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

rails failing to establish connection between dynamically added nested form and its parent

Currently, I have a form called 'tasks' and I am dynamically inserting another form called 'steps' as a child. The addition of 'steps' is handled by a javascript function that calls render('steps/form'). While the ...

Confirmation message is displayed upon clicking to delete a row in the editable Gridview, prompting the user to

One issue I am facing is with a Delete button in an editable Gridview on my ASP.NET application. When the delete button is clicked, it triggers a function that displays a confirmation popup asking the user for feedback on whether they want to proceed with ...

There seems to be an issue with the Ajax call as it is returning a null

The situation at hand is this: Whenever I initiate an ajax call with the provided script, it always triggers the error exception instead of a success. The XML output seems to be correctly formatted with the appropriate mime type and character set. Since t ...

The vertical scrolling feature seems to be disabled for ng-repeat within the Angular view

I have a list of customers coming from the backend that I need to populate into ng-repeat. However, even though there are more customers, the vertical scroll is not visible. Why is that? <link rel="stylesheet" href="hike.css"> <div ng-show=' ...

MUI: Interaction with a button inside a MenuItem when not interacted with MenuItem itself?

Currently, I am utilizing MUI's Menu / MenuItem to create a menu of missions / tasks similar to the screenshot below: https://i.sstatic.net/6FGrx.png The MenuItem is interactive: // ... other code <MenuItem value={mission.issu ...

The button event is currently only targeting the initial class. (Jquery)

I am having an issue where only the first instance of the saveBtn class is being saved into local storage when clicked. Can anyone provide some assistance with this? Here is the HTML: <div class="hour-container" id="8am"> & ...

Issue with updating input value during keyup event in Android Chrome specifically

Check out this straightforward jsfiddle: https://jsfiddle.net/gq9jga4q/33/ <input type="text" id="kbdhook" /> <div id="result" >1: </div> <div id="result2" >2: </div> <div id="result3" >3: </div> $('#kbdh ...

Executing a SQL function in a JavaScript file using ASP.NET C# - here's how!

I am currently working on a calendar project that retrieves entries from a SQL database. The calendar is being built programmatically in a JavaScript file, where I have hardcoded the code for generating the calendar cell for a specific day (in this case, i ...

What is the process for loading a .x model in Three.js?

Could anyone provide guidance on how to successfully load an animated .x model in Three.js? I am currently struggling with the process and have attempted to convert the .x model to collada without success. Unfortunately, my search for a free program to c ...

Show the item in the menu with a label that has either subscript or superscript styling

Within the realm of electrons, the application menu is specified: const menuTemplate = [ { label:"Menu Item 1", click(){ //define some behavior } } ]; Is there a method to exhibit the name of the menu item as Me ...

Objective: Remove all soft items from array stored in MongoDb using Mongoose

I have a unique Array property in my document that is filled with lovely fluffy objects. Now, I am looking to remove all objects within a specific range -> qdate:20210225. The property in the database is named weeks and appears as follows: [ { ...

Guide to showcasing images dynamically within a table

I am currently working on a dynamic table that updates its data using a script. My goal is to also display corresponding images of the groups next to their names in the table. Whenever the group names change, I want the images to change as well. The funct ...

jQuery failing to trigger onClick event for a specific group of buttons

Javascript: <script> $(document).ready(function(){//Implementing AJAX functionality $(".acceptorbutton").on('click', function(){ var whichClicked = this.attr('name'); ...

Establish a connection to the Socket.IO server using a designated path and namespace

I am currently running my Node.js application on URL http://www.example.com/myapp/. Within my setup, I have a custom namespace for a Socket.IO server with version 1.3.5. Below is an example code snippet: var server = http.createServer(...); var io = sock ...

Encircle the text within intersecting divs

I am facing an issue with my main div that contains some text and 2 other overlapping divs. The text in the main div is getting overlapped by the other two divs. I need help to make the text in the main div wrap around the overlapping divs. It may sound s ...

JavaScript encountered an issue as it attempted to reference the variable "button" which was

I am currently working on developing a new API, but I have encountered some issues with JavaScript: Below is my JS/HTML code snippet: const express = require('express'); const app = express(); const PORT = 3000; submit.onclick = function() ...

Utilize Ajax to prevent the display of additional modals when encountering errors in the form data

I have a multi-step form on my website that consists of several modals. Each time I click "next," it triggers the next modal and submits form data. I am currently working on implementing validation for these modals, where I want to prevent the activation o ...

There is a single occurrence that happens when you click on a label alongside an

How can I change the number of events triggered when clicking a label without directly selecting the input element? Is there a way to achieve this using CSS only, or is it necessary to use JavaScript like document.querySelector('label input')? ...

Storing an image or video file to the disk of a NodeJS server and creating a reference in MongoDB

Seeking advice on the optimal method to store an image or video file on a NodeJS server and link it to MongoDB. Additionally, I need to enable streaming for the videos. Any recommendations would be greatly appreciated. Thank you! ...

Issue with PeerJs Localhost Setup

Implementing the zoom clone with WebRTC using peerjs, I am facing an issue where myPeer.on("call", (call) => { is not getting called. This code works fine for others on localhost who followed the tutorial on the zoom clone. I am unsure of what ...