Three.js: THREE.FontLoader() parse() does not return a THREE Font object instance

Created on 22 Mar 2018  路  2Comments  路  Source: mrdoob/three.js

I use my own system getFile() function to retrieve files from our secure app. I fetched the file helvetiker_regular.typeface.json and then parsed it like so:

let loader = new THREE.FontLoader();
let font = loader.parse( jsonFile);

As you can see in my 3 attachments, the parse method returns a generic object, not an instance of a THREE Font object.

When I use loader.load() method it returns an instance of THREE Font object.

Is this a bug?

Alejandro

screen shot 2018-03-22 at 2 28 11 pm
screen shot 2018-03-22 at 2 28 43 pm
screen shot 2018-03-22 at 2 29 30 pm

Most helpful comment

I guess you have to do this:

const loader = new THREE.FontLoader();
const json = JSON.parse( jsonFile ); // you have to parse the data so it becomes a JS object 
const font = loader.parse( json );

BTW: Even in you "incorrect" result, the result object is an instance of type THREE.Font. You just have a wrong data property.

All 2 comments

I guess you have to do this:

const loader = new THREE.FontLoader();
const json = JSON.parse( jsonFile ); // you have to parse the data so it becomes a JS object 
const font = loader.parse( json );

BTW: Even in you "incorrect" result, the result object is an instance of type THREE.Font. You just have a wrong data property.

Aha! I did try JSON.parse() but inadvertently parsed it backwards!

I did this and of course it wouldn't work...

let font = loader.parse( jsonFile );
font = JSON.parse( font ); 

This is my final working code, thank you!

    getFile( "/fonts/helvetiker_regular.typeface.json", ( jsonString ) =>
    {
      let jsonParsed = JSON.parse(jsonString);
      let loader = new THREE.FontLoader();
      let font = loader.parse( jsonParsed );
      let shapes = font.generateShapes( "SAMPLE TEXT", 100, 2 );
    } );
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonobr1 picture jonobr1  路  95Comments

mrdoob picture mrdoob  路  75Comments

fernandojsg picture fernandojsg  路  85Comments

kumavis picture kumavis  路  153Comments

DefinitelyMaybe picture DefinitelyMaybe  路  88Comments