Last active
October 12, 2021 10:02
-
-
Save mmoczkowski/f76f76262a301ffd410117097bd6add7 to your computer and use it in GitHub Desktop.
Simple, procedural mountain landscape view with Jetpack Compose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright 2020 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 */ | |
@Composable | |
fun BackgroundView( | |
speed: Float = 0.3f, | |
peakDensity: Float = .5f, | |
sunOffset: Float = 0.4f, | |
sunColor: Color = Color.White, | |
startColor: Color = Color.DarkGray, | |
endColor: Color = Color.LightGray, | |
steps: Int = 12, | |
modifier: Modifier | |
) { | |
var time: Long by remember { mutableStateOf(0L) } | |
LaunchedEffect(true) { | |
while (isActive) { | |
withFrameMillis { time = it } | |
} | |
} | |
Canvas(modifier = modifier) { | |
drawCircle(color = sunColor, center = Offset(x = center.x, y = size.height * sunOffset)) | |
val path = Path() | |
(0..steps).forEach { index -> | |
val parallax2 = log(x = 2 + index.toDouble(), base = steps + 1.0).toFloat() | |
val parallax3 = log(x = 3 + index.toDouble(), base = steps + 1.0).toFloat() | |
val waveWidth = (size.width / peakDensity * parallax3).toInt() | |
path.reset() | |
(0..(size.width.toInt() + waveWidth * 4) step waveWidth).forEachIndexed { peakIndex, offset -> | |
path.lineTo( | |
x = offset.toFloat() - (time * parallax2 * speed) % waveWidth * 2, | |
y = size.height / 1f * if (peakIndex % 2 == 0) parallax2 else parallax3 | |
) | |
} | |
path.lineTo(size.width, size.height) | |
path.lineTo(0f, size.height) | |
path.close() | |
val parallax1 = log(x = 1 + index.toDouble(), base = steps + 1.0).toFloat() | |
val color = lerp(startColor, endColor, parallax1) | |
drawPath(path = path, color = color) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment