# Load font into global variable - efficiency

**URL:** <https://discourse.threejs.org/t/load-font-into-global-variable-efficiency/31608>\
**Category:** Questions\
**Tags:** font-loader\
**Created:** [November 8, 2021, 4:44pm UTC](https://discourse.threejs.org/t/load-font-into-global-variable-efficiency/31608 "2021-11-08T16:44:23Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![hofk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/hofk/32/44944_2.png) [@hofk](https://discourse.threejs.org/u/hofk)\
**Post date:** [November 9, 2021, 7:29pm UTC](https://discourse.threejs.org/t/load-font-into-global-variable-efficiency/31608/2 "2021-11-09T19:29:11Z")

</div>

I took a closer look at the loaders. With `DefaultLoadingManager.onLoad` things become clearer and possibly a bit more performant.

The animate function is simpler.  
At [DynamicChangeableText](https://hofk.de/main/discourse.threejs/2021/DynamicChangeableText/DynamicChangeableText.html) now the new version.

The modified part of the code:

```javascript

THREE.DefaultLoadingManager.onLoad = function ( ) {

	console.log( 'Loading Complete!');
    change = true;
    initText( );
    
};
            // https://threejs.org/examples/fonts/helvetiker_regular.typeface.json
fontLoader.load( 'helvetiker_regular.typeface.json', font => ft = font );

 
console.log( 'global 0', ft );

animate( );

function animate( ) {

    requestAnimationFrame( animate );   
    if( change ) changeText( );
    renderer.render( scene, camera );
    
}

```

* * *

## ===================================

**UPDATE:**

The simplest things are often only found at the end.  
One thinks much too complicated and crosswise.

The solution now under the original link [DynamicChangeableText](https://hofk.de/main/discourse.threejs/2021/DynamicChangeableText/DynamicChangeableText.html)

The full code:

```javascript
<!DOCTYPE html>
<!-- https://discourse.threejs.org/t/load-font-into-global-variable-efficiency/31608 -->
<head>
    <title> DynamicChangeableText </title>
    <meta charset="utf-8" />
    <style>
    body {
        margin: 0;
    }
    </style>
</head>
<body> </body>

<script src="../js/three.min.134.js"></script>
<script src="../js/OrbitControls.134.js"></script>
<script src="../js/FontLoader.134.js"></script>
<script>

// @author hofk

const scene = new THREE.Scene();

const camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set( 0, 0, 3 );

const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0xfdfde2, 1 );

document.body.appendChild( renderer.domElement );

const controls = new THREE.OrbitControls( camera, renderer.domElement );
 
const fontLoader = new THREE.FontLoader( );	
let textsGeometry = [];
let text = [];
 
THREE.DefaultLoadingManager.onLoad = function ( ) {
 
    initText( );
    animate( );
    
};
            // https://threejs.org/examples/fonts/helvetiker_regular.typeface.json
fontLoader.load( 'helvetiker_regular.typeface.json', font => ft = font );

function animate( ) {

    requestAnimationFrame( animate );   
    changeText( );
    renderer.render( scene, camera );
    
}

function initText( ) { // 1000 text lines

  for ( let i = 0; i < 1000 ; i ++ ) {
    
    const textsShapes = ft.generateShapes( 'initial text >>> ' + i , 0.04 );
    textsGeometry[i] = new THREE.ShapeGeometry( textsShapes );    
    const textsMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, side: THREE.DoubleSide } ); 
    text[i] = new THREE.Mesh( textsGeometry[i], textsMaterial );
    text[i].position.set( 0, -20 + i / 20 , 0 );
    scene.add( text[i]); 
  
  }
  
}

function changeText( ) { // change some text lines dynamically

    for ( let i = 390; i < 410; i ++ ) {

        const textShp = ft.generateShapes( 'new text >>> ' + i + ' random ' + Math.random( ), 0.035 );
        textsGeometry[i].dispose( );     
        text[i].geometry = new THREE.ShapeGeometry( textShp );
        text[i].geometry.needsUpdate = true;
    
    }
    
}

</script>
</html>

```

---

_[View the full topic](https://discourse.threejs.org/t/load-font-into-global-variable-efficiency/31608)._
