0

I use "readPixels()" for reading pixel color on webgl-canvas. I cannot read pixel when antialise set to false for getContext option. I can read with below line on Safari/MacOS, Chrome, Firefox and Safari/iOS.

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

But I cannot read with below on Safari/MacOS, but can read on Chrome, Firefox and Safari/iOS.

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

ADD LINE: console output Uint8Array [0, 0, 0, 0], but the pixel has color.

Is there a problem with Safari/MacOS or Do I need any option for Safari/MacOS ? Can I read pixel color with Safari/MacOS with antialias=false ?

Sorry for my poor English, thank you.

UsagiLabo
  • 63
  • 3

1 Answers1

0

Post a minimal repo in a snippet if you want help debugging

This works for me in Safari on Mac and iPhone

function test(antialias) {
  const gl = document.createElement('canvas').getContext('webgl', {
    preserveDrawingBuffer: true,
    antialias,
  });
  gl.clearColor(1, 0.5, 0.25, 1);
  gl.clear(gl.COLOR_BUFFER_BIT);
  const pixel = new Uint8Array(4);
  gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
  log(JSON.stringify(gl.getContextAttributes(), null, 2));
  log('pixel:', pixel);
}

function log(...args) {
  const elem = document.createElement('pre');
  elem.textContent = args.join(' ');
  document.body.appendChild(elem);
}

test(false);
test(true);
gman
  • 100,619
  • 31
  • 269
  • 393
  • Thank you for your answer. I am surprised at such short sample script is valid for WebGL, I am just a beginner. I use WebGL in combination with Leaflet, and it seems to affect WebGL redrawing. I do not know why the trouble is occurred on only Safari/MacOS, but I will solve the problem soon. – UsagiLabo Jun 12 '20 at 08:16
  • There are plenty of ways to make [short webgl examples](https://webglfundamentals.org/webgl/lessons/webgl-smallest-programs.html) which are great for quick tests or for making a **minimal** repo to post on stack overflow – gman Jun 12 '20 at 08:20