1

I am trying to use a simple piece of code to render 3d text but getting error XMLHttpRequest cannot load file:///Users/<name>/threejs/fonts/helvetiker_regular.typeface.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

var loader = new THREE.FontLoader();
loader.crossOrigin = '';

loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {

    // your code here
    var material = new THREE.MeshPhongMaterial({
          color: 0xdddddd
        });
        var textGeom = new THREE.TextGeometry( 'Hello World!', {
          font: '' // Must be lowercase!
        });
        var textMesh = new THREE.Mesh( textGeom, material );

        scene.add( textMesh );

        // Do some optional calculations. This is only if you need to get the
        // width of the generated text
        textGeom.computeBoundingBox();
        textGeom.textWidth = textGeom.boundingBox.max.x - textGeom.boundingBox.min.x;

} );

This seems to be a common issue just from googling this but don't understand what's different with my code.

I even tried following this http://blog.andrewray.me/creating-a-3d-font-in-three-js/ where they import the font with <script src="your-font.typeface.js"></script> but that isn't working for me either. I get an error elvetiker_regular.typeface.json:1 Uncaught SyntaxError: Unexpected token :

Where am I going wrong?

Stretch0
  • 8,362
  • 13
  • 71
  • 133
  • Maybe `font: '' // Must be lowercase!` should be `font: font`, as you get the `font` from your callback function and, in accordance to the [documentation](https://threejs.org/docs/index.html#api/geometries/TextGeometry), you have to put it in the parameters of `THREE.TextGeometry()`. – prisoner849 Jun 25 '17 at 09:18
  • 2
    Possible duplicate of ["Cross origin requests are only supported for HTTP." error when loading a local file](https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local) – Peter O. Jun 25 '17 at 13:02
  • 1
    Also read "[How to run things locally](https://threejs.org/docs/index.html#manual/introduction/How-to-run-thing-locally)" in the three.js documentation. – Peter O. Jun 25 '17 at 13:03
  • @PeterO. should add that as an answer.. – Martin Schuhfuß Jun 26 '17 at 22:35

0 Answers0