Last active
May 10, 2024 14:05
-
-
Save Sal7one/fe446c5800e073ae000536c214cbe39d to your computer and use it in GitHub Desktop.
Waves in canvas 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
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to remove the bounds from the box!