https://kotlinlang.org logo
Title
a

antonarhipov

06/09/2022, 6:54 PM
k

Kirill Grouchnikov

06/09/2022, 7:20 PM
You wouldn't want the canvas to keep its state. Canvas is, well, canvas. You issue drawing commands for "this" particular frame. Every frame you issue new commands. So if you want some objects to persist, if you will, you need to store those objects, along with operations to add/remove them. And then your drawing pass goes over all the current objects and "converts" them to canvas drawing commands.
a

antonarhipov

06/09/2022, 7:22 PM
Meaning, on every click I re-draw all the objects that I’ve created?
k

Kirill Grouchnikov

06/09/2022, 7:35 PM
Yes, this is how canvas works
a

antonarhipov

06/09/2022, 7:35 PM
Thanks!
k

Kirill Grouchnikov

06/09/2022, 7:35 PM
Otherwise it'd be pure madness to "erase" previously drawn content on every frame
a

antonarhipov

06/09/2022, 7:37 PM
it’s not how the same code works with drawing the Path object’s though, but probably that is the specifics of that specific object? code: https://github.com/antonarhipov/compose-for-desktop-samples/blob/main/src/main/kotlin/Strokes.kt
o

orangy

06/09/2022, 7:52 PM
@antonarhipov the difference is that you remembered the path and modifying it. Essentially, the path is your storage of drawing commands. Canvas is redrawn each time, but the path is accumulating the changes
:thank-you: 1
y

Yan Pujante

06/10/2022, 3:06 PM
I have created this little prototype for a project I am working on

https://www.youtube.com/watch?v=ST3pGgbns3A

. So I confirm that what you are trying to do can be done but the "state" is in the model.. not with the canvas. The canvas call simply renders the background image + every widget on top (including their state, like when selected, it draws a yellow rectangle around). And I am also handling all the drag/drop myself. Once you wrap your head around the model it is actually not that hard and pretty clean.