Three.js failing to display objects

I am currently developing a project using modular Three.js with TypeScript. Although the scene is being created successfully, I am facing an issue where no elements are being rendered on the canvas. Below is the snippet of my code:

import * as THREE from 'three/build/three.module'

class App {
    private scene = new THREE.Scene();
    private camera;
    private renderer = new THREE.WebGLRenderer();
    private container = document.getElementById('widget');

    constructor() {
        this._init();
        let geometry = new THREE.BoxGeometry(1, 1, 1);
        let material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true });
        let cube = new THREE.Mesh(geometry, material);
        this.scene.add(cube);
    }

    _init() {
        this.camera = new THREE.PerspectiveCamera(45, this.container.clientWidth / this.container.clientHeight, 1, 1000);
        this.camera.position.z = 5;
        this.scene.add(this.camera)
        this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
        this.container.appendChild(this.renderer.domElement);
    }
}

new App()

Answer №1

Your code is not displaying anything because you haven't specified it to render yet.

When referring to the "getting started" documentation at , it's important to note that you've only covered the "creating the scene" aspect so far.

In addition to setting up the scene, you also need to include the steps for rendering the scene as outlined in the tutorial.

Somewhere within your code, there should be a call to

yourAppInstance.renderer.render()
method. This can be done by storing your App instance in a variable like this:

var myApp = new App()

myApp.renderer.render(myApp.camera, myApp.scene)

If working with TypeScript, consider how you access these components in a safe manner.

Alternatively, you can integrate the render call within your constructor function:

constructor() {
    this._init();
    let geometry = new THREE.BoxGeometry(1, 1, 1);
    let material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true });
    let cube = new THREE.Mesh(geometry, material);
    this.scene.add(cube);
    this.renderer.render(this.camera,this.scene) // <-- add this

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps for incorporating a type declaration for an array of objects in a React application with TypeScript

How can I specify the type for an array of objects in React using TypeScript? Here is the code snippet: const SomeComponent = (item: string, children: any) => { //some logic } In this code, you can see that I am currently using 'any' as ...

javascript issue with showing content on click

I'm currently working on a school assignment where I am using the onclick() function to display information about an object. However, I am facing an issue where the information is not popping up as expected. HTML <!DOCTYPE html> <html> ...

Encountering a problem when using the routingService to navigate inside a JavaScript function

Within my Angular component, I have a method called onCellPrepared. In this method, I am using jQuery to attach a span tag. I want to be able to trigger an Angular service to navigate to another page when the span tag is clicked. How can I successful ...

Angular 2: Copious Errors in the Mix

Working on building an Ionic 2 app using the Angular 2 framework, I encountered a problem when trying to display a map on a page. I found a helpful guide at the following link: While the map was working fine, I started getting an error in my console: You ...

What is the function of the Angular dependency injection mechanism?

I'm trying to grasp the inner workings of Angular/NestJs dependency injection. It's intriguing how the type of parameters gets lost when a Typescript class is constructed. For example: type Dependency1 = {}; type Dependency2 = {}; class X { ...

Tips for uploading information to a web service

Issue Description:- I am attempting to access web services from a different domain (i.e. Systems are not locally connected) and encountering the following error: "Failed to load : Response for preflight is invalid (redirect)" var username = "<a href= ...

The Vue application encountered an issue while trying to mount the component due to the absence of a defined template or render function. The error was triggered

Here is the code snippet for my component: <template> <uploader class="uploader-example"> <uploader-unsupport></uploader-unsupport> <uploader-drop> <p>Drop ...

Receiving and monitoring events triggered by a Vue component that was dynamically mounted

I am currently mounting a Vue component dynamically within a mixin to incorporate the resulting HTML into a map popup. While everything is functioning correctly, I am facing an issue with listening to events emitted by the component. I am unsure of how to ...

How to Retrieve Video Viewing Time Using jQuery

Monitoring the duration of an html5 video can be tricky. When playing a video and pausing at 2 seconds, the console may record a usage duration of 2 seconds. However, if you resume the video at 2 seconds and play for an additional 3 seconds, the total usag ...

What is the best method to retrieve the value from a chosen list item?

I decided to incorporate a horizontal selection menu from CodePen into my project, and you can find the source code here Here is the outcome: var btn = document.querySelector("button") var dropdown = document.querySelector(".dropdown-options") var opti ...

The findOneAndUpdate function in MongoDB is adding a new record into the database

Whenever I make an update API call, I just need to update the serviceActiveFlag status. After the update API call, a new document with an empty vehicle array is created, as shown below: _id:59c76073c11d3929148f500f vehicle:Array _v:0 The ID field will ov ...

Steps for invoking the next() function within the beforeRouterEnter guard

I've been grappling with this issue for countless hours now. I'm currently using Vue3 My goal is to implement a beforeRouterEnter guard in order to check the user's role upon login and direct them to the appropriate route accordingly. Once ...

Iterate over a collection of objects to find connections between the starting and ending points, and retrieve the number of occurrences

My challenge involves analyzing an array of objects containing origin and destination data, and the total volume of objects moving between locations. I am specifically looking to compare flow counts between two cities. For example, when the origin is Vanco ...

Encountering issues with the setSinkId() function in an Electron application

I'm currently working on an app that involves playing audio and allowing users to switch between audio devices. My main issue arises when attempting to utilize the setSinkId() function - it consistently results in a DOMException AbortError with the er ...

Implementing Laravel's functionality for calculating average ratings with the help of jQuery

In my database, I have two tables named Users and ratings. The Users can give ratings to consultants, and the Ratings table structure is as follows: User_id | consultant_id | rating --------------------------------- 1 1 2 ...

Unlock the message with Proxy Re-encryption technology

I'm completely new to encryption and am experimenting with a Node JS library called recrypt-js for Proxy Re-encryption using CryptoJS. I tried encrypting the message "test data" following the example provided, but I'm facing difficulty when tryin ...

What is preventing this from retrieving the source code?

I'm trying to retrieve the HTML source code from http://yahoo.com/(index.html) and display it in a dialog box using this code snippet: $.ajax({ url: 'http://yahoo.com', success: function(data) { alert(data); } }); Unfortunately, ...

Retrieving multiple images from a directory and storing the image filenames in a JSON array

Currently, I am attempting to locate and retrieve the images stored within a specific folder using the following code. This code successfully retrieves the image names along with the total count of images. Subsequently, my goal is to save this information ...

Does an invisible property value exist?

Instead of: if ( ! $('#XX').is(':visible) ) Is there a property named invisible? I tested that one out, but it seems to not work. Appreciate the help! ...

The file 'index.js' does not have a default export

Stepping into the world of MERN development, I embarked on a journey to construct a learning management system based on an outdated tutorial. To my dismay, I encountered the following error: > ..\server\middlewares\index.js:1 > import ...