Node text: three.js and ngraph.tree working together

I am currently working on developing a 3D network (network of people) for a browser using three.js and ngraph. The graph has been successfully created, but the nodes are currently displayed as squares. My goal is to replace these squares with the text "node" (which will later be substituted with the names of the individuals in the network). However, I am struggling with the process of displaying text instead of the current squares. Any assistance would be greatly appreciated!

Upon investigation, I came across this code snippet in .../ngraph.three/lib/default.js, where the squares are being created:

function createNodeUI(node) {

  var nodeMaterial = new THREE.MeshBasicMaterial({ color: 0xfefefe });

  var nodeGeometry = new THREE.BoxGeometry(NODE_SIZE, NODE_SIZE, NODE_SIZE);

  return new THREE.Mesh(nodeGeometry, nodeMaterial);
  
  }

Additionally, I identified this snippet in .../ngraph.three/node_modules/three/three.js:

case 'BoxGeometry':

case 'CubeGeometry': // backwards compatible

geometry = new THREE.BoxGeometry(

data.width,

    data.height,

data.depth,

data.widthSegments,

data.heightSegments,

    data.depthSegments

);

It appears that I may require something similar to the above code snippets to display text instead of squares, but my current knowledge of JavaScript is limited.

Despite referencing various tutorials and manuals, my attempts to replace the squares with text have resulted in the entire graph disappearing.

For instance, I tried the following code:

function createNodeUI(node) {

  var nodeMaterial = new THREE.MeshBasicMaterial({color: 0xfefefe});

  var nodeGeometry = new THREE.TextGeometry("text", {font:'helvetiker'});

  return new THREE.Mesh(nodeGeometry, nodeMaterial);  
 
}

implemented with:

THREE.TextGeometry = function ( text, parameters ) {



parameters = parameters || {};



var textShapes = THREE.FontUtils.generateShapes( text, parameters );



// translate parameters to ExtrudeGeometry API



parameters.amount = parameters.height !== undefined ? parameters.height : 50;



// defaults



if ( parameters.bevelThickness === undefined ) parameters.bevelThickness = 10;

if ( parameters.bevelSize === undefined ) parameters.bevelSize = 8;

if ( parameters.bevelEnabled === undefined ) parameters.bevelEnabled = false;



THREE.ExtrudeGeometry.call( this, textShapes, parameters );



this.type = 'TextGeometry';



};



THREE.TextGeometry.prototype = Object.create( THREE.ExtrudeGeometry.prototype );

THREE.TextGeometry.prototype.constructor = THREE.TextGeometry;

Thank you in advance for any assistance you can provide!

Answer №1

It turns out that the issue was quite simple to solve.

The problem arose from the fact that the 'helvetiker' font was missing, causing the entire graph to vanish while attempting to create the nodes (text) before drawing the lines. Since I couldn't figure out how to locate the 'helvetiker' font, I resorted to hard-coding it into my three.js code (not the best practice, I know...)

For those facing a similar problem, here is my 'solution':

function createNodeUI(node) {
   var nodeMaterial = new THREE.MeshFaceMaterial( [
     new THREE.MeshPhongMaterial( { color: 0x00cccc, shading:   THREE.FlatShading } ), // front
     new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ) // side

   ] );

   var nodeGeometry = new THREE.TextGeometry( node.data, { size: 5, height: 2, curveSegments: 6, font: "helvetiker", weight: "normal", style: "normal" });

   return new THREE.Mesh(nodeGeometry, nodeMaterial);

}

And the hard-coded section:

loadFace: function ( data ) {
    data = {"glyphs":{"ο" ...

If there is a more efficient way to address this issue, I am open to suggestions. However, for now, this solution seems to be working well :)

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

Verification response parameter not received from Google Authentication Express

I've been working on implementing a "sign in with Google" feature for my localhost website. Despite receiving the POST request, I noticed that it lacks the credential parameter. I'm unsure if this issue lies within the code or the API configurati ...

Checking for mobile SSR in a ReactJS applicationUncover the signs of mobile

I recently integrated the mobile-detect library into my project following this informative tutorial: link /src/utiles/isMobile.tsx: import MobileDetect from "mobile-detect"; import { GetServerSidePropsContext } from "next"; export co ...

Exploring the nuances in semantics between AJAX and post/get requests

I'm currently trying to grasp the concept of 'AJAX.' I know that it is short for Async JavaScript over XML, although JSON can also be used instead of XML. As far as I understand, AJAX allows for updating only parts of a web page without need ...

React Native: Having trouble opening the date picker

I'm encountering an error when trying to activate the date picker, but I'm not sure why it's happening. Any assistance would be greatly appreciated. The error message is as follows: "value.getTime is not a function. (In 'value.ge ...

Resolving the dilemma of complete form validation in Jquery

I'm currently working on a PHP form that is being validated with jQuery. In this form, there is a radio button list. If the user clicks "Yes," then the textbox related to that radio button should not be validated. However, if the user clicks "No," the ...

What is the best way to ensure one div expands to full width while simultaneously hiding another div on the page?

Hey there, I'm looking for a way to expand the width of a clicked div while making the other div disappear from the screen simultaneously. It should also be toggleable, so clicking the button again will shrink the div and bring back the other one. For ...

The useNavigate() hook from react-router-dom is not properly setting the id in the URL path

I am currently using react-router-dom v6 for my routing needs. My goal is to pass an ID in the navigate URL path. Here is the onClick method and button code that I am working with: let navigate = useNavigate(); const routeChange = (id) => { let ...

The Angular directive ng-model is not able to return a value

I'm currently troubleshooting an issue with the filters in an older project. Here's the HTML snippet: <input type="text" class="form-control" ng-model="FilterEventsEdit" ng-change="FilterEvents()" ...

What is the best method for validating multiple fields in a form with Javascript?

I am currently working on validating multiple fields within a form. Although I lack experience in JavaScript, I have managed to piece together a code that is functional for the most part. If all fields are left blank, error messages prompt correctly. Howe ...

Adjusting the size of div content without changing its dimensions

I'm in search of a solution for resizing the contents within a fixed width and height div. Currently, my div looks like this: <div id="editor_preview" style="width:360px !important; color:gray; ...

Code snippet for calculating the size of an HTML page using JavaScript/jQuery

Does anyone know of a way to calculate and display the size/weight (in KB) of an HTML page, similar to what is done here: Page size: 403.86KB This would include all resources such as text, images, and scripts. I came across a Pelican plugin that does th ...

What is the process for converting x and y coordinates to align with a fixed display?

When I make an API call, I am receiving X and Y coordinates in the following format: x: "-0.0120956897735595703" y: "0.147876381874084473" To display these coordinates on my minimap images, I have set their display to be absolute. The "left" and "top" pr ...

Adding a detached element under certain conditions

When attempting to add a detached span based on a specific condition, I encountered the following situation. const arrow = d3 .create('span') .classed('arrow', true) .text('\u2192'); //d3.select('bod ...

Adjust the node's location in Cytoscape.js

I recently switched from using Cola to fCose in Cytoscape.js for graphing multiple graphs with no connections. With Cola, I was able to manually set node positions by tweaking the layout options. However, with fCose, despite adjusting variables like quali ...

AngularJS Get request unable to fetch image URL due to receiving a "302 found" error

Trying to enhance my AngularJS app by incorporating an image from a random cat image site, using the URL received. Below is the snippet from my controller.js: 'use strict'; /* Controllers */ var catPath = "http://thecatapi.com/api/images/get? ...

Issue in TypeScript where object properties may still be considered undefined even after verifying using Object.values() for undefined values

I'm encountering an issue with TypeScript regarding my interface MentionItem. Both the id and value properties are supposed to be strings, but TypeScript is flagging them as possibly string | undefined. Interestingly, manually checking that id and va ...

What is the best way to showcase information retrieved using the getDocs function from Firebase Firestore?

Hello! I am currently exploring the world of Firestore and facing a challenge. My goal is to retrieve a data object from Firestore, set it as state, and then display it on my application: Below are the imports that I have utilized for this task (please ig ...

Instructions for removing a class using the onclick event in JavaScript

Is there a way to make it so that pressing a button will display a specific class, and if another button is pressed, the previous class is removed and a new one is shown? Thank you for your assistance. function myFunction() { document.getElementById ...

Is it true that all events in JavaScript go through capturing and bubbling phases?

My current project involves binding one eventListener to an <audio> element for the play event and another eventListener to its parent element for the same event. I've observed that the callback for the child element always gets executed, while ...

Require a more efficient strategy for iterating through lines of input

One of the challenges I'm facing with my form is that it contains 5 input lines. I need to keep any blank lines that are sandwiched between two filled lines, while removing all others. For instance, if the first line is blank, the second line contains ...