JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16.15k stars 1.17k forks source link

Is there any way to dump Canvas to a picture file? #574

Closed sumy7 closed 3 years ago

sumy7 commented 3 years ago

I want to save what I draw on the canvas. but didn't find any API to do it. Did I miss something?

olonho commented 3 years ago

One way is to see how our screenshot tests are organized: https://github.com/JetBrains/androidx/blob/androidx-main/compose/ui/ui/src/desktopTest/kotlin/androidx/compose/ui/platform/GraphicsLayerTest.kt

akurasov commented 3 years ago

@sumy7 does is answer your question?

sumy7 commented 3 years ago

@akurasov Sorry forgot to reply. This is not an effective method, because some methods are not available in Canvas class.

akurasov commented 3 years ago

@sumy7 I'm a bit lost. How methods absent in Canvas class relate to your original question? And what exactly is missing?

sumy7 commented 3 years ago

@akurasov Hi, akurasov. First of all, I want to get image bits from Canvas. It is good if there is some method like canvas.getBitmap() to get all Pixels on Canvas. But there is no such method. So I opened this issue, and olonho give me screenshot tests. I notice that GraphicsLayerTest.kt has it's own TestComposeWindow and take screenshot from it. But this method is too troublesome, and I could not find some method to simply it. So the issue has been put on hold.

In addition, I thought about some other methods, but did not verify whether it is feasible.

  1. Implement my own MyCanvas, and replay DrawScope on it. In my control, I could get screenshot from MyCanvas.
  2. Get skija instance from DesktopCanvas, and dump Pixels from it.

If I follow olonho tests method, how could I get picture from canvas? Is my method what i thought feasible? Is there anything else I haven’t noticed?

akurasov commented 3 years ago

@sumy7 maybe Canvas.readPixels is what you need?

akurasov commented 3 years ago

Closing as inactive

okushnikov commented 3 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.