thorvg.viewer icon indicating copy to clipboard operation
thorvg.viewer copied to clipboard

save2png exports empty image

Open tinyjin opened this issue 4 months ago • 4 comments

  1. Load an animation
  2. Switch backend engine
  3. Export to png leads empty image
Image

tinyjin avatar Aug 11 '25 08:08 tinyjin

@tinyjin This issue was not related to the viewer itself, as the same problem (saving not working) also occurred on thorvg.web. It only happens with the WebGL backend.

It appears to be the same issue described in the “Taking a Screenshot of the Canvas” section of this article: https://webgl2fundamentals.org/webgl/lessons/webgl-tips.html

By default, the browser clears the drawing buffer after rendering to a WebGL canvas, which prevents the content from being saved.

The simplest way to fix this is to enable the GL_TESTING option when compiling with Emscripten: GL_TESTING: When enabled, sets preserveDrawingBuffer in the context, to allow tests to work (but adds overhead)

After enabling GL_TESTING, I confirmed that save2png works correctly with the WebGL backend.

However, as noted, this approach comes with a performance overhead.

The other solutions described in the WebGL2 Fundamentals article (e.g., using a capture flag during rendering, or re-rendering right before capture) would require modifying the core rendering code.

kunyoungparkk avatar Aug 11 '25 13:08 kunyoungparkk

@tinyjin Enabling the preserveDrawingBuffer option is the simplest and most straightforward way to address this issue.

Since there was some concern about potential performance overhead, I ran a quick benchmark test using WebGL rendering. Based on the test results, there was no meaningful change in FPS performance.

I’d like to ask for your opinion on resolving the issue by enabling the preserveDrawingBuffer (GL_TESTING) option.

Image Image

kunyoungparkk avatar Aug 21 '25 13:08 kunyoungparkk

Image

https://emscripten.org/docs/tools_reference/settings_reference.html#gl-testing

Yes, preserveDrawingBuffer would be overheaded. Enabling the option just for save2png functionality isn't good idea, since it effects to rendering performance. Buffer flush after frame is browser's basic optimization, we shouldn't bypass that.

Other than that, if we cannot capture the webgl canvs in technical reason, we better adopt same approach as save2gif.

tinyjin avatar Aug 21 '25 16:08 tinyjin

see this as well: https://github.com/thorvg/thorvg/issues/3737

hermet avatar Aug 22 '25 04:08 hermet