I'm interested in learning how to implement a model loaded event in Aframe.js. Can someone provide me with a

Query:

Hello, I am trying to implement a loading screen in Aframe. I came across the following page which talks about object loading, but I am having trouble understanding how to use it...

<a-asset-item class="model" id="back" src="./models/back.obj"></a-asset-item>
<img id="backTex" src="./textures/back.png">

<a-asset-item class="model" id="front" src="./models/front.obj"></a-asset-item>
<img id="frontTex" src="./textures/front.png">

-

var models = document.getElementsByClassName("model");

/*trial 1*/
for(var i in models){
    models[i].addEventListener("loaded",()=>{console.log("loaded!")});//CAN'T FIND addEventListener
}

function OnLoaded(){
console.log("loaded!");
}

/*trial 2*/
for(var i in models){
    models[i].loaded  = OnLoaded;
}

In the HTML file, I have included some

<a-asset-item class="model" id="mainModel" src="./models/mainModel.obj"></a-assets-item>
,

I selected all of the models with the class model. When I tried to attach an event listener to its child element, it gave me an error saying that "addEventListener is not a function". It seems like there is no addEventListener method for this element.

As a workaround, I inspected the children and noticed that they had events like variables (onclick, onmousedown etc.). I added the variables "loaded" and "model-loaded" to the children, but still no action was taken.

I also attempted using the event name "model-loaded", but that did not work either... https://github.com/aframevr/aframe/blob/master/src/components/obj-model.js#L51

Could someone provide some examples of how to utilize the loaded event for <a-assets-item>? Any help is appreciated.

  • A-Frame Version: 0.7.1
  • Reproducible Code Snippet or URL:

Update

for(var i = 0; i < this.models[i].length; i++){
    this.models[i].addEventListener("model-loaded", ()=>{console.log("Hello!");})
    this.models[i].addEventListener("loaded", ()=>{console.log("Hello!");})
}

I replaced the "for/in" loop with a regular for loop. Now, there are no errors, but nothing is happening...

Answer №1

When working with the getElementsByClassName method, it returns a collection of HTML elements. However, be cautious when iterating through this collection using the for .. in loop as it may lead to unexpected outcomes, like trying to add an event listener to the item() function within the collection.


To avoid issues, it is recommended to use a traditional for loop like below:

for (var i = 0; i < models.length; ++i) {
  models[i].addEventListener(....)
}

This approach should work effectively without any errors.


In reality, your initial method should have worked fine as well - the problem arose only when the loop encountered the item() function within the iteration.

For additional insights on navigating through a HTMLCollection, you can refer to this informative post.


Regarding event names, keep in mind that the <a-asset-item> element, treated as a a-node, triggers the loaded event. On the other hand, both <a-obj-model> and <a-entity obj-model=""> will emit the model-loaded event.

Answer №2

<a-asset-item> elements serve the purpose of network preloading and are not actual models themselves.

If you want to display a model, utilize <a-obj-model> and apply your model's class to those elements instead.

Answer №3

To enhance your listener's performance, consider updating it to the object3dset event. Your revised code should look like this:

models[i].addEventListener('object3dset', e => this.handleModelChanges());

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

You can obtain the values of multiple div selections using jQuery

In a display with two columns, I want to be able to perform a common operation (such as delete) based on the selection of certain div IDs. If a user selects the div IDs participant_1, participant_4, participant_6 at the same time, I need to collect these ...

Transforming Attribute Directives into Elements in Angular 2

I've been trying to wrap my head around this Attribute Directive feature in Angular 2. Initially, I thought that directives could only be created using Attributes, but after some experimentation, I realized that I can also create directives using Ele ...

Utilizing Ramda to standardize API responses

What is the best way to transform this API response using Ramda? appointmentsConfig: Object isEnable: true isReadonly: false reminderInAdvanceMinutes: 1440 __proto__: Object tasksConfigs: Array(4) 0: Object isEnable: true isReado ...

Traversing a hierarchical JSON structure reminiscent of a tree object

I am working with a complex object structure, var cObj = { name: 'Object1', oNumbers: 3, leaf: [ { name: 'Inner Object 1', oNumbers: 4, leaf: [] }, { name: 'Inner Object 2', oN ...

The content on my website is shown as &#039; instead of '

My PHP script returns a JSON object, and I have a JavaScript function that parses this JSON. I used htmlspecialchars in my PHP code, but when I display the values on my webpage &#039; is not being replaced by '. The same issue occurs with &quo ...

how to execute a javascript function in an ionic controller from the template

My journey in creating my first Ionic app has been smooth so far. I am not well-versed in UI design, so I may not be implementing the best practices, but I am making progress nonetheless... Within a template, I have encountered the following code snippet ...

Dynamically load highcharts with a dynamic configuration setting

I am currently utilizing highcharts and dynamically loading them using ng-repeat. <div ng-repeat="(key,value) in chartCheckboxes track by $index"> <div class="card" id="{{key}}"> <div class="card-body" id="{{key}}"> ...

Tips for sending AJAX POST requests to PHP servers

I've been struggling to grasp how to effectively use ajax for posting data. Despite simplifying my form down to just two values, I still can't seem to get it to work. Here's my HTML code: <html> <head> <script type="text/java ...

How to Retrieve iFrame URL Using JQuery / Javascript

My webpage contains the following elements... <div> <iframe id="mainContent"> <iframe id="littleBox"> </div> and I am looking to achieve the following functionality... $(".someButton").live('click', function() { ...

Exploring the possibilities of using React for looping?

I have integrated Dexie.js with React for this specific example. However, the implementation details are not of great importance to me. My main focus is on finding out how to iterate through all objects in my IndexDB database using React. In the code snip ...

Interfacing Contact Form Data from Vue Application to Magento Using API - A Step-by-Step Guide

Introduction A custom vue-component has been implemented on the application, serving as a contact form. This component is imported into the header component and enclosed within a modal container. The primary function of this contact form is to trigger an ...

Display a vibrant welcome screen on an Android WebView that ensures a visually appealing experience while the content loads for the

When launching an application, the desired behavior is as follows: Display a splash screen while simultaneously loading a URL Upon the firing of a JavaScript interface event on page load, remove the splash screen Mainactivity.java myWebView.addJavascript ...

As soon as I closed the modal, I noticed that the checked inputs reverted back to

I am using checkboxes within a modal to narrow down my search results. However, I have encountered an issue where when I check multiple checkboxes and then close the modal, the checked inputs become unchecked. Every time I open the modal, I want the check ...

Transferring an array of objects from one array to another with the click of a button

I'm facing an issue with moving data between two arrays of objects using buttons in a Nextjs project. The functionality works correctly when selecting a single data item, but it gives unexpected results when selecting multiple items. Although my code ...

Efficiently handle user authentication for various user types in express.js with the help of passport.js

Struggling to effectively manage user states using Passport.js in Express.js 4.x. I currently have three different user collections stored in my mongodb database: 1. Member (with a profile page) 2. Operator (access to a dashboard) 3. Admin (backend privi ...

Retrieve all users along with their respective posts, ensuring that each post is also accompanied by its corresponding comments in

In my Laravel project, I have set up Eloquent models for User, Post, and Comment. The relationships are as follows: User model public function posts(){ return $this->hasMany('App\Post'); } public function comments(){ return $t ...

Refreshing div with updated form/content using jQuery and AJAX

Is it possible to use jQuery/AJAX functions to replace a form (or any content) with another form when a button is clicked, without reloading the page? Essentially creating a multi-part form dynamically. Can I achieve this with the following code snippet? ...

Substitute all numerical values with a designated number from a variable

I came across a link that looks like this foo.net/index.php?page=15 My goal is to replace any number after page=xxx and retrieve the new number from a variable Currently, my code only replaces 15 with 16 var num = 16, // What if the str = foo.net/index ...

Phonegap's JavaScript canvas feature is experiencing issues

Recently, I came across a JavaScript bouncing ball animation that works perfectly on the Chrome browser when used on a PC. However, when I tried running it using Phonegap Eclipse Android emulator, I encountered an issue where the canvas appeared blank and ...

The Cloudflare KV namespace remains unbound

After running wrangler dev, it appears that Worker KV is not being bound properly: ERROR in /src/handler.ts ./src/handler.ts 16:8-17 [tsl] ERROR in /src/handler.ts(16,9) TS2304: Cannot find name 'generalKV'. This is the content of handler. ...