The fontloader in three.js encountered an error while trying to read undefined generateshapes

I'm encountering an issue with my code where I am trying to generate meshes and add 3D text to a scene. However, whenever I attempt to do this, I receive the following error message:

TypeError: Cannot read property 'generateShapes' of undefined

Here is the snippet of code that I have for generating the meshes and adding the 3D text:

var x = 0;
var y = 0;
var finalSize = 450;
var textureLoader = new THREE.TextureLoader();
var fontLoader = new THREE.FontLoader();

The variable 'font' appears to be undefined. Why is that?

var font = fontLoader.load( 'css/arial_bold.json');
var fontColor = textMaterial = new THREE.MeshBasicMaterial({ color: 0x000000, overdraw: 0.5 });

for (var i = javascriptProjects.length - 1; i >= 0; i--) {

    var object = {};
    object.scale = javascriptProjects[i].baseImageData["0"] / finalSize;
    var geometry = new THREE.PlaneGeometry(javascriptProjects[i].baseImageData["0"]/object.scale,javascriptProjects[i].baseImageData["1"]/object.scale, 10, 10);
    object.texture = textureLoader.load( "data/"+ javascriptProjects[i].base_image );
    object.material = new THREE.MeshBasicMaterial( { map: object.texture, overdraw: 0.5, transparent: true } );
    object.material.opacity = 0.5;
    object.mesh = new THREE.Mesh(geometry, object.material);

//The line below is causing the error

    object.FontGeo = new THREE.TextGeometry( javascriptProjects[i].project_name , {
        font: font,
        size: 50,
        height: 2,
        curveSegments: 12,
        bevelThickness: 1,
        bevelSize: 1,
        bevelEnabled: false
    });
    object.textMesh = new THREE.Mesh( object.FontGeo, fontColor );
    object.textMesh.position.x = object.location.x;
    object.textMesh.position.y = object.location.y;
    object.textMesh.position.z = object.location.z - 100;

    scene.add(object.textMesh);

    object.location = new THREE.Vector3(x, 120, y);
    object.id = "" + javascriptProjects[i].project_id;
    object.mesh.position.x = object.location.x;
    object.mesh.position.y = object.location.y;
    object.mesh.position.z = object.location.z;


    scene.add(object.mesh);
    meshes.push(object.mesh);
    objects.push(object);


    x += 600;
    if(i % 3 == 0){
        y += 600;
        x = 0;
    };
};

It seems like 'font' is not defined correctly. Can someone point out where I might be going wrong? Any help or suggestions would be greatly appreciated. Please let me know if anything is unclear.

Thank you in advance

Answer №1

When using FontLoader.load(), keep in mind that it is an asynchronous function call. Therefore, an <code>onLoad
callback function is necessary.

It's important to note that the library calls font.generateShapes() before the actual font is fully loaded.

To ensure proper sequencing, consider using a pattern like the one below:

var loader = new THREE.FontLoader();
loader.load( 'myFontFile.json', function ( font ) {

    // Add your code here

} );

For more information and examples, you can check out this three.js demo, as well as refer to the three.js documentation.

Version of three.js being used: r.84

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

Display the webpage exclusively when the application has been set with `app.use('/api')` in Express 4

Here is what I currently have: app.js ... var api = require('./routes/api'); app.use('/', api); app.use('/api', api); ./routes/api ... var router = express.Router(); router.get('/', passport.authenticate(' ...

What is the best way to refresh a Windows 7 command prompt screen before executing a new function in Node.js?

I attempted system calls for cls and also tested out this code snippet: function clear() { process.stdout.write('\u001B[2J\u001B[0;0f'); } Unfortunately, none of the options seem to be effective. ...

When using v-for to render components and <selection>, is there a way to customize it for just one specific instance of the component?

How can I modify the selection in each instance separately when rendering elements of an array obtained from the backend using v-for? Currently, changing one selection affects all instances due to the v-model. Is there a way to target only one selection ...

Invoking functions from a JS file that has already been loaded

I am currently utilizing jQuery for my project. Within a JS file named super.js, I have the following code: $("#content").load("/profile"); function setHash(hash) { location.hash = "#/:-)/"+hash; } The "/profile" path is used to load an external JS f ...

Issue with content overlapping when hamburger icon is tapped on mobile device

When the hamburger menu is pressed on smaller screens, I want my navbar to cover the entire screen. To achieve this, I included the .push class in my code (see the jQuery and CSS) to trigger when the .navbar-toggle-icon is pushed. However, after implemen ...

Texture that can be clicked on the cube in Three.js

I am just starting to learn JavaScript and HTML and I recently created a textured cube. Now, I want to include different HTML links that will open pages in an iframe. To create my cube, I used this example: You can see what I've done so far here: I ...

Utilize jQuery to extract all values from the second cell of a table and store them in an array

I have a task that involves pushing numbers from text boxes into an array. These text boxes are located in the second cell of each table row, while the third cell contains a date picker. Currently, the code snippet provided retrieves all values from both ...

"Exploring the power of NodeJS with createServer, dealing with

Can instances of global.request potentially collide in this NodeJS scenario? I have a basic web server set up in NodeJS where I am globally exposing the request that is created: http.createServer(function(req, res) { global.request = req; // do ...

Trouble with Ajax loading asynchronously - webpage refreshing upon form submission

I am currently learning about ajax, and it seems like my code is correct. However, I am facing an issue where the page always refreshes when displaying the returned JSON string. Any assistance on this matter would be greatly appreciated! <script> ...

Refreshing a table row in AngularJS after adding an attribute to a directive

Referencing : Dynamically add directive in AngularJS I am attempting to dynamically add a directive to an element after it has been created and the page has loaded. When using $(nRow), which refers to a specific element, nothing happens with the current ...

The MongoDB Node cursor could not be located due to a false timeout setting

In my nodejs/express server, I am attempting to merge and sort sorted results from multiple mongodb collections to create a sorted CSV file. My method involves keeping the mongodb cursors alive (without timing out) until all data is read or an error occurs ...

Error encountered in React JS: Anticipated input was "CALC", "dimension", "number", but received unexpected end of input. CompileError originates from undefined CSS selector

After completing my entire project, I encountered an error while running yarn build. The error message from Yarn is as follows: Parse error on line 1: ^ Expecting "CALC", "LPAREN", "ADD", "SUB", "FUNCTION" ...

Conceal the navigation bar when scrolling to the top of the

Hey there! I'm looking for a way to hide my navigation bar when not scrolling, and then display it again once I start scrolling. Currently, I have two menus on this website: one with a white background and the other with a blue background. You can fi ...

Customize the MUI Select component with unique item children

I am currently working on developing a custom Select component that involves mapping through a list of items with specific types. Each item is supposed to display a certain MUI icon based on its type. To achieve this, I have created separate components for ...

The timer freezes at one minute remaining

I encountered a problem with the jQuery countdown I created. Once the count reaches 01:00, it stops instead of continuing to 00:59 with minutes at 0. var start = $('#start'); var countMinutes = 2; var timer; start.on('click', function ...

Getting the parameter route value from Laravel and passing it to Ajax

Need help with returning the parameter of a Laravel route to an AJAX response. Here is the relevant code: public function getPermissions(Request $request) { //$v = request()->route()->parameters('profile'); $v = request()-& ...

Retrieve a specific row from a table in Bootstrap

Is there a way to highlight a row in a table by clicking on it using CSS classes? $("#infoTable tr").click(function() { var selected = $(this).hasClass("highlight"); $("#infoTable tr").removeClass("highlight"); if (!selected) $(this).addClass( ...

Emails not being sent by Nodemailer

I recently configured my glitch project with a contact form and I'm attempting to set it up so that it sends me an email when someone fills out the form. The issue I'm experiencing is that while the server console logs indicate that the message h ...

What methods can be used to extend the distance measurement with the help of Google Maps

I've searched online for the answer, but still haven't found it. I'm currently developing a website where users can search and select a location using the Google Geocoding API. Once the user chooses a place, I retrieve its bounds, but then ...

delete content of file in vue 3

I have a Vue 3 component that looks like this: <template> <input type="file" :class="inputClass" accept=".pdf" @input="onFileSelect" ref="fileInput"> </template> <script> import ...