Skip to content

Instantly share code, notes, and snippets.

@Sal7one
Last active May 10, 2024 14:05
Show Gist options
  • Save Sal7one/fe446c5800e073ae000536c214cbe39d to your computer and use it in GitHub Desktop.
Save Sal7one/fe446c5800e073ae000536c214cbe39d to your computer and use it in GitHub Desktop.
Waves in canvas compose
val otherColors = listOf(
// Color(0xffffffff),
Color(0xff125B34),
)
@Composable
fun CubicWaves() {
var angle by remember { mutableFloatStateOf(0f) }
val cols = 50
val rows = 50
val size = 26f
val startingAngle = -280f
val boxSize = size - size / 4
Box(
Modifier
.fillMaxWidth()
.height(420.dp)
// .background(Color.Green)
.padding(8.dp)
) {
Canvas(
modifier = Modifier
.fillMaxSize()
.clipToBounds()
) {
val width = size * cols
val height = size * rows
val dx = (size / 2) - (width / 2)
val dy = (size / 2) - (height / 2)
// Loop over each position in the grid.
for (i in 0 until cols) {
for (j in 0 until rows) {
val x = i * size + dx
val y = j * size + dy
val currentColor = otherColors[j % otherColors.size]
withTransform({
translate(left = center.x - x, top = center.y + y)
rotate(
degrees = angle + i * startingAngle + j * startingAngle,
pivot = Offset(boxSize * 2, boxSize * 2)
)
}) {
clipRect {
drawRect(
color = currentColor,
topLeft = Offset(0f, 0f),
size = Size(boxSize, boxSize)
)
}
}
}
}
angle -= 1.5f
}
}
}
@Sal7one
Copy link
Author

Sal7one commented May 10, 2024

Feel free to remove the bounds from the box!

SmartSelect_20240510_170353_TestingTalk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment