I am a beginner to compose. Working myself thought...
# compose-desktop
d
I am a beginner to compose. Working myself thought some tutorials and such so far, everything seems logical. Something I'd like to do, since I used to code some gameloops and drawing effects before: Draw manually (lines, pixels etc.) on some canvas. And I am not sure how this would work together. As I understand, overwriting a Painter would not be the right appraoch as the painting might be expensive - I expect my drawing to be the bottleneck for drawing perforance. So I should create a ImageBitmap, darw on there, and just display the Image? But then again, drawing on it would mutate the data , causing a recompose every time? I guess I would double-buffer…? Can someone give me a breadcrumb trail to follow? 😄
r
It really depends on what you will be drawing. A toolkit like Compose is itself double buffered and Canvas operations all run on the GPU, so it's unlikely you'll beat it by mutating bitmap data on the CPU (again, depends on what you are trying to do)
Note that Compose also offers APIs like graphics layers to retain or even rasterizer canvas operations
d
A canvas wants an ImageBitmap. Not sure how else I could be poking pixels efficiently. Ideally I'd like a classic gameloop that does some calculations and then draw operations on some Buffer. Hmm.
r
You can get a Canvas from the UI Toolkit.
By that I mean you can just use the Canvas() Composable and draw into it. It'll be hardware accelerated
d
I am playing with it right now. 🙂 I found https://github.com/SebastianAigner/asteroids-compose-for-desktop which, despite being a bit old, gave me a good hint on how to integrate a gameloop. It showed 'LaunchedEffect' to me. The documentation then brought me to rememberCoroutineScope, which I am currently playing with.
One thing I was wondering: The example above uses actual composable elements for drawing its things. I.e. a 'Box' for drawing boxes. I remember many years ago I play with animating Swing components, moving buttons or labels around. It was miserable from the performance standpoint. Only when I started using drawing functions on frames I was able to make some decent 2D effects. Does this translate to Compose? If I just want to draw a box, is using and manipulating a 'Box' a good idea?
r
It's fine for a few elements and if you are very careful about triggering draws only. I would recommend using canvas though