New issue
Advanced search Search tips

Issue 752460 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner: ----
Closed: Aug 2017
Components:
EstimatedDays: ----
NextAction: ----
OS: Mac
Pri: 3
Type: Bug



Sign in to add a comment

webGLCanvas.toBlob() fails when not in a rAF

Project Member Reported by mscales@google.com, Aug 4 2017

Issue description

Chrome Version       : 59.0.3071.115
                     : 62.0.3176.0
OS Version: OS X 10.12.6

Test code:

<canvas id="canvas"></canvas>

<script>
  const canvas = document.getElementById('canvas');
  const gl = canvas.getContext('webgl');

  const testBlob = () => {
    requestAnimationFrame(testBlob);

    gl.clearColor(0.8, 0.5, 0.8, 1);
    gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.flush();

    canvas.toBlob((blob) => {
      console.log(blob);
    });

    setTimeout(() => {
      canvas.toBlob((blob) => {
        console.log(blob);
      });
    }, 0);
  }

  testBlob();
</script>

Expected:
Both blobs produced each frame should be purple.

Actual:
The blob produced inside the setTimeout is entirely blank, with alpha set to zero everywhere.

I can't get similar behaviour with a 2d canvas.

 
Components: Blink>Canvas Blink>WebGL

Comment 2 by junov@chromium.org, Aug 4 2017

Status: WontFix (was: Unconfirmed)
This is not a bug. It is the intended behavior. 

Presenting canvas contents to screen (the buffer swap), which happens immediately after the execution of the requestAnimationFrame callback, erases the contents of the canvas. This behavior is consistent with the WebGL specification.  To prevent this from happening you have to set the preserveDrawingBuffer option to true.  There is a small performance penalty for using this option. It works like this:

const gl = canvas.getContext('webgl', {preserveDrawingBuffer: true});

Comment 3 by junov@chromium.org, Aug 4 2017

BTW: 2d canvas works differently: it always preserves its contents.  This difference in behavior is normal (as spec'ed).

Sign in to add a comment