https://kotlinlang.org logo
Title
m

Michael Paus

11/30/2020, 2:00 PM
I am just trying to get my head around compose-desktop. How would you create a time based, multi-platform animation of some complex graphics. The state of the graphics is computed based on the current time. So the graphics should be rendered constantly as fast as possible (capped to 60 fps). The examples that I have seen seem to use some Android features which are not available on desktop. The JavaFX equivalent of what I am looking for would be an AnimationTimer. Any help is appreciated.
j

jim

11/30/2020, 2:07 PM
cc @Doris Liu @Adam Powell
👀 1
s

Siggi Gunnarss

11/30/2020, 2:19 PM
I think the common approach is to start an animation and then make it's animated value a part of the composition: I was trying to make a 3d renderer: https://github.com/sigurdurrafn/compose3d/ and the best examples seemed to use that: https://github.com/adibfara/ComposeClock You can then animate arbitrary values, while never having direct control, e.g calling
invalidate()
directly
a

Adam Powell

11/30/2020, 3:09 PM
The core primitive used for this is the
withFrameNanos
suspend function. Its operation is synchronized to the target framerate of the composition when running in a
CoroutineScope
obtained from
LaunchedEffect
or
rememberCoroutineScope
.
👍 3
i

Igor Demin

11/30/2020, 5:11 PM
Note, that we should use
CoroutineScope
only obtained from
LaunchedEffect
 or 
rememberCoroutineScope
(not hand-created independent CoroutineScope). Because it contains the local timer (
coroutineContext[MonotonicFrameClock]
) for the current display (we can have multiple displays with different refresh rate). Currently
withFrameNanos
timer uses default display's refresh rate, but soon there will be a fix in Compose for Desktop (
withFrameNanos
will use the local display timer).
m

Michael Paus

12/01/2020, 8:29 AM
I got this working according to Siggis proposal although I feel that is not the right way to do it. I have to admit that I don’t (yet) understand what Adam and Igor are saying. Do you have an example?
Concerning the
withFrameNanos.
. Which timer will it use if my drawing is on two or more screens at the same time? (Single, multi-screen desktop)(Same problem as with multi-screens with different rendering scales)
i

Igor Demin

12/07/2020, 3:55 PM
Which timer will it use if my drawing is on two or more screens at the same time
It will use timer with refresh rate of display on which the window is currently running. If we move window to another display - it will use different timer. This is how it will work soon. Now timer uses only default's display refresh rate.