Initiating the animation/game loop for a three.js object

I am utilizing angularJS and Three.js for the frontend of my project.

The Three.js object is created in the following manner:

var ThreeObject = (function() {
//constructor
function ThreeObject() {}

ThreeObject.prototype.init = functio init (){
//initialize scene, renderer, etc.
};

ThreeObject.prototype.update = function update(){
//update various objects attached to the scene
};

ThreeObject.prototype.draw = function draw(){
this.renderer.render(this.scene,this.camera);
};

return ThreeObject;
})();

Within my angular controller, I call:

app.controller('Controller', ['$scope', '$http', function($scope,$http) {

    var foo = new ThreeObject(800,600);
    foo.init(document.getElementById('container'));

    function loop() {
        foo.update();
        foo.draw();
        requestAnimationFrame(loop);
    };

    requestAnimationFrame(loop);
 }]);

Given that angular advocates for MVC, my initial thought was to encapsulate all Three.js behavior within a model. However, I'm uncertain if this is the correct approach.

Would it be more beneficial to create a directive to handle the loop? Are there any advantages to this alternative approach?

Answer №1

Check out this unique example I created using AngularJS and THREEjs with a factory instead of a directive. I've implemented a render loop within the factory for this demonstration.

I trust this will be beneficial to you.

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

Why won't hover over function properly in separate divs for two items?

My goal is to show text when hovering over a logo, but the text and logo are in different divs. Despite trying various solutions like using display: none and display: block, I still can't get it to work. import styles from '../styles/Float.module ...

When the x position of the Mesh is negative in ThreeJS, the cube will begin to

As a newcomer to ThreeJS, I am excited to learn more about it! :) It seems like the default behavior in ThreeJS when you set the position of a Mesh. I am currently working on creating a structure in ThreeJS that resembles a Project's Work Breakdown ...

Invoking a function sharing the same name as a local variable

The code snippet below is causing me some trouble... var firstPlay = 1; if (firstPlay == 1) { firstPlay(); } If I remove the if statement and simply have: firstPlay(); It works fine, but for some reason it doesn't work with the if statement. ...

Passing URL parameters from App.js to routes/index.js

I have a URL that looks like this: http://localhost:3000/?url=test In my Express application's app.js file, I'm attempting to extract the "test" parameter from the URL and pass it to my routes/index.js. Currently, I can easily send a static var ...

Exploring the Depths of JSON Arrays within Typescript

I am faced with a challenge in extracting the value of the "id" from the following array of JSON data. The issue lies in the fact that the value is enclosed within double square brackets "[[" which are causing complications in retrieving the desired result ...

HttpRequestInterceptor is repeatedly showing the same error multiple times

Within my AngularJS app, I implemented an HttpRequestInterceptor to show a notification using toastr whenever a request fails. angular.module('spwApp.factories') .factory('httpRequestInterceptor', ['$q', '$injector&a ...

JavaScript code not functioning properly when accessing all information retrieved from Django Model

When I retrieve the endDate field from a Django model using a for loop on an HTML page, I want to verify if all the end dates are earlier than today's date. To achieve this, I am utilizing JavaScript code. Although my code successfully checks the fir ...

How to retrieve a JavaScript variable in AngularJS

Struggling to access a JavaScript variable, update it in the controller, and then utilize it in the route. However, the value remains unchanged. var userEditid = 0; app.controller("cn_newuser", function ($scope,$window) { editUser = function (this_) ...

The rendering of the component is experiencing issues when using react-router

function App() { return ( //Following BEM naming convention <div className="app"> <div className="app__body"> <Sidebar /> <Chat /> <Router> <Routes> ...

Can a collapse that was saved in a separate file be displayed on the requested page?

Imagine having a navigation bar on your website's index page with four buttons, each leading to a different page. What if you wanted to bring all those separate pages together into one by using collapsible elements? That's the challenge this user ...

Karma testing shows quick results, but in reality, the performance is sluggish

If you'd like a visual explanation, check out this video (or see the gif below): The Karma progress reporter may indicate that the tests are taking milliseconds, but in reality, it's taking much longer... I mentioned this on Twitter and was adv ...

Executing jQuery when the DOM is loaded in Angularjs

Seeking assistance with integrating jQuery into AngularJS for a project. I am relatively new to AngularJS and experiencing difficulty in firing jQuery on Dom Ready. Despite various attempts, the code does not seem to work as expected. I am currently using ...

Best Practices for Handling an Abundance of Data in React or Java

I am facing a challenge with my project setup, where I have the front end in ReactJS and the backend API in Spring Boot. The task at hand is to display a drop down list filled with records retrieved from the API. Here's the scenario: I receive a list ...

Is there a way for me to determine the size of this JSON structure?

Can anyone help me figure out the length of this JSON object? I need to know how many data are in it. var ddData = [{ "01":"United States", "02":"United Kingdom", "03":"Aruba", "04":"United Kingdom", ...

Tips for accessing information from an Angular Resource promise

I am currently facing an issue when trying to read data from an angular promise that was returned. Before, I had the following code: var data = category.get({id:userid}); Later, I realized that the data being returned was an unresolved promise. So, I ma ...

Sophisticated method for retrograding every Angular component for AngularJS

I am in the process of transitioning my app from angularjs to angular. As I create new angular components, I am looking for a more streamlined way to import and downgrade them automatically. Is there an elegant solution for this? Here is my current code: ...

Specify a preset selection time in a TextField of time type in Material UI

I am looking to establish a default time for a textfield of type time in Material UI. My requirement is that before the user clicks on the picker, no time should be pre-set, but once they click, 08:00 should appear as the default time to choose from. View ...

Troubleshooting Vue.js data binding problems

Utilizing HTML targeting with data binding <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <div class="row" v-for="test in tests"> <div class="col-12"> <router-link tag="a" :to="{ name: ...

AngularJS controller encounters a scoping problem within a callback function

I have created an angular application with a simple login form that can be viewed on this JSFiddle. HTML Code: <form data-ng-app="jsApDemo" data-ng-controller="loginCtrl"> <label for="username">Username:</label> <input type=" ...

What is causing the unexpected impact of the "Product Quick View" JavaScript plugin on divs that are not being activated by user interaction?

As a newcomer to web design, I have implemented the "Product-Quick-View" plugin from CodyHouse on my website. Upon clicking the DEMO button and inspecting the code, you will find the following: <body> <header> <h1>Product Q ...