Last active
May 17, 2024 18:55
-
-
Save MoyuruAizawa/59559db6ccc32709b5fd5c2a56cd8194 to your computer and use it in GitHub Desktop.
This file contains 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
@Composable | |
fun Warp( | |
modifier: Modifier = Modifier, | |
novasInFrame: Int = 5, | |
acceleration: Int = 15, | |
cometFactor: Float = 0.2f, | |
lineWidth: Dp = 1.dp, | |
) { | |
val particles = remember { arrayListOf<Particle>() } | |
var frame by remember { mutableLongStateOf(0L) } | |
Canvas(modifier = modifier) { | |
repeat(novasInFrame) { particles.add(Particle(Random.nextDouble(6.283185307179586), frame)) } | |
val iterator = particles.iterator() | |
while (iterator.hasNext()) { | |
val particle = iterator.next() | |
val lifetime = frame - particle.createdAt | |
val start = Offset( | |
size.center.x + lifetime * cos(particle.radian).toFloat(), | |
size.center.y + lifetime * sin(particle.radian).toFloat(), | |
) | |
val lineLength = calculateDistance(size.center, start) * cometFactor | |
val end = Offset( | |
size.center.x + (lifetime - lineLength) * cos(particle.radian).toFloat(), | |
size.center.y + (lifetime - lineLength) * sin(particle.radian).toFloat(), | |
) | |
if (end.x < 0 || end.y < 0 || end.x > size.width || end.y > size.height) { | |
iterator.remove() | |
} else { | |
drawLine(Color.Black, start, end, lineWidth.toPx()) | |
} | |
} | |
frame+=acceleration | |
} | |
} | |
data class Particle(val radian: Double, val createdAt: Long) | |
private fun calculateDistance(p1: Offset, p2: Offset): Float { | |
val dx = p2.x - p1.x | |
val dy = p2.y - p1.y | |
return sqrt(dx.pow(2) + dy.pow(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Screen_recording_20240518_032853.mp4