Eli Peters
Eli Peters

Reputation: 420

Constructor issues with threejs npm and Browserify

I can't seem to instantiate THREEjs objects using the threejs npm build and browserify.

var THREE = require("three-js");
var camera = new THREE.PerspectiveCamera( 75, 1, 20, 2250 );

This throws an error

bundle.js:24 Uncaught TypeError: THREE.PerspectiveCamera is not a constructor

I can see that the constructor exists in bundle.js

    THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {

    THREE.Camera.call( this );

    this.type = 'PerspectiveCamera';

    this.fov = fov !== undefined ? fov : 50;
    this.zoom = 1;

    this.near = near !== undefined ? near : 0.1;
    this.far = far !== undefined ? far : 2000;
    this.focus = 10;

    this.aspect = aspect !== undefined ? aspect : 1;
    this.view = null;

    this.filmGauge = 35;    // width of the film (default in millimeters)
    this.filmOffset = 0;    // horizontal film offset (same unit as gauge)

    this.updateProjectionMatrix();

};

As I am porting this code from a working build I suspect that I may be doing something wrong with Browserify.

npm Threejs build version 79

Upvotes: 0

Views: 481

Answers (1)

Eli Peters
Eli Peters

Reputation: 420

The problem was not constructing the three-js object correctly. From https://www.npmjs.com/package/three-js

var THREE = require("three-js")();

The extra brackets were missing from my example.

FYI: To get addons working use this structure

var oc = require("three-js/addons/OrbitControls");
var anotherAddon = require("three-js/addons/anotherAddon");
var THREE = require('three-js')([oc,anotherAddon]);

Upvotes: 0

Related Questions