jsc3d icon indicating copy to clipboard operation
jsc3d copied to clipboard

Canvas or viewer loading complete?

Open GoogleCodeExporter opened this issue 11 years ago • 2 comments

Hi.
I want export image of canvas on new tab when replace scene complete. I write 
this:

viewer.replaceSceneFromUrl(url);
viewer.onloadingcomplete = function () {
   window.open(canvas.toDataURL());
};

It's open blank page. So I solve this:

viewer.replaceSceneFromUrl(url);
viewer.onloadingcomplete = function () {
  setTimeout(function(){
     window.open(canvas.toDataURL());
  },3000)
};

It's ok. But setTimeout is not a good choice. How to identify the view or 
canvas is loading complete, please?

Original issue reported on code.google.com by [email protected] on 4 Sep 2014 at 8:33

GoogleCodeExporter avatar Apr 09 '15 16:04 GoogleCodeExporter

Yes, it is. Before viewer.onloadingcomplete is invoked, the viewer sets a flag 
to call for a redraw but won't perform it immediately. There's an asynchronous 
routine, periodically scheduled by an internal timer, to get the actual job 
done.  So there will be only background when your code is executed.

Try this solution instead:

  var canExport = false;
  ...
  viewer.onloadingcomplete = function() {
    // now we can export the screenshot
    canExport = true;
  };
  viewer.afterupdate = function() {
    // do export it and reset the flag
    if (canExport) {
      window.open(canvas.toDataURL());
      canExport = false;
    }
  };
  ...

It makes use of another event handler viewer.afterupdate which is ensured to be 
called after a redraw is complete for the actual exporting. This handler is 
documented here: 
http://jsc3d.googlecode.com/svn/trunk/jsc3d/docs/symbols/JSC3D.Viewer.html#after
update.

Original comment by [email protected] on 4 Sep 2014 at 3:16

GoogleCodeExporter avatar Apr 09 '15 16:04 GoogleCodeExporter

Thank you. It's work well

Original comment by [email protected] on 5 Sep 2014 at 2:02

GoogleCodeExporter avatar Apr 09 '15 16:04 GoogleCodeExporter