Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using three.js in Angular2

I am trying to integrate a three.js scene into a angular 2 page. I am a beginner in Angular 2 and javascript, I have included the three.js file in the script tag, in index.html, i.e, and in the scene.component.ts file I have the code like this...

//scene.component.ts
import { Component } from 'angular2/core';
import { THREE } from 'three-js/three';
@Component({
    selector: 'ps-scene',
    templateUrl: 'app/webgl/scene.component.html'
})
export class SceneComponent{
    scene = new THREE.Scene();
}
///////////////

The problem is showing in the line - import { THREE } from 'three-js/three';

This is in the same directory path as 'angular2/core'

Thanks in advance.

like image 720
rashmi Avatar asked Dec 19 '22 12:12

rashmi


2 Answers

are you using angular cli?

if so, don't forget to add the reference in your typings.d file:

declare module 'three';

https://github.com/angular/angular-cli#3rd-party-library-installation

3rd Party Library Installation

Simply install your library via npm install lib-name --save and import it in your code.

If the library does not include typings, you can install them using npm:

npm install d3 --save
npm install @types/d3 --save-dev
If the library doesn't have typings available at @types/, you can still use it by manually adding typings for it:

// in src/typings.d.ts
declare module 'typeless-package';

// in src/app/app.component.ts
import * as typelessPackage from 'typeless-package';
typelessPackage.method();
like image 149
Benjamin McFerren Avatar answered Dec 28 '22 06:12

Benjamin McFerren


Checkout this ng2-three-demo, where I use Angular Components to define objects for ThreeJS.

Additionally, there is a presentation on the topic itself in the repo.

like image 37
amcdnl Avatar answered Dec 28 '22 05:12

amcdnl