xml3d / xml3d.js

The WebGL/JS implementation of XML3D
Other
75 stars 25 forks source link

Transform element getters attachment #152

Closed Arsakes closed 8 years ago

Arsakes commented 8 years ago

Hi, I'm trying to use Camera code you provide in tools and it seems it relies on existence of rotation/translation getters for <transform> element referenced by <view>.

The problem is in my case those getters are attached too late - they are not immediately accessible after creation <transform>. Assuming that camera module requires xml3d, following code produces error due to the undefined "rotation"

requireqjs ('[camera]', function () {
   $('body').append('
      <xml3d> 
          <view transform="#t"></view> 
          <transform id="t" rotation=...></transform> 
     </xml3d>');
     var cam = XML3D.StandardCamera($('view')[0]);
});
csvurt commented 8 years ago

Yeah I've seen this before. Internally we override document.createElement to be able to configure things immediately, but AFAIK jQuery bypasses this when using append with a DOMString by creating a <div> and settings its innerHTML to the string.

There's not much we can do about this, unfortunately. The only possibility to hear about DOM nodes created in this way is through the MutationEvent that it triggers, and that's created asynchronously (in this case after your camera initialization). Your best bet would be to add a load listener to the XML3D element and have that initialize the camera:

$('body').append('
    <xml3d onload="init()"> 
        <view transform="#t"></view> 
        <transform id="t" rotation=...></transform> 
    </xml3d>'
);

function init() {
    var cam = XML3D.StandardCamera($('view')[0]);
}