Leveraging the power of ES6, achieve recursion with requestAnimationFrame in

Lately, I've been working on creating a versatile SceneManager class that not only manages scenes but also handles rendering.

class SceneManager {
    constructor() {

        this.scene = new THREE.Scene();
        this.camera = new THREE.PerspectiveCamera( 60, window.innerWidth/window.innerHeight, 0.1, 10000 );

        this.renderer = new THREE.WebGLRenderer();
        this.renderer.setSize( window.innerWidth, window.innerHeight );
    }
    CreateScene(){
        document.body.appendChild( this.renderer.domElement );
        this.scene.add(new THREE.AmbientLight(0x777777));
    }
    RenderCycle(){
        requestAnimationFrame(this.RenderCycle());
        this.renderer.render(this.scene, this.camera);
    }  
}

I have been implementing the methods like this:

var sceneManager = new SceneManager();
sceneManager.CreateScene();
sceneManager.RenderCycle();

Unfortunately, an error has popped up:

scenemanager.class.js:18 Uncaught RangeError: Maximum call stack size exceeded
at SceneManager.RenderCycle (scenemanager.class.js:18)
at SceneManager.RenderCycle (scenemanager.class.js:19)
at SceneManager.RenderCycle (scenemanager.class.js:19)
at SceneManager.RenderCycle (scenemanager.class.js:19)
at SceneManager.RenderCycle (scenemanager.class.js:19)

My understanding is that it's due to the immediate invocation of RenderCycle(), but I'm unsure how else to approach it.

Any assistance or advice on this matter would be highly appreciated. Thanks!

Answer №1

To ensure proper functionality, make sure to connect the renderCycle method to the current class in your constructor. Additionally, follow the camelCase convention of JavaScript:

class AnimationController {

  constructor() {

    this.renderCycle = this.renderCycle.bind(this);

    // ... 
  }

  renderCycle() {

    requestAnimationFrame(this.renderCycle);
    this.renderer.animate(this.scene, this.camera);
  }  
}

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

There was an issue with Angular 2.0 at :0:0, which was caused by a response with status: 0 from the URL: null

I am a beginner in Angular 2.0 and I am currently working on creating a sample application using @angular\cli. To serve the application on my local machine, I use the command ng serve --open, which opens it at localhost:4200. Now, I have developed a ...

Absence of event firing in .on method in Asp.NET Core 6 when using JQuery

I am currently working on an ASP.NET Core 6 application that utilizes JQuery. Here is the JQuery code snippet I have implemented in my page: @section Scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } ...

Is it recommended to differentiate between users and usersList in node.js?

Exploring the world of Node.js and Express, I find myself in the process of constructing a server after completing most of the frontend work with React for a complex application. Within the app structure, I decided to segregate mock data into users and use ...

Tips for implementing AngularJS on a webpage transfer

I am a beginner in learning AngularJS. I have gone through the basic tips on W3Schools, but now I am stuck on implementing the login function. When I click the "sign in" button, the webpage should redirect to the login page of the website. However, I am ...

Is there a quicker method than using document.getElementById('element-id').innerHTML to retrieve content?

Currently, I am developing a user-friendly step-by-step wizard for my website to gather information about custom orders. Utilizing JavaScript, I have been tasked with updating the content of each "page" using the document.getElementById('element-id&ap ...

Searching for the position of different size values according to their specific value

information = { boxNoTo: 1, boxNoFrom: 1, size: 'M', } items = [{ size: 'M', },{ size: 'M', },{ size: 'S,M,L,XS', boxNoTo: 1, boxNoFrom: 1, country: 'CA', name: 'Josh' }] This is what I have don ...

Modify the Text Displayed in Static Date and Time Picker Material-UI

Looking to update the title text on the StaticDateTimePicker component? Check out this image for guidance. In the DOM, you'll find it as shown in this image. Referring to the API documentation, I learned that I need to work with components: Toolbar ...

Struggling to Manage and Preserve Cookies in Browser While Using React App Hosted on GitHub Pages and Node.js Backend Running on Render

I've encountered a challenge with setting and storing cookies in the browser while utilizing a React application deployed on GitHub Pages and a Node.js backend hosted on Render. Let me explain the setup and the issue I'm facing: Setup: Frontend ...

Why are my values not being applied to the model class in Angular 7?

I'm currently developing an online shopping website where I have defined my order Model class as shown below: import { User } from './user.model'; export class Order { constructor(){} amount: Number = 0; status: String = ""; date: ...

"Failure in bundling with NodeJS using nexe and fusebox

I am attempting to convert a nodejs application into an .exe file. I initially tried using pkg, but encountered errors with half of the node-modules. Now, I am experimenting with a different method. However, when I run the following command: nexe index.js ...

Having issues with Next.js server-side rendering when integrating API functionality

"Not building properly" means that the project is not fully completing the build process. I've developed a simple blog project with dynamic SSR that pulls data from the Notion-API to generate static blog pages. Everything functions correctly ...

Issue with passing parameter values in MVC Html.ActionLink

Currently, I am experimenting with MVC for a demonstration and have managed to put together some components. However, I am encountering difficulties with an Html.ActionLink. The goal is to display a series of dropdown lists to the user that must be selecte ...

Rotating an object with the mouse in three.js without relying on the camera movements

I am working on a project where I need to create multiple objects and rotate each of them individually using the mouse. While I have been able to select an object with the mouse, I am facing challenges when it comes to rotating them with the mouse. Cur ...

Confirming Bootstrap - Implement an onConfirm method for element attributes

I am looking to add a JavaScript function to my confirmation button. I have created the elements off-page and fetched them using ajax, so it is crucial that I can set the function in-properties rather than via $('#id').confirmation() So far, I h ...

Arranging an array in alphabetical order, with some special cases taken into consideration

Below is a breakdown of the array structure: [{ name: "Mobile Uploads" }, { name: "Profile Pictures" }, { name: "Reports" }, { name: "Instagram Photos" }, { name: "Facebook" }, { name: "My Account" }, { name: "Twi ...

Position div elements randomly on the webpage when it first loads

I am trying to achieve a set of divs that will appear randomly on the page when it loads. Currently, I have the divs moving around the viewport in a random manner, but they all seem to load in the top left corner. This is the method I am currently using: ...

Updating a React event as it changes with each onChange event

Let's address a disclaimer before diving into the issue - for a quick look, visit this pen and type something there. The Scenario This is the JSX code snippet used in my render method: <input value={this.state.value} onChange={this.handleCh ...

Creating a full-screen loading animation that appears when a button is clicked is a great way to enhance user experience. This animation can flow seamlessly from right to left before disappearing, providing a visually engaging transition for users as they

Despite anticipating more negative responses than positive ones, I believe that even one correct answer outweighs it all! So, I'm trying to figure out how to create a loading animation that covers the entire screen whenever a button on the navigation ...

The server failed to respond to the Angular HTTP request

To store data in my database, I have created a function in my component.ts file that invokes a service: ajoutText(newtext: String) { this.dataService.sendtext(newtext); } On the HTML side, it looks like this: <form class="form"> <mat-form-f ...

Incorrect comparison of floats within arrays resulted in inaccurate results

I am currently working on a project that involves comparing values in an Array which are dynamically fetched from a website, and I'm using Selenium-IDE to assist with this comparison. However, I've noticed that the values are being compared as s ...