Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 and Three.js

So I've been attempting to implement THREE.js into my Angular 2 app. I have not been able to successfully import the libraries into Angular 2. There are a few projects that have outdated source code, that run with either Angular 2 (beta) or Angular js. Right now I'd be ecstatic to have a blank project that runs THREE.js, from there I could start my directives.

If anyone knows of any good examples or pointers on how to add THREE.js to an angular 2 project, I'd appreciate it.

like image 259
KitCat Avatar asked Oct 25 '16 22:10

KitCat


People also ask

Can I use three js with Angular?

Now you need to integrate three. js with Angular by importing the three. js to the component. In this way you can render the 3D object, which is component.

Does angular 2 use JavaScript?

Angular 2 is an open source JavaScript framework to build web applications in HTML and JavaScript. This tutorial looks at the various aspects of Angular 2 framework which includes the basics of the framework, the setup of Angular and how to work with the various aspects of the framework.

Can you use TypeScript with three js?

Buy Three. js and TypeScript: Learn Three. js while using TypeScript to create interactive 3D content on the web.

Is it OK to use js in Angular?

Yes. Angular does use . ts files by default. But if you write simple javascript code in them it will still work.


2 Answers

You can implement Three.js into your Angular2 app in 2 steps:

  1. add the library to your modules with npm install three command;
  2. put import * as THREE from 'three' inside your component or service.

For typings support execute npm install --save @types/three.

like image 199
lccmpn Avatar answered Sep 28 '22 14:09

lccmpn


You should use the angular-cli.json file so the 3 steps would be:

  1. Add the library to your modules with npm i three --save command.

  2. Include the library in the "angular-cli.json" file in the "scripts" area:

    "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/three/build/three.min.js",
    ],
    
  3. Put declare var THREE:any; at the top of your component or service.

like image 21
russiansummer Avatar answered Sep 28 '22 13:09

russiansummer