I want to read out a Canvas's pixel data (using OpenJFX 12), and do some checks on it.

int x = ...;
int y = ...;
Canvas canvas = ...;
WritableImage snapshot = canvas.snapshot(null, null);
PixelReader pixelReader = snapshot.getPixelReader();
Color color = pixelReader.getColor(x, y);

This works except that if the canvas were cleared with canvas.getGraphicsContext2D().clearRect(...), color will be 0xffffffff, which is white, fully visible, but it should be 0x00000000, as the documentation says. The drawn image is correct, where it sould be transparent, it is, because I see items behind it.

I'm also using TestFX (right now in visible mode), but I don't think that affects the canvas' data.

Why is that? Maybe I misunderstand what clearRect do?

0 Answers