Using Threejs + OrbitContols in TypeScript










9















I'm not able to get this example of using the aforementioned combo going in TypeScript.



I have <script src="lib/three.min.js"></script> and <script src="lib/OrbitControls.js"></script> in my html <head> and the typescript file in <body>:



/// <reference path="libthree.d.ts" />
...
this.controls = new THREE.OrbitControls(this.camera); //there's the error
this.controls.addEventListener('change', this.render);
...


and



this.controls.update();


in periodically called render() function. For all I know, the setup is identical to the expample, but gives me a huge error (abbreviated) on compilation of the OrbitControls constructor line:



The property 'OrbitControls' does not exist on value of type '{REVISION:string; 
CullFace: {[x: number ...


I guess there's whole Threejs in that error, since Visual Studio crashes the moment I click on it :). Thanks for your help.










share|improve this question




























    9















    I'm not able to get this example of using the aforementioned combo going in TypeScript.



    I have <script src="lib/three.min.js"></script> and <script src="lib/OrbitControls.js"></script> in my html <head> and the typescript file in <body>:



    /// <reference path="libthree.d.ts" />
    ...
    this.controls = new THREE.OrbitControls(this.camera); //there's the error
    this.controls.addEventListener('change', this.render);
    ...


    and



    this.controls.update();


    in periodically called render() function. For all I know, the setup is identical to the expample, but gives me a huge error (abbreviated) on compilation of the OrbitControls constructor line:



    The property 'OrbitControls' does not exist on value of type '{REVISION:string; 
    CullFace: {[x: number ...


    I guess there's whole Threejs in that error, since Visual Studio crashes the moment I click on it :). Thanks for your help.










    share|improve this question


























      9












      9








      9


      4






      I'm not able to get this example of using the aforementioned combo going in TypeScript.



      I have <script src="lib/three.min.js"></script> and <script src="lib/OrbitControls.js"></script> in my html <head> and the typescript file in <body>:



      /// <reference path="libthree.d.ts" />
      ...
      this.controls = new THREE.OrbitControls(this.camera); //there's the error
      this.controls.addEventListener('change', this.render);
      ...


      and



      this.controls.update();


      in periodically called render() function. For all I know, the setup is identical to the expample, but gives me a huge error (abbreviated) on compilation of the OrbitControls constructor line:



      The property 'OrbitControls' does not exist on value of type '{REVISION:string; 
      CullFace: {[x: number ...


      I guess there's whole Threejs in that error, since Visual Studio crashes the moment I click on it :). Thanks for your help.










      share|improve this question
















      I'm not able to get this example of using the aforementioned combo going in TypeScript.



      I have <script src="lib/three.min.js"></script> and <script src="lib/OrbitControls.js"></script> in my html <head> and the typescript file in <body>:



      /// <reference path="libthree.d.ts" />
      ...
      this.controls = new THREE.OrbitControls(this.camera); //there's the error
      this.controls.addEventListener('change', this.render);
      ...


      and



      this.controls.update();


      in periodically called render() function. For all I know, the setup is identical to the expample, but gives me a huge error (abbreviated) on compilation of the OrbitControls constructor line:



      The property 'OrbitControls' does not exist on value of type '{REVISION:string; 
      CullFace: {[x: number ...


      I guess there's whole Threejs in that error, since Visual Studio crashes the moment I click on it :). Thanks for your help.







      javascript html three.js typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 19 '13 at 13:57







      asdf

















      asked Oct 18 '13 at 8:05









      asdfasdf

      289312




      289312






















          11 Answers
          11






          active

          oldest

          votes


















          10














          After few hours spend on this problem, I ended up creating a new package: three-orbitcontrols-ts



          import * as THREE from 'three';
          import OrbitControls from 'three-orbitcontrols-ts';

          const controls = new OrbitControls(camera, renderer.domElement);





          share|improve this answer


















          • 1





            you are a very good soul! thanks mate!

            – messerbill
            Feb 21 '17 at 18:40











          • Has anyone found something like this but for the trackball controls?

            – Pablo Kvitca
            Jul 23 '18 at 20:04











          • Great work, but now we have another package to get out of sync. For instance, this package is already missing the saveState routine which is used by some other components I am using.

            – user9315861
            Jul 31 '18 at 18:46


















          5














          Importing the OrbitControls directly from the source file after importing THREE worked great for me (without using the three-orbitcontrols-ts package):



          import * as THREE from 'three';
          import 'three/examples/js/controls/OrbitControls';


          Using only the three and @types/three packages






          share|improve this answer























          • looks like this post this should be marked as correct answer instead.

            – daniel.bavrin
            Sep 15 '17 at 19:10











          • The problem is that after the first import THREE is an imported object, not a property on the window object as three/examples/js/controls/OrbitControls expects in order to install itself.

            – user9315861
            Jul 22 '18 at 11:43


















          3














          While previous answers may work out to some degree, you should go with the ready made definition files from DefinitelyTyped. They also define OrbitControl's properties and methods.






          share|improve this answer




















          • 1





            the link is broken

            – smartmeta
            Mar 5 '17 at 11:21











          • Sorry, here is the updated link: github.com/DefinitelyTyped/DefinitelyTyped/blob/master/three/…

            – BenR
            Mar 6 '17 at 12:31






          • 2





            @BenR The updated link is broken again :(

            – N15M0_jk
            Sep 8 '17 at 8:42











          • This does not answer the question of how to bring in OrbitControls themselves.

            – user9315861
            Jul 31 '18 at 18:45


















          1














          Feeling kind of silly since the solution (or workaround at least) turns out to be quite simple...



          Just declare the OrbitControls variable:



          declare var THREE.OrbitControls: any; // even "declare var THREE.OrbitControls;" will do the trick


          There are still some compilation errors but it works.






          share|improve this answer
































            1














            I tried some of the suggested solutions and could not get it to work right so after some experimentation, I ended up with:



            import * as THREE from 'three';
            import OrbitControlsLibrary = require('three-orbit-controls');
            var OrbitControls = OrbitControlsLibrary(THREE);
            ...
            this.controls = new OrbitControls(this.camera);


            I am using webpack bundling and with this I did not have to add the script tag in the HTML template.






            share|improve this answer






























              1














              Unfortunately none of above answers solved problem for me. I have found solution without installing third party packages by creating three.js file with below content:



              import * as THREE from 'three';
              window.THREE = THREE;
              require('three/examples/js/controls/OrbitControls');

              export default THREE;


              and then import THREE from './three';






              share|improve this answer






























                0














                The answer didn't work for me in eclipse typescript, since it conflicted with the reference statement earlier. But the following works:



                declare module THREE export var OrbitControls





                share|improve this answer






























                  0














                  After adding the three-orbitcontrols typings and three-orbit-controls npm package (why aren't these named the same?), I was still having issues getting it to work in Typescript. This ended up working for me in case it's helpful for anyone else who doesn't want to use the workaround:



                  import * as three from 'three';
                  three.OrbitControls = require('three-orbit-controls')(three);

                  let camera = new three.PerspectiveCamera(75, aspectRatio, 1, 1000);
                  let renderer = new three.WebGLRenderer();
                  let controls = new three.OrbitControls(camera, renderer.domElement);


                  As of today, it seems that the OrbitControls npm package is using an old style of exports that requires an old-style require statement that passes in the Three library.






                  share|improve this answer






























                    0














                    outside ts:



                    var THREEa = THREE;


                    inside ts:



                    var controls = new THREEa.OrbitControls(pCamera, pContainer);





                    share|improve this answer






























                      0














                      I was not able to get anything from here this post actually worked and seemed consistent or had enough detail to be actionable. Hopefully, this answer might provide some insight to others.



                      In my objects I already had three working by using typings to extract the THREE ts file. This was very reasonable and put the ts file in typings/globals/three.



                      Here are the steps assuming that you have downloaded and installed node.js. This also installs npm (Node Project Manager) which you can run from a command prompt to so all the steps below.



                      npm install typings --global
                      typings install dt~three --global --save


                      At this point all the basic THREE types are available once I "include" the ts file in my object:



                      /// <reference path="typings/globals/three/index.d.ts" />


                      At this point OrbitControls is not defined because the basic three package does not include it. However, there is ts data in the typings database. So I installed it as well:



                      typings install --globals three-orbitcontrols --save


                      It puts the ts file here:



                      node_modulesthree-orbitcontrols-tsdistindex.d.ts


                      I tried a number of things to get it to work but I am missing something. Generally the error was that THREE.OrbitControls was not defined. If I removed the THREE then it generated non-working JavaScript.



                      What I finally did which I consider a workaround is edit typings/globals/three/index.d.ts and cut and pasted the contents of typings/globals/three/OrbitControls/index.d.ts into the THREE namespace at the end.



                      At the top of the OrbitControls/index.d.ts it calls out <reference types="three" /> which doesn't work because in that context it cannot find "three". Once I worked around that it compiled but it generated non-working code.






                      share|improve this answer






























                        0














                        I've also had some bad luck implementing the suggested solutions as none of them made the typescript compiler happy. Couldn't overwrite THREE.OrbitControls at compile time because it was set to read-only. My solution was as follows:



                        Install both 'three' and 'three-orbit-controls' with npm. (This is kinda dumb but necessary since 'three' already contains all the code for the orbit controls in the examples/js/controls/OrbitControl.js file but doesn't export it in a way that typescript understands. If you don't want to mess with your 3rd party libraries, just install 'three-orbit-controls' alongside 'three' which contains a duplicate of the code but exports it in a nice way.



                        Now use it like below:




                        import * as THREE from 'three';

                        import * as OrbitControlsFunction from 'three-orbit-controls';

                        const OrbitControls = OrbitControlsFunction(THREE); // OrbitControls is now your constructor

                        const controls: THREE.OrbitControls = new OrbitControls(camera, element); // Code as you would from here on out.







                        share|improve this answer






















                          Your Answer






                          StackExchange.ifUsing("editor", function ()
                          StackExchange.using("externalEditor", function ()
                          StackExchange.using("snippets", function ()
                          StackExchange.snippets.init();
                          );
                          );
                          , "code-snippets");

                          StackExchange.ready(function()
                          var channelOptions =
                          tags: "".split(" "),
                          id: "1"
                          ;
                          initTagRenderer("".split(" "), "".split(" "), channelOptions);

                          StackExchange.using("externalEditor", function()
                          // Have to fire editor after snippets, if snippets enabled
                          if (StackExchange.settings.snippets.snippetsEnabled)
                          StackExchange.using("snippets", function()
                          createEditor();
                          );

                          else
                          createEditor();

                          );

                          function createEditor()
                          StackExchange.prepareEditor(
                          heartbeatType: 'answer',
                          autoActivateHeartbeat: false,
                          convertImagesToLinks: true,
                          noModals: true,
                          showLowRepImageUploadWarning: true,
                          reputationToPostImages: 10,
                          bindNavPrevention: true,
                          postfix: "",
                          imageUploader:
                          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                          allowUrls: true
                          ,
                          onDemand: true,
                          discardSelector: ".discard-answer"
                          ,immediatelyShowMarkdownHelp:true
                          );



                          );













                          draft saved

                          draft discarded


















                          StackExchange.ready(
                          function ()
                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19444592%2fusing-threejs-orbitcontols-in-typescript%23new-answer', 'question_page');

                          );

                          Post as a guest















                          Required, but never shown

























                          11 Answers
                          11






                          active

                          oldest

                          votes








                          11 Answers
                          11






                          active

                          oldest

                          votes









                          active

                          oldest

                          votes






                          active

                          oldest

                          votes









                          10














                          After few hours spend on this problem, I ended up creating a new package: three-orbitcontrols-ts



                          import * as THREE from 'three';
                          import OrbitControls from 'three-orbitcontrols-ts';

                          const controls = new OrbitControls(camera, renderer.domElement);





                          share|improve this answer


















                          • 1





                            you are a very good soul! thanks mate!

                            – messerbill
                            Feb 21 '17 at 18:40











                          • Has anyone found something like this but for the trackball controls?

                            – Pablo Kvitca
                            Jul 23 '18 at 20:04











                          • Great work, but now we have another package to get out of sync. For instance, this package is already missing the saveState routine which is used by some other components I am using.

                            – user9315861
                            Jul 31 '18 at 18:46















                          10














                          After few hours spend on this problem, I ended up creating a new package: three-orbitcontrols-ts



                          import * as THREE from 'three';
                          import OrbitControls from 'three-orbitcontrols-ts';

                          const controls = new OrbitControls(camera, renderer.domElement);





                          share|improve this answer


















                          • 1





                            you are a very good soul! thanks mate!

                            – messerbill
                            Feb 21 '17 at 18:40











                          • Has anyone found something like this but for the trackball controls?

                            – Pablo Kvitca
                            Jul 23 '18 at 20:04











                          • Great work, but now we have another package to get out of sync. For instance, this package is already missing the saveState routine which is used by some other components I am using.

                            – user9315861
                            Jul 31 '18 at 18:46













                          10












                          10








                          10







                          After few hours spend on this problem, I ended up creating a new package: three-orbitcontrols-ts



                          import * as THREE from 'three';
                          import OrbitControls from 'three-orbitcontrols-ts';

                          const controls = new OrbitControls(camera, renderer.domElement);





                          share|improve this answer













                          After few hours spend on this problem, I ended up creating a new package: three-orbitcontrols-ts



                          import * as THREE from 'three';
                          import OrbitControls from 'three-orbitcontrols-ts';

                          const controls = new OrbitControls(camera, renderer.domElement);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 7 '17 at 10:34









                          nicolaspanelnicolaspanel

                          492716




                          492716







                          • 1





                            you are a very good soul! thanks mate!

                            – messerbill
                            Feb 21 '17 at 18:40











                          • Has anyone found something like this but for the trackball controls?

                            – Pablo Kvitca
                            Jul 23 '18 at 20:04











                          • Great work, but now we have another package to get out of sync. For instance, this package is already missing the saveState routine which is used by some other components I am using.

                            – user9315861
                            Jul 31 '18 at 18:46












                          • 1





                            you are a very good soul! thanks mate!

                            – messerbill
                            Feb 21 '17 at 18:40











                          • Has anyone found something like this but for the trackball controls?

                            – Pablo Kvitca
                            Jul 23 '18 at 20:04











                          • Great work, but now we have another package to get out of sync. For instance, this package is already missing the saveState routine which is used by some other components I am using.

                            – user9315861
                            Jul 31 '18 at 18:46







                          1




                          1





                          you are a very good soul! thanks mate!

                          – messerbill
                          Feb 21 '17 at 18:40





                          you are a very good soul! thanks mate!

                          – messerbill
                          Feb 21 '17 at 18:40













                          Has anyone found something like this but for the trackball controls?

                          – Pablo Kvitca
                          Jul 23 '18 at 20:04





                          Has anyone found something like this but for the trackball controls?

                          – Pablo Kvitca
                          Jul 23 '18 at 20:04













                          Great work, but now we have another package to get out of sync. For instance, this package is already missing the saveState routine which is used by some other components I am using.

                          – user9315861
                          Jul 31 '18 at 18:46





                          Great work, but now we have another package to get out of sync. For instance, this package is already missing the saveState routine which is used by some other components I am using.

                          – user9315861
                          Jul 31 '18 at 18:46













                          5














                          Importing the OrbitControls directly from the source file after importing THREE worked great for me (without using the three-orbitcontrols-ts package):



                          import * as THREE from 'three';
                          import 'three/examples/js/controls/OrbitControls';


                          Using only the three and @types/three packages






                          share|improve this answer























                          • looks like this post this should be marked as correct answer instead.

                            – daniel.bavrin
                            Sep 15 '17 at 19:10











                          • The problem is that after the first import THREE is an imported object, not a property on the window object as three/examples/js/controls/OrbitControls expects in order to install itself.

                            – user9315861
                            Jul 22 '18 at 11:43















                          5














                          Importing the OrbitControls directly from the source file after importing THREE worked great for me (without using the three-orbitcontrols-ts package):



                          import * as THREE from 'three';
                          import 'three/examples/js/controls/OrbitControls';


                          Using only the three and @types/three packages






                          share|improve this answer























                          • looks like this post this should be marked as correct answer instead.

                            – daniel.bavrin
                            Sep 15 '17 at 19:10











                          • The problem is that after the first import THREE is an imported object, not a property on the window object as three/examples/js/controls/OrbitControls expects in order to install itself.

                            – user9315861
                            Jul 22 '18 at 11:43













                          5












                          5








                          5







                          Importing the OrbitControls directly from the source file after importing THREE worked great for me (without using the three-orbitcontrols-ts package):



                          import * as THREE from 'three';
                          import 'three/examples/js/controls/OrbitControls';


                          Using only the three and @types/three packages






                          share|improve this answer













                          Importing the OrbitControls directly from the source file after importing THREE worked great for me (without using the three-orbitcontrols-ts package):



                          import * as THREE from 'three';
                          import 'three/examples/js/controls/OrbitControls';


                          Using only the three and @types/three packages







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 23 '17 at 19:04









                          sporshsporsh

                          28933




                          28933












                          • looks like this post this should be marked as correct answer instead.

                            – daniel.bavrin
                            Sep 15 '17 at 19:10











                          • The problem is that after the first import THREE is an imported object, not a property on the window object as three/examples/js/controls/OrbitControls expects in order to install itself.

                            – user9315861
                            Jul 22 '18 at 11:43

















                          • looks like this post this should be marked as correct answer instead.

                            – daniel.bavrin
                            Sep 15 '17 at 19:10











                          • The problem is that after the first import THREE is an imported object, not a property on the window object as three/examples/js/controls/OrbitControls expects in order to install itself.

                            – user9315861
                            Jul 22 '18 at 11:43
















                          looks like this post this should be marked as correct answer instead.

                          – daniel.bavrin
                          Sep 15 '17 at 19:10





                          looks like this post this should be marked as correct answer instead.

                          – daniel.bavrin
                          Sep 15 '17 at 19:10













                          The problem is that after the first import THREE is an imported object, not a property on the window object as three/examples/js/controls/OrbitControls expects in order to install itself.

                          – user9315861
                          Jul 22 '18 at 11:43





                          The problem is that after the first import THREE is an imported object, not a property on the window object as three/examples/js/controls/OrbitControls expects in order to install itself.

                          – user9315861
                          Jul 22 '18 at 11:43











                          3














                          While previous answers may work out to some degree, you should go with the ready made definition files from DefinitelyTyped. They also define OrbitControl's properties and methods.






                          share|improve this answer




















                          • 1





                            the link is broken

                            – smartmeta
                            Mar 5 '17 at 11:21











                          • Sorry, here is the updated link: github.com/DefinitelyTyped/DefinitelyTyped/blob/master/three/…

                            – BenR
                            Mar 6 '17 at 12:31






                          • 2





                            @BenR The updated link is broken again :(

                            – N15M0_jk
                            Sep 8 '17 at 8:42











                          • This does not answer the question of how to bring in OrbitControls themselves.

                            – user9315861
                            Jul 31 '18 at 18:45















                          3














                          While previous answers may work out to some degree, you should go with the ready made definition files from DefinitelyTyped. They also define OrbitControl's properties and methods.






                          share|improve this answer




















                          • 1





                            the link is broken

                            – smartmeta
                            Mar 5 '17 at 11:21











                          • Sorry, here is the updated link: github.com/DefinitelyTyped/DefinitelyTyped/blob/master/three/…

                            – BenR
                            Mar 6 '17 at 12:31






                          • 2





                            @BenR The updated link is broken again :(

                            – N15M0_jk
                            Sep 8 '17 at 8:42











                          • This does not answer the question of how to bring in OrbitControls themselves.

                            – user9315861
                            Jul 31 '18 at 18:45













                          3












                          3








                          3







                          While previous answers may work out to some degree, you should go with the ready made definition files from DefinitelyTyped. They also define OrbitControl's properties and methods.






                          share|improve this answer















                          While previous answers may work out to some degree, you should go with the ready made definition files from DefinitelyTyped. They also define OrbitControl's properties and methods.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 7 '17 at 9:08

























                          answered Dec 2 '14 at 4:48









                          BenRBenR

                          275210




                          275210







                          • 1





                            the link is broken

                            – smartmeta
                            Mar 5 '17 at 11:21











                          • Sorry, here is the updated link: github.com/DefinitelyTyped/DefinitelyTyped/blob/master/three/…

                            – BenR
                            Mar 6 '17 at 12:31






                          • 2





                            @BenR The updated link is broken again :(

                            – N15M0_jk
                            Sep 8 '17 at 8:42











                          • This does not answer the question of how to bring in OrbitControls themselves.

                            – user9315861
                            Jul 31 '18 at 18:45












                          • 1





                            the link is broken

                            – smartmeta
                            Mar 5 '17 at 11:21











                          • Sorry, here is the updated link: github.com/DefinitelyTyped/DefinitelyTyped/blob/master/three/…

                            – BenR
                            Mar 6 '17 at 12:31






                          • 2





                            @BenR The updated link is broken again :(

                            – N15M0_jk
                            Sep 8 '17 at 8:42











                          • This does not answer the question of how to bring in OrbitControls themselves.

                            – user9315861
                            Jul 31 '18 at 18:45







                          1




                          1





                          the link is broken

                          – smartmeta
                          Mar 5 '17 at 11:21





                          the link is broken

                          – smartmeta
                          Mar 5 '17 at 11:21













                          Sorry, here is the updated link: github.com/DefinitelyTyped/DefinitelyTyped/blob/master/three/…

                          – BenR
                          Mar 6 '17 at 12:31





                          Sorry, here is the updated link: github.com/DefinitelyTyped/DefinitelyTyped/blob/master/three/…

                          – BenR
                          Mar 6 '17 at 12:31




                          2




                          2





                          @BenR The updated link is broken again :(

                          – N15M0_jk
                          Sep 8 '17 at 8:42





                          @BenR The updated link is broken again :(

                          – N15M0_jk
                          Sep 8 '17 at 8:42













                          This does not answer the question of how to bring in OrbitControls themselves.

                          – user9315861
                          Jul 31 '18 at 18:45





                          This does not answer the question of how to bring in OrbitControls themselves.

                          – user9315861
                          Jul 31 '18 at 18:45











                          1














                          Feeling kind of silly since the solution (or workaround at least) turns out to be quite simple...



                          Just declare the OrbitControls variable:



                          declare var THREE.OrbitControls: any; // even "declare var THREE.OrbitControls;" will do the trick


                          There are still some compilation errors but it works.






                          share|improve this answer





























                            1














                            Feeling kind of silly since the solution (or workaround at least) turns out to be quite simple...



                            Just declare the OrbitControls variable:



                            declare var THREE.OrbitControls: any; // even "declare var THREE.OrbitControls;" will do the trick


                            There are still some compilation errors but it works.






                            share|improve this answer



























                              1












                              1








                              1







                              Feeling kind of silly since the solution (or workaround at least) turns out to be quite simple...



                              Just declare the OrbitControls variable:



                              declare var THREE.OrbitControls: any; // even "declare var THREE.OrbitControls;" will do the trick


                              There are still some compilation errors but it works.






                              share|improve this answer















                              Feeling kind of silly since the solution (or workaround at least) turns out to be quite simple...



                              Just declare the OrbitControls variable:



                              declare var THREE.OrbitControls: any; // even "declare var THREE.OrbitControls;" will do the trick


                              There are still some compilation errors but it works.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Oct 21 '13 at 13:09

























                              answered Oct 21 '13 at 9:22









                              asdfasdf

                              289312




                              289312





















                                  1














                                  I tried some of the suggested solutions and could not get it to work right so after some experimentation, I ended up with:



                                  import * as THREE from 'three';
                                  import OrbitControlsLibrary = require('three-orbit-controls');
                                  var OrbitControls = OrbitControlsLibrary(THREE);
                                  ...
                                  this.controls = new OrbitControls(this.camera);


                                  I am using webpack bundling and with this I did not have to add the script tag in the HTML template.






                                  share|improve this answer



























                                    1














                                    I tried some of the suggested solutions and could not get it to work right so after some experimentation, I ended up with:



                                    import * as THREE from 'three';
                                    import OrbitControlsLibrary = require('three-orbit-controls');
                                    var OrbitControls = OrbitControlsLibrary(THREE);
                                    ...
                                    this.controls = new OrbitControls(this.camera);


                                    I am using webpack bundling and with this I did not have to add the script tag in the HTML template.






                                    share|improve this answer

























                                      1












                                      1








                                      1







                                      I tried some of the suggested solutions and could not get it to work right so after some experimentation, I ended up with:



                                      import * as THREE from 'three';
                                      import OrbitControlsLibrary = require('three-orbit-controls');
                                      var OrbitControls = OrbitControlsLibrary(THREE);
                                      ...
                                      this.controls = new OrbitControls(this.camera);


                                      I am using webpack bundling and with this I did not have to add the script tag in the HTML template.






                                      share|improve this answer













                                      I tried some of the suggested solutions and could not get it to work right so after some experimentation, I ended up with:



                                      import * as THREE from 'three';
                                      import OrbitControlsLibrary = require('three-orbit-controls');
                                      var OrbitControls = OrbitControlsLibrary(THREE);
                                      ...
                                      this.controls = new OrbitControls(this.camera);


                                      I am using webpack bundling and with this I did not have to add the script tag in the HTML template.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 22 '16 at 21:29









                                      Aaron BotelerAaron Boteler

                                      7114




                                      7114





















                                          1














                                          Unfortunately none of above answers solved problem for me. I have found solution without installing third party packages by creating three.js file with below content:



                                          import * as THREE from 'three';
                                          window.THREE = THREE;
                                          require('three/examples/js/controls/OrbitControls');

                                          export default THREE;


                                          and then import THREE from './three';






                                          share|improve this answer



























                                            1














                                            Unfortunately none of above answers solved problem for me. I have found solution without installing third party packages by creating three.js file with below content:



                                            import * as THREE from 'three';
                                            window.THREE = THREE;
                                            require('three/examples/js/controls/OrbitControls');

                                            export default THREE;


                                            and then import THREE from './three';






                                            share|improve this answer

























                                              1












                                              1








                                              1







                                              Unfortunately none of above answers solved problem for me. I have found solution without installing third party packages by creating three.js file with below content:



                                              import * as THREE from 'three';
                                              window.THREE = THREE;
                                              require('three/examples/js/controls/OrbitControls');

                                              export default THREE;


                                              and then import THREE from './three';






                                              share|improve this answer













                                              Unfortunately none of above answers solved problem for me. I have found solution without installing third party packages by creating three.js file with below content:



                                              import * as THREE from 'three';
                                              window.THREE = THREE;
                                              require('three/examples/js/controls/OrbitControls');

                                              export default THREE;


                                              and then import THREE from './three';







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Oct 6 '18 at 23:04









                                              mtxmtx

                                              304519




                                              304519





















                                                  0














                                                  The answer didn't work for me in eclipse typescript, since it conflicted with the reference statement earlier. But the following works:



                                                  declare module THREE export var OrbitControls





                                                  share|improve this answer



























                                                    0














                                                    The answer didn't work for me in eclipse typescript, since it conflicted with the reference statement earlier. But the following works:



                                                    declare module THREE export var OrbitControls





                                                    share|improve this answer

























                                                      0












                                                      0








                                                      0







                                                      The answer didn't work for me in eclipse typescript, since it conflicted with the reference statement earlier. But the following works:



                                                      declare module THREE export var OrbitControls





                                                      share|improve this answer













                                                      The answer didn't work for me in eclipse typescript, since it conflicted with the reference statement earlier. But the following works:



                                                      declare module THREE export var OrbitControls






                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Feb 11 '14 at 5:16









                                                      DejayDejay

                                                      31




                                                      31





















                                                          0














                                                          After adding the three-orbitcontrols typings and three-orbit-controls npm package (why aren't these named the same?), I was still having issues getting it to work in Typescript. This ended up working for me in case it's helpful for anyone else who doesn't want to use the workaround:



                                                          import * as three from 'three';
                                                          three.OrbitControls = require('three-orbit-controls')(three);

                                                          let camera = new three.PerspectiveCamera(75, aspectRatio, 1, 1000);
                                                          let renderer = new three.WebGLRenderer();
                                                          let controls = new three.OrbitControls(camera, renderer.domElement);


                                                          As of today, it seems that the OrbitControls npm package is using an old style of exports that requires an old-style require statement that passes in the Three library.






                                                          share|improve this answer



























                                                            0














                                                            After adding the three-orbitcontrols typings and three-orbit-controls npm package (why aren't these named the same?), I was still having issues getting it to work in Typescript. This ended up working for me in case it's helpful for anyone else who doesn't want to use the workaround:



                                                            import * as three from 'three';
                                                            three.OrbitControls = require('three-orbit-controls')(three);

                                                            let camera = new three.PerspectiveCamera(75, aspectRatio, 1, 1000);
                                                            let renderer = new three.WebGLRenderer();
                                                            let controls = new three.OrbitControls(camera, renderer.domElement);


                                                            As of today, it seems that the OrbitControls npm package is using an old style of exports that requires an old-style require statement that passes in the Three library.






                                                            share|improve this answer

























                                                              0












                                                              0








                                                              0







                                                              After adding the three-orbitcontrols typings and three-orbit-controls npm package (why aren't these named the same?), I was still having issues getting it to work in Typescript. This ended up working for me in case it's helpful for anyone else who doesn't want to use the workaround:



                                                              import * as three from 'three';
                                                              three.OrbitControls = require('three-orbit-controls')(three);

                                                              let camera = new three.PerspectiveCamera(75, aspectRatio, 1, 1000);
                                                              let renderer = new three.WebGLRenderer();
                                                              let controls = new three.OrbitControls(camera, renderer.domElement);


                                                              As of today, it seems that the OrbitControls npm package is using an old style of exports that requires an old-style require statement that passes in the Three library.






                                                              share|improve this answer













                                                              After adding the three-orbitcontrols typings and three-orbit-controls npm package (why aren't these named the same?), I was still having issues getting it to work in Typescript. This ended up working for me in case it's helpful for anyone else who doesn't want to use the workaround:



                                                              import * as three from 'three';
                                                              three.OrbitControls = require('three-orbit-controls')(three);

                                                              let camera = new three.PerspectiveCamera(75, aspectRatio, 1, 1000);
                                                              let renderer = new three.WebGLRenderer();
                                                              let controls = new three.OrbitControls(camera, renderer.domElement);


                                                              As of today, it seems that the OrbitControls npm package is using an old style of exports that requires an old-style require statement that passes in the Three library.







                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Jul 13 '16 at 22:02









                                                              DaveDave

                                                              2,5461921




                                                              2,5461921





















                                                                  0














                                                                  outside ts:



                                                                  var THREEa = THREE;


                                                                  inside ts:



                                                                  var controls = new THREEa.OrbitControls(pCamera, pContainer);





                                                                  share|improve this answer



























                                                                    0














                                                                    outside ts:



                                                                    var THREEa = THREE;


                                                                    inside ts:



                                                                    var controls = new THREEa.OrbitControls(pCamera, pContainer);





                                                                    share|improve this answer

























                                                                      0












                                                                      0








                                                                      0







                                                                      outside ts:



                                                                      var THREEa = THREE;


                                                                      inside ts:



                                                                      var controls = new THREEa.OrbitControls(pCamera, pContainer);





                                                                      share|improve this answer













                                                                      outside ts:



                                                                      var THREEa = THREE;


                                                                      inside ts:



                                                                      var controls = new THREEa.OrbitControls(pCamera, pContainer);






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Jan 8 '17 at 14:00









                                                                      Lior TrauLior Trau

                                                                      381213




                                                                      381213





















                                                                          0














                                                                          I was not able to get anything from here this post actually worked and seemed consistent or had enough detail to be actionable. Hopefully, this answer might provide some insight to others.



                                                                          In my objects I already had three working by using typings to extract the THREE ts file. This was very reasonable and put the ts file in typings/globals/three.



                                                                          Here are the steps assuming that you have downloaded and installed node.js. This also installs npm (Node Project Manager) which you can run from a command prompt to so all the steps below.



                                                                          npm install typings --global
                                                                          typings install dt~three --global --save


                                                                          At this point all the basic THREE types are available once I "include" the ts file in my object:



                                                                          /// <reference path="typings/globals/three/index.d.ts" />


                                                                          At this point OrbitControls is not defined because the basic three package does not include it. However, there is ts data in the typings database. So I installed it as well:



                                                                          typings install --globals three-orbitcontrols --save


                                                                          It puts the ts file here:



                                                                          node_modulesthree-orbitcontrols-tsdistindex.d.ts


                                                                          I tried a number of things to get it to work but I am missing something. Generally the error was that THREE.OrbitControls was not defined. If I removed the THREE then it generated non-working JavaScript.



                                                                          What I finally did which I consider a workaround is edit typings/globals/three/index.d.ts and cut and pasted the contents of typings/globals/three/OrbitControls/index.d.ts into the THREE namespace at the end.



                                                                          At the top of the OrbitControls/index.d.ts it calls out <reference types="three" /> which doesn't work because in that context it cannot find "three". Once I worked around that it compiled but it generated non-working code.






                                                                          share|improve this answer



























                                                                            0














                                                                            I was not able to get anything from here this post actually worked and seemed consistent or had enough detail to be actionable. Hopefully, this answer might provide some insight to others.



                                                                            In my objects I already had three working by using typings to extract the THREE ts file. This was very reasonable and put the ts file in typings/globals/three.



                                                                            Here are the steps assuming that you have downloaded and installed node.js. This also installs npm (Node Project Manager) which you can run from a command prompt to so all the steps below.



                                                                            npm install typings --global
                                                                            typings install dt~three --global --save


                                                                            At this point all the basic THREE types are available once I "include" the ts file in my object:



                                                                            /// <reference path="typings/globals/three/index.d.ts" />


                                                                            At this point OrbitControls is not defined because the basic three package does not include it. However, there is ts data in the typings database. So I installed it as well:



                                                                            typings install --globals three-orbitcontrols --save


                                                                            It puts the ts file here:



                                                                            node_modulesthree-orbitcontrols-tsdistindex.d.ts


                                                                            I tried a number of things to get it to work but I am missing something. Generally the error was that THREE.OrbitControls was not defined. If I removed the THREE then it generated non-working JavaScript.



                                                                            What I finally did which I consider a workaround is edit typings/globals/three/index.d.ts and cut and pasted the contents of typings/globals/three/OrbitControls/index.d.ts into the THREE namespace at the end.



                                                                            At the top of the OrbitControls/index.d.ts it calls out <reference types="three" /> which doesn't work because in that context it cannot find "three". Once I worked around that it compiled but it generated non-working code.






                                                                            share|improve this answer

























                                                                              0












                                                                              0








                                                                              0







                                                                              I was not able to get anything from here this post actually worked and seemed consistent or had enough detail to be actionable. Hopefully, this answer might provide some insight to others.



                                                                              In my objects I already had three working by using typings to extract the THREE ts file. This was very reasonable and put the ts file in typings/globals/three.



                                                                              Here are the steps assuming that you have downloaded and installed node.js. This also installs npm (Node Project Manager) which you can run from a command prompt to so all the steps below.



                                                                              npm install typings --global
                                                                              typings install dt~three --global --save


                                                                              At this point all the basic THREE types are available once I "include" the ts file in my object:



                                                                              /// <reference path="typings/globals/three/index.d.ts" />


                                                                              At this point OrbitControls is not defined because the basic three package does not include it. However, there is ts data in the typings database. So I installed it as well:



                                                                              typings install --globals three-orbitcontrols --save


                                                                              It puts the ts file here:



                                                                              node_modulesthree-orbitcontrols-tsdistindex.d.ts


                                                                              I tried a number of things to get it to work but I am missing something. Generally the error was that THREE.OrbitControls was not defined. If I removed the THREE then it generated non-working JavaScript.



                                                                              What I finally did which I consider a workaround is edit typings/globals/three/index.d.ts and cut and pasted the contents of typings/globals/three/OrbitControls/index.d.ts into the THREE namespace at the end.



                                                                              At the top of the OrbitControls/index.d.ts it calls out <reference types="three" /> which doesn't work because in that context it cannot find "three". Once I worked around that it compiled but it generated non-working code.






                                                                              share|improve this answer













                                                                              I was not able to get anything from here this post actually worked and seemed consistent or had enough detail to be actionable. Hopefully, this answer might provide some insight to others.



                                                                              In my objects I already had three working by using typings to extract the THREE ts file. This was very reasonable and put the ts file in typings/globals/three.



                                                                              Here are the steps assuming that you have downloaded and installed node.js. This also installs npm (Node Project Manager) which you can run from a command prompt to so all the steps below.



                                                                              npm install typings --global
                                                                              typings install dt~three --global --save


                                                                              At this point all the basic THREE types are available once I "include" the ts file in my object:



                                                                              /// <reference path="typings/globals/three/index.d.ts" />


                                                                              At this point OrbitControls is not defined because the basic three package does not include it. However, there is ts data in the typings database. So I installed it as well:



                                                                              typings install --globals three-orbitcontrols --save


                                                                              It puts the ts file here:



                                                                              node_modulesthree-orbitcontrols-tsdistindex.d.ts


                                                                              I tried a number of things to get it to work but I am missing something. Generally the error was that THREE.OrbitControls was not defined. If I removed the THREE then it generated non-working JavaScript.



                                                                              What I finally did which I consider a workaround is edit typings/globals/three/index.d.ts and cut and pasted the contents of typings/globals/three/OrbitControls/index.d.ts into the THREE namespace at the end.



                                                                              At the top of the OrbitControls/index.d.ts it calls out <reference types="three" /> which doesn't work because in that context it cannot find "three". Once I worked around that it compiled but it generated non-working code.







                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered May 17 '17 at 19:00









                                                                              Ken CowsiwinKen Cowsiwin

                                                                              84




                                                                              84





















                                                                                  0














                                                                                  I've also had some bad luck implementing the suggested solutions as none of them made the typescript compiler happy. Couldn't overwrite THREE.OrbitControls at compile time because it was set to read-only. My solution was as follows:



                                                                                  Install both 'three' and 'three-orbit-controls' with npm. (This is kinda dumb but necessary since 'three' already contains all the code for the orbit controls in the examples/js/controls/OrbitControl.js file but doesn't export it in a way that typescript understands. If you don't want to mess with your 3rd party libraries, just install 'three-orbit-controls' alongside 'three' which contains a duplicate of the code but exports it in a nice way.



                                                                                  Now use it like below:




                                                                                  import * as THREE from 'three';

                                                                                  import * as OrbitControlsFunction from 'three-orbit-controls';

                                                                                  const OrbitControls = OrbitControlsFunction(THREE); // OrbitControls is now your constructor

                                                                                  const controls: THREE.OrbitControls = new OrbitControls(camera, element); // Code as you would from here on out.







                                                                                  share|improve this answer



























                                                                                    0














                                                                                    I've also had some bad luck implementing the suggested solutions as none of them made the typescript compiler happy. Couldn't overwrite THREE.OrbitControls at compile time because it was set to read-only. My solution was as follows:



                                                                                    Install both 'three' and 'three-orbit-controls' with npm. (This is kinda dumb but necessary since 'three' already contains all the code for the orbit controls in the examples/js/controls/OrbitControl.js file but doesn't export it in a way that typescript understands. If you don't want to mess with your 3rd party libraries, just install 'three-orbit-controls' alongside 'three' which contains a duplicate of the code but exports it in a nice way.



                                                                                    Now use it like below:




                                                                                    import * as THREE from 'three';

                                                                                    import * as OrbitControlsFunction from 'three-orbit-controls';

                                                                                    const OrbitControls = OrbitControlsFunction(THREE); // OrbitControls is now your constructor

                                                                                    const controls: THREE.OrbitControls = new OrbitControls(camera, element); // Code as you would from here on out.







                                                                                    share|improve this answer

























                                                                                      0












                                                                                      0








                                                                                      0







                                                                                      I've also had some bad luck implementing the suggested solutions as none of them made the typescript compiler happy. Couldn't overwrite THREE.OrbitControls at compile time because it was set to read-only. My solution was as follows:



                                                                                      Install both 'three' and 'three-orbit-controls' with npm. (This is kinda dumb but necessary since 'three' already contains all the code for the orbit controls in the examples/js/controls/OrbitControl.js file but doesn't export it in a way that typescript understands. If you don't want to mess with your 3rd party libraries, just install 'three-orbit-controls' alongside 'three' which contains a duplicate of the code but exports it in a nice way.



                                                                                      Now use it like below:




                                                                                      import * as THREE from 'three';

                                                                                      import * as OrbitControlsFunction from 'three-orbit-controls';

                                                                                      const OrbitControls = OrbitControlsFunction(THREE); // OrbitControls is now your constructor

                                                                                      const controls: THREE.OrbitControls = new OrbitControls(camera, element); // Code as you would from here on out.







                                                                                      share|improve this answer













                                                                                      I've also had some bad luck implementing the suggested solutions as none of them made the typescript compiler happy. Couldn't overwrite THREE.OrbitControls at compile time because it was set to read-only. My solution was as follows:



                                                                                      Install both 'three' and 'three-orbit-controls' with npm. (This is kinda dumb but necessary since 'three' already contains all the code for the orbit controls in the examples/js/controls/OrbitControl.js file but doesn't export it in a way that typescript understands. If you don't want to mess with your 3rd party libraries, just install 'three-orbit-controls' alongside 'three' which contains a duplicate of the code but exports it in a nice way.



                                                                                      Now use it like below:




                                                                                      import * as THREE from 'three';

                                                                                      import * as OrbitControlsFunction from 'three-orbit-controls';

                                                                                      const OrbitControls = OrbitControlsFunction(THREE); // OrbitControls is now your constructor

                                                                                      const controls: THREE.OrbitControls = new OrbitControls(camera, element); // Code as you would from here on out.








                                                                                      share|improve this answer












                                                                                      share|improve this answer



                                                                                      share|improve this answer










                                                                                      answered Nov 12 '18 at 17:05









                                                                                      Stephen VerbistStephen Verbist

                                                                                      16016




                                                                                      16016



























                                                                                          draft saved

                                                                                          draft discarded
















































                                                                                          Thanks for contributing an answer to Stack Overflow!


                                                                                          • Please be sure to answer the question. Provide details and share your research!

                                                                                          But avoid


                                                                                          • Asking for help, clarification, or responding to other answers.

                                                                                          • Making statements based on opinion; back them up with references or personal experience.

                                                                                          To learn more, see our tips on writing great answers.




                                                                                          draft saved


                                                                                          draft discarded














                                                                                          StackExchange.ready(
                                                                                          function ()
                                                                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19444592%2fusing-threejs-orbitcontols-in-typescript%23new-answer', 'question_page');

                                                                                          );

                                                                                          Post as a guest















                                                                                          Required, but never shown





















































                                                                                          Required, but never shown














                                                                                          Required, but never shown












                                                                                          Required, but never shown







                                                                                          Required, but never shown

































                                                                                          Required, but never shown














                                                                                          Required, but never shown












                                                                                          Required, but never shown







                                                                                          Required, but never shown







                                                                                          Popular posts from this blog

                                                                                          Use pre created SQLite database for Android project in kotlin

                                                                                          Darth Vader #20

                                                                                          Ondo