webGLCanvas.toBlob() fails when not in a rAF |
||
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.
,
Aug 4 2017
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});
,
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 |
||
Comment 1 by mscales@chromium.org
, Aug 4 2017