Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save canvas as PNG without loss of resolution #95

Open
axelboc opened this issue Apr 22, 2020 · 5 comments · Fixed by #118
Open

Save canvas as PNG without loss of resolution #95

axelboc opened this issue Apr 22, 2020 · 5 comments · Fixed by #118
Labels
waiting Waiting for another task to be completed

Comments

@axelboc
Copy link
Contributor

axelboc commented Apr 22, 2020

Note that, in Firefox at least, there's a "View image" contextual option on the heatmap's canvas, which generates an image automatically. This doesn't work for me, but it works somewhat for others:

When I do "View image" with Firefox I see the zoomed image but then if leave go to another tab in firefox and go back to the image I see a empty (white) image. I get this URL: blob:null/b92087e5-f865-4a88-b13c-6c7b1481b192.

It may be worth making sure "View image" works before implementing a more advanced export feature.

@axelboc axelboc self-assigned this May 6, 2020
@axelboc
Copy link
Contributor Author

axelboc commented May 6, 2020

This is why View image rarely works: https://stackoverflow.com/a/15563621/758806 -- the rendering buffer is cleared soon after rendering for performance reasons.

@axelboc
Copy link
Contributor Author

axelboc commented May 12, 2020

The screenshot button should generate an image with the same resolution as the dataset instead of just exporting the current rendering buffer of the canvas (the "View image" context menu action can still do that).

@axelboc axelboc reopened this May 12, 2020
@axelboc
Copy link
Contributor Author

axelboc commented May 18, 2020

I think this is a great use case for OffscreenCanvas - https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas. The idea would be to render the heatmap in a canvas the size of the data but off screen, in a web worker. It would then be trivial to generate a data URL from the canvas and open it a new tab like we do already.

I think this solution would be a lot cleaner than trying to generate a PNG manually from the texture data. Unfortunately, OffscreenCanvas is still behind a flag in Firefox. They intend to remove the flag this year, so I think it's worth waiting a bit.

EDIT: Firefox officially supports OffscreenCanvas since v105.

@axelboc axelboc added the waiting Waiting for another task to be completed label May 18, 2020
@axelboc axelboc removed their assignment May 22, 2020
@axelboc
Copy link
Contributor Author

axelboc commented Apr 16, 2021

With the OffscreenCanvas solution, we'll be able to remove the previous screenshot implementation, and therefore turn off preserveDrawingBuffer for performance reasons.

@axelboc axelboc removed the waiting Waiting for another task to be completed label Nov 29, 2021
@axelboc axelboc self-assigned this Nov 29, 2021
@axelboc axelboc added the waiting Waiting for another task to be completed label Nov 29, 2021
@axelboc axelboc removed their assignment Nov 29, 2021
@axelboc
Copy link
Contributor Author

axelboc commented Dec 3, 2021

Just to clarify that this issue is about generating a PNG image of the WebGL canvas as the user sees it (zoom level, color map, etc.), as opposed to #35, which was about exporting the raw dataset/slice as TIFF/NPY.

The challenge here is to generate a PNG without losing any resolution. The current "Snapshot" button only outputs an image with the size of the canvas as it is displayed on the screen. This is where OffscreenCanvas comes in: it should allow rendering a WebGL canvas offscreen with the exact dimensions of the dataset/slice to ensure we never generate a PNG with fewer pixels.

EDIT: R3F now seems to support rendering into an offscreen canvas: pmndrs/react-three-fiber#2770

@axelboc axelboc changed the title [Heatmap] Export to image Save canvas as PNG without loss of resolution Jul 26, 2023
@axelboc axelboc mentioned this issue Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting Waiting for another task to be completed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant