Hi there!
I am using typescript to create a canvas with ThreeJS in a class.
The code is like this:
js/main.js:
import { PerspectiveCamera, Scene, WebGLRenderer } from "three";
class World {
private windowWidth: number;
private windowHeight: number;
private renderer: WebGLRenderer;
private camera: PerspectiveCamera;
private scene: Scene;
private viewAngle = 45;
private aspectRatio: number;
private cameraNear = 0.1;
private cameraFar = 10000;
constructor(container: string) {
this.renderer = new WebGLRenderer({
antialias: true
});
this.renderer.setSize(this.windowWidth, this.windowHeight);
this.aspectRatio = this.windowWidth / this.windowHeight;
this.camera = new PerspectiveCamera(this.viewAngle, this.aspectRatio, this.cameraNear, this.cameraFar);
this.camera.position.set(0, 0, 300);
this.scene = new Scene();
this.scene.add(this.camera);
const html = document.querySelector(container);
html?.appendChild(this.renderer.domElement);
function update() {
this.renderer.render(this.scene, this.camera);
requestAnimationFrame(update);
}
requestAnimationFrame(update);
}
}
window.addEventListener('load',() => {
new World('#container');
});
In index.html:
<body>
<div id="container"></div>
<script src="js/main.js"></script>
</body>
So at the bottom of the javascript I create an instance of the World class. On creation, the constructor is ran and it should create a canvas with a threeJS scene in it. I provide the id of the HTML Element upon creating the new World object, so it should know where to go.
If I run this I get errors like āUncaught TypeError: Cannot read property ārendererā of undefinedā. This errors occurs inside of the update method. Has anybody else come across this error and how did they solve it? I donāt really understand how this can happen. It feels like there is some asynchronous action going on, because when I try to console.log āthis.cameraā right before the update function it does give me all the info of the camera object. If anyone could clarify how this works, I would be very grateful.
When I run the code below, it does work, I do not get any errors: (with the same HTML)
Note the āconstā declarations of the variables.
class World {
private windowWidth: number;
private windowHeight: number;
private viewAngle = 45;
private aspectRatio: number;
private cameraNear = 0.1;
private cameraFar = 10000;
constructor(container: string) {
this.windowWidth = window.innerWidth;
this.windowHeight = window.innerHeight;
const renderer = new WebGLRenderer({
antialias: true
});
renderer.setSize(this.windowWidth, this.windowHeight);
this.aspectRatio = this.windowWidth / this.windowHeight;
const camera = new PerspectiveCamera( this.viewAngle, this.aspectRatio, this.cameraNear, this.cameraFar);
camera.position.set(0, 0, 300);
const scene = new Scene();
scene.add(camera);
const html = document.querySelector(container);
html?.appendChild(renderer.domElement);
function update() {
renderer.render(scene, camera);
requestAnimationFrame(update);
}
requestAnimationFrame(update);
}
}
window.addEventListener('load',() => {
new World('#container');
});