Skip to content

Instantly share code, notes, and snippets.

@pauloaapereira
Created April 23, 2021 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pauloaapereira/cf7f2fef77bfeaa86f8effc4611569d2 to your computer and use it in GitHub Desktop.
Save pauloaapereira/cf7f2fef77bfeaa86f8effc4611569d2 to your computer and use it in GitHub Desktop.
Jetpack Compose - Earthquake Effect_9
@Composable
fun EarthquakeBox(
onEarthquakeFinished: () -> Unit = {},
content: @Composable IEarthquakeScope.() -> Unit
) {
val coroutineScope = rememberCoroutineScope()
val state = remember { EarthquakeState() }
val scope = remember { EarthquakeScope(state = state) }
val mover = remember { EarthquakeMover() }
val controller = remember {
EarthquakeController(
scope = coroutineScope,
mover = mover,
onEarthquakeFinished = {
state.isShaking = false
onEarthquakeFinished()
}
)
}
LaunchedEffect(state.isShaking) {
if (state.isShaking) {
controller.startShaking(
earthquakeDuration = state.earthquakeDuration,
shakeDuration = 1000L / state.shakesPerSecond,
shakeForce = state.shakeForce
)
} else {
controller.stopShaking()
}
}
Box(
modifier = Modifier
.alpha(mover.alpha.value)
.offset(mover.x.value.dp, mover.y.value.dp)
.rotate(mover.rotation.value)
.padding(state.shakeForce.dp)
) {
scope.content()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment