# 3D text using meshwriter in Angular

**URL:** <https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656>\
**Category:** Feature requests\
**Created:** [October 30, 2019, 8:47am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656 "2019-10-30T08:47:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gopivell](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gopivell](https://forum.babylonjs.com/u/gopivell)\
**Post date:** [October 30, 2019, 8:47am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/1 "2019-10-30T08:47:51Z")

</div>

[3D text in Babylon](https://forum.babylonjs.com/t/3d-text-in-babylon/253).

When I tried to use Meshwriter in angular, I get an error like this

ERROR TypeError: meshwriter\_\_WEBPACK\_IMPORTED\_MODULE\_3\_\_\_default.a.MeshWriter is not a function.  
Babylonjs-GUI is also an extension that works good inside angular framework when installed as npm package.

MeshWriter should be added in Babylon namespace(Babylon.d.ts).

---

<div class="post-metadata">

**Author:** ![TheLeftover](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@TheLeftover](https://forum.babylonjs.com/u/TheLeftover)\
**Post date:** [October 30, 2019, 2:19pm UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/2 "2019-10-30T14:19:59Z")

</div>

gopivell, sorry for the difficulty. Can you give me a bit more information?

How are you loading it in Angular? Can you copy the commands to this thread? And where does the error arrive? On the JavaScript console of the browser? If a screen shot of that has any more information, that would be great.

There are several ways to load MeshWriter as a regular script file. For example:

```auto
const typeURL = "https://host/path/meshwriter.min.js";  
jQuery.getScript(typeURL).then(startWriting)

```

But, as it was written in JavaScript, not Typescript, it might not fit into certain build flows. Please be patient.

---

<div class="post-metadata">

**Author:** ![gopivell](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gopivell](https://forum.babylonjs.com/u/gopivell)\
**Post date:** [October 31, 2019, 5:22am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/3 "2019-10-31T05:22:14Z")

</div>

I have Installed meshwriter package 1.1.1 from npm in the project.  
Since its is angular, I cant import like this,

const typeURL = “[https://host/path/meshwriter.min.js](https://host/path/meshwriter.min.js)”;  
jQuery.getScript(typeURL).then(startWriting)

## code snippet:

import \* as BABYLON from ‘babylonjs’;  
import \* as GUI from ‘babylonjs-gui’;  
import \* as MeshWriter from ‘meshwriter’;

export class TextDisplay {  
private GUI2DText: GUI.TextBlock;  
constructor(){  
this.GUI2DText = new GUI.TextBlock();  
}

```
public Display3DText ( scene?:any ): void{
  let Writer = (BABYLON as any).MeshWriter(scene, {scale:0.5});
  let text1 = new Writer( 
            "ABC",
            {
                "anchor": "center",
                "letter-height": 50,
                "color": "#1C3870",
                "position": {
                    "z": 20
                }
            }
        );

       let text2 = new Writer( 
            "abcdefghijklmnopqrstuvwxyz",
            {
                "anchor": "center",
                "letter-height": 50,
                "color": "#70381C",
                "position": {
                    "z": -20
                }
            }
        );

```

let text3 = new Writer(  
“$12,345-#67,890 = 100%++!”,  
{  
“anchor”: “left”, // The default value; this line has no effect  
“letter-height”: 20,  
“color”: “#70701C”,  
“position”: {  
“y”: 40,  
“x”: 5  
}  
}  
);  
let text4 = new Writer(  
“false||TRUE === 1 && 0.0??”,  
{  
“anchor”: “right”,  
“letter-height”: 20,  
“color”: “#70701C”,  
“position”: {  
“y”: 40,  
“x”: -5  
}  
}  
);  
}  
}

In this sample code, I have used GUI. Here GUI runs without any errors.

The error I get for meshwriter is

AppComponent\_Host.ngfactory.js? [sm]:1 ERROR TypeError: babylonjs\_\_WEBPACK\_IMPORTED\_MODULE\_1\_\_.MeshWriter is not a function  
at TextDisplay.Display3DText (TextDisplay.ts:13)  
at AppComponent.InitializeRendering (app.component.ts:38)  
at AppComponent.ngAfterViewInit (app.component.ts:28)  
at callProviderLifecycles (core.js:32328)  
at callElementProvidersLifecycles (core.js:32293)  
at callLifecycleHooksChildrenFirst (core.js:32275)  
at checkAndUpdateView (core.js:44281)  
at callWithDebugContext (core.js:45632)  
at Object.debugCheckAndUpdateView [as checkAndUpdateView] (core.js:45194)  
at ViewRef\_.detectChanges (core.js:31126)

You can also refer the following links

> **[TypeScript & Babylon.GUI Working Example](https://www.html5gamedevs.com/topic/32364-typescript-babylongui-working-example/)**
>
> Hello. I apologies for multiple threads around the same issue, but I honestly feel very frustrated. I am waiting for the NPM package of Babylon and Babylon.gui to be refactored to support moduler importing. Until then does anyone have a working...

 ![MeshWriter%20Error](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/f/fec127fa270cdc3e10e17e5b2a659ec84b897477.png)

---

<div class="post-metadata">

**Author:** ![gopivell](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gopivell](https://forum.babylonjs.com/u/gopivell)\
**Post date:** [October 31, 2019, 5:28am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/4 "2019-10-31T05:28:19Z")

</div>

You can also refer

> <https://stackoverflow.com/questions/45227188/import-an-extension-module-in-node>

  

> **[How do you activate the extensions included in the npm babylonjs library.](https://www.html5gamedevs.com/topic/31752-how-do-you-activate-the-extensions-included-in-the-npm-babylonjs-library/)**
>
> Good evening all, My name is Christopher, and I have decided to use the Babylonjs npm repo. Question Once you include the babylonjs npm library through npm install how do you activate the extensions which are included? the attached file-structure.png...

---

<div class="post-metadata">

**Author:** ![TheLeftover](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@TheLeftover](https://forum.babylonjs.com/u/TheLeftover)\
**Post date:** [October 31, 2019, 12:07pm UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/5 "2019-10-31T12:07:36Z")

</div>

That is a ton of helpful information! Thank you.

Let me see what I can do on this today.

---

<div class="post-metadata">

**Author:** ![TheLeftover](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@TheLeftover](https://forum.babylonjs.com/u/TheLeftover)\
**Post date:** [October 31, 2019, 4:48pm UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/6 "2019-10-31T16:48:31Z")

</div>

@gopivell, I have gone through this twice now. I think the larger problem may be that MeshWriter is not written in TypeScript. This prevents it from fitting into some build processes.

When the module is loaded, it should put MeshWriter in some handy places, like BABYLON.MeshWriter. (If BABYLON is in the global address space.) Typing **BABYLON.MeshWriter** in the address bar should give you the MeshWriter Wrapper. That _ **really** _ should work.

If BABYLON is not loaded previously then the function is associated with **window.MeshWriter**. You can go from there.

Nonetheless I updated the version of MeshWriter in NPM. You might (possibly) find that the latest version (1.1.2) yields better results. Please do try it.

Although it is untidy, it may be best to simply load meshwriter.min.js independently, after BABYLON is loaded. Unless Angular prevents that somehow . . .

---

<div class="post-metadata">

**Author:** ![gopivell](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gopivell](https://forum.babylonjs.com/u/gopivell)\
**Post date:** [November 1, 2019, 4:55am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/7 "2019-11-01T04:55:45Z")

</div>

@TheLeftover,  
Thanks for package update.

I tried loading script in the angular component, But it is not working.

> <https://stackoverflow.com/questions/44204417/dynamically-load-external-javascript-file-from-angular-component>

I have used npm package MeshWriter 1.1.2. Javascript packages can be imported into angular. Importing works fine now.  
I am able to import like this

import MeshWriter from ‘meshwriter’;

public Display3DText ( scene?:any ): void{  
let Writer = MeshWriter(scene, {scale:0.5});  
let text1 = new Writer(  
“ABC”,  
{  
“anchor”: “center”,  
“letter-height”: 50,  
“color”: “#1C3870”,  
“position”: {  
“z”: 20  
}  
}  
);

```
       let text2 = new Writer( 
            "abcdefghijklmnopqrstuvwxyz",
            {
                "anchor": "center",
                "letter-height": 50,
                "color": "#70381C",
                "position": {
                    "z": -20
                }
            }
        );

```

let text3 = new Writer(  
“$12,345-#67,890 = 100%++!”,  
{  
“anchor”: “left”, // The default value; this line has no effect  
“letter-height”: 20,  
“color”: “#70701C”,  
“position”: {  
“y”: 40,  
“x”: 5  
}  
}  
);  
let text4 = new Writer(  
“false||TRUE === 1 && 0.0??”,  
{  
“anchor”: “right”,  
“letter-height”: 20,  
“color”: “#70701C”,  
“position”: {  
“y”: 40,  
“x”: -5  
}  
}  
);  
}

But, it crashes in the line  
let text1 = new Writer()

 ![meshwriter_error_2](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/8/8a37c6f724bbbc4f64d343fb42003f57edfa1a5f.png)

---

<div class="post-metadata">

**Author:** ![TheLeftover](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@TheLeftover](https://forum.babylonjs.com/u/TheLeftover)\
**Post date:** [November 1, 2019, 10:20am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/8 "2019-11-01T10:20:04Z")

</div>

That is good news. Different error messages help.

The Earcut module is a common stumbling point. You can read about it at the MeshWriter github repo help. ([GitHub - briantbutton/meshwriter: Babylon Mesh Writer](https://github.com/briantbutton/meshwriter)) You can get it there. A recent version of Earcut is also delivered in the MeshWriter npm package (node\_modules/meshwriter/earcut).

Hope that helps. 3:00am here. Going back to sleep. I will check in again tomorrow.

---

<div class="post-metadata">

**Author:** ![TheLeftover](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@TheLeftover](https://forum.babylonjs.com/u/TheLeftover)\
**Post date:** [November 1, 2019, 3:19pm UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/9 "2019-11-01T15:19:01Z")

</div>

> [@gopivell](#):
>
> I am able to import like this
> 
> import MeshWriter from ‘meshwriter’;

@gopivell, that is awesome! I am delighted.

Were you able to find Earcut and get it loaded likewise? I would like to close this out as a success.

---

<div class="post-metadata">

**Author:** ![gopivell](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gopivell](https://forum.babylonjs.com/u/gopivell)\
**Post date:** [November 11, 2019, 5:59am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/10 "2019-11-11T05:59:58Z")

</div>

Hi @TheLeftover,  
Sorry for the late reply.  
I did find earcut and installed it as npm package. But the above mentioned error is repeated, “Earcut is not defined”.  
I suggest you to import earcut as a npm package in mesh writer. I hope this solves the issue.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 12, 2019, 7:26pm UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/11 "2019-11-12T19:26:24Z")

</div>

Another possibility is to simply add something like `<script src="https://preview.babylonjs.com/earcut.min.js"></script>` in your html page (or get earcut in your own directory tree and make the script points to it).

---

<div class="post-metadata">

**Author:** ![gopivell](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gopivell](https://forum.babylonjs.com/u/gopivell)\
**Post date:** [November 13, 2019, 10:42am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/12 "2019-11-13T10:42:35Z")

</div>

@Evgeni_Popov  
Hi,  
In Angular, Cannot import external scripts like that.  
Need to import as npm package only.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 13, 2019, 11:12am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/13 "2019-11-13T11:12:38Z")

</div>

It seems it is possible:

> <https://stackoverflow.com/questions/44817349/how-do-i-include-a-javascript-script-file-in-angular-and-call-a-function-from-th>

> <https://stackoverflow.com/questions/37081943/angular2-import-external-js-file-into-component>

---

<div class="post-metadata">

**Author:** ![gopivell](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gopivell](https://forum.babylonjs.com/u/gopivell)\
**Post date:** [November 20, 2019, 6:16am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/14 "2019-11-20T06:16:27Z")

</div>

@Evgeni_Popov  
Hi,  
External javascript can be imported if it does not have any dependency with any other modules.

If I am using Earcut directly in my app, I can use it as external js. But I am using meshwriter in my project. Earcut is dependency for meshwriter.  
Here, Meshwriter is an npm package. So, meshwriter has to import Earcut as a module.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 20, 2019, 1:32pm UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/15 "2019-11-20T13:32:31Z")

</div>

Ok, thanks for the information, I don’t know Angular (yet).

---

<div class="post-metadata">

**Author:** ![TheLeftover](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@TheLeftover](https://forum.babylonjs.com/u/TheLeftover)\
**Post date:** [November 20, 2019, 3:37pm UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/16 "2019-11-20T15:37:34Z")

</div>

gopivell, did you try the link approach per Evgeni\_Popov suggestion?

The Earcut module is a standalone module; it has no external dependencies. At the end, it defines “Earcut” as a new global variable. That’s it. I know Angular like to control the environment but surely it does not inhibit something like that!

For the record, MeshWriter makes no mention of Earcut. It needs/wants no pointer to Earcut. It calls BABYLON.PolygonMeshBuilder which uses Earcut.

There are many other uses for PolygonMeshBuilder. Hope this helps.

---

<div class="post-metadata">

**Author:** ![gopivell](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gopivell](https://forum.babylonjs.com/u/gopivell)\
**Post date:** [November 28, 2019, 10:42am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/17 "2019-11-28T10:42:40Z")

</div>

Hi @TheLeftover,

In index.js,  
Kindly add  
var earcut = require(‘earcut’);

and  
in following statement pass this variable  
meshBuilder = new BABYLON.PolygonMeshBuilder(“MeshWriter-”+letter+index+"-"+weeid(), array, scene, earcut);

In package.json,  
add this dependency  
“dependencies”: {  
“earcut”: “^2.2.1”  
}

This issue is caused due to Babylon code in polygonBuilder.ts.  
declare var earcut: any;  
variable is not initialized.  
So, Earcut should be added in babylon’s package.json  
declare var earcut: any = = require(‘earcut’);

Either we can fix in babylonjs or meshwriter.

Thanks  
Gopinath Vellingiri

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 28, 2019, 12:47pm UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/18 "2019-11-28T12:47:57Z")

</div>

It is actually by design in the babylon side to be compatible with both umd and esm modules, so in case of es6 we can inject the dependency. I guess the easiest is to allow the same injection through the meshWriter.

---

<div class="post-metadata">

**Author:** ![TheLeftover](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@TheLeftover](https://forum.babylonjs.com/u/TheLeftover)\
**Post date:** [December 1, 2019, 6:37pm UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/19 "2019-12-01T18:37:43Z")

</div>

gopivell, you have done impressive homework! I will add earcut to the MeshWriter package and pass it to BABYLON as a parameter, per your suggestion. Clever of you to find that!

I’ll get the new version out on NPM this week. I need to update documentation and check for backward compatibility.

Thank you for your patient and helpful persistence. MeshWriter will be better because of this.

* * *

Update: The version used for the playgrounds: [https://sanfrancisco.ca.illuminated.city/js/meshwriter.min.js](https://sanfrancisco.ca.illuminated.city/js/meshwriter.min.js)  
has been updated to incorporate Earcut. It does not seem to have broken anything. When I get a build on NPM, I will post here. You can tell me if it fixes your problem.

---

<div class="post-metadata">

**Author:** ![gopivell](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gopivell](https://forum.babylonjs.com/u/gopivell)\
**Post date:** [December 2, 2019, 9:09am UTC](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656/20 "2019-12-02T09:09:34Z")

</div>

Hi @TheLeftover,  
I shall use new version of meshwriter once it is ready.

Thank you for your patience to help me.

[Next page](https://forum.babylonjs.com/t/3d-text-using-meshwriter-in-angular/6656.md?page=2)
