Last active
October 28, 2025 11:04
-
-
Save Kyriakos-Georgiopoulos/30507812dedc6d74c5dd099db59ddaf1 to your computer and use it in GitHub Desktop.
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 2025 Kyriakos Georgiopoulos | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| import androidx.compose.foundation.Canvas | |
| import androidx.compose.foundation.Image | |
| import androidx.compose.foundation.background | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.aspectRatio | |
| import androidx.compose.foundation.layout.fillMaxSize | |
| import androidx.compose.foundation.layout.fillMaxWidth | |
| import androidx.compose.material.icons.Icons | |
| import androidx.compose.material.icons.filled.Star | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.runtime.LaunchedEffect | |
| import androidx.compose.runtime.getValue | |
| import androidx.compose.runtime.mutableStateOf | |
| import androidx.compose.runtime.remember | |
| import androidx.compose.runtime.setValue | |
| import androidx.compose.runtime.withFrameNanos | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.geometry.Offset | |
| import androidx.compose.ui.geometry.center | |
| import androidx.compose.ui.graphics.BlendMode | |
| import androidx.compose.ui.graphics.Brush | |
| import androidx.compose.ui.graphics.Color | |
| import androidx.compose.ui.graphics.Path | |
| import androidx.compose.ui.graphics.StrokeCap | |
| import androidx.compose.ui.graphics.drawscope.DrawScope | |
| import androidx.compose.ui.graphics.drawscope.Stroke | |
| import androidx.compose.ui.graphics.painter.Painter | |
| import androidx.compose.ui.graphics.vector.rememberVectorPainter | |
| import androidx.compose.ui.layout.ContentScale | |
| import kotlin.math.PI | |
| import kotlin.math.cos | |
| import kotlin.math.max | |
| import kotlin.math.min | |
| import kotlin.math.pow | |
| import kotlin.math.sin | |
| import kotlin.math.sqrt | |
| data class BeamSpec( | |
| val color: Color, | |
| val widthPx: Float, | |
| val trailLength: Int = 110, | |
| val speed: Float, | |
| val baseRadiusRatio: Float, | |
| val subRadiusRatio: Float = 0f, | |
| val spiralJitter: Float = 0f, | |
| val spiralFreq: Float = 2f | |
| ) | |
| @Composable | |
| fun OrbitBeamsAnimation( | |
| modifier: Modifier = Modifier, | |
| imagePainter: Painter, | |
| backgroundColor: Color = Color(0xFF111111), | |
| beam1: BeamSpec, | |
| beam2: BeamSpec, | |
| beam3: BeamSpec | |
| ) { | |
| var t by remember { mutableStateOf(0f) } | |
| LaunchedEffect(Unit) { | |
| val start = System.nanoTime() | |
| while (true) withFrameNanos { now -> t = (now - start) / 1_000_000_000f } | |
| } | |
| val trail1 = remember { ArrayDeque<Offset>() } | |
| val trail2 = remember { ArrayDeque<Offset>() } | |
| val trail3 = remember { ArrayDeque<Offset>() } | |
| Box(modifier = modifier.background(backgroundColor), contentAlignment = Alignment.Center) { | |
| Image( | |
| painter = imagePainter, | |
| contentDescription = null, | |
| modifier = Modifier | |
| .fillMaxWidth(0.34f) | |
| .aspectRatio(1f), | |
| contentScale = ContentScale.Fit | |
| ) | |
| Canvas(Modifier.fillMaxSize()) { | |
| val center = size.center | |
| val radius = min(size.width, size.height) * 0.5f | |
| fun unit(a: Float) = Offset(cos(a), sin(a)) | |
| val r1 = radius * beam1.baseRadiusRatio | |
| val p1 = center + unit(t * beam1.speed) * r1 | |
| val r2Base = radius * beam2.subRadiusRatio | |
| val r2 = r2Base * (1f + beam2.spiralJitter * sin(t * beam2.spiralFreq * 2f * PI.toFloat())) | |
| val p2 = p1 + unit(t * beam2.speed * 1.2f) * r2 | |
| val joint = Offset((p1.x + p2.x) / 2f, (p1.y + p2.y) / 2f) | |
| val r3Base = radius * beam3.subRadiusRatio | |
| val r3 = r3Base * (1f + beam3.spiralJitter * sin(t * beam3.spiralFreq * 2f * PI.toFloat())) | |
| val p3 = joint + unit(t * beam3.speed * 1.5f) * r3 | |
| fun push(buf: ArrayDeque<Offset>, pt: Offset, max: Int) { | |
| buf.addLast(pt); while (buf.size > max) buf.removeFirst() | |
| } | |
| push(trail1, p1, beam1.trailLength) | |
| push(trail2, p2, beam2.trailLength) | |
| push(trail3, p3, beam3.trailLength) | |
| fun DrawScope.drawTrail(points: List<Offset>, color: Color, width: Float) { | |
| if (points.size < 2) return | |
| fun lerp(a: Float, b: Float, t: Float) = a + (b - a) * t | |
| val n = points.size - 1 | |
| for (i in 0 until n) { | |
| val p0 = points[i] | |
| val p1 = points[i + 1] | |
| val headness = (i + 1) / n.toFloat() | |
| val eased = headness.pow(2.2f) | |
| val alpha = lerp(0.02f, 1f, eased) | |
| val w = width * lerp(0.55f, 1.15f, eased) | |
| val path = Path().apply { moveTo(p0.x, p0.y); lineTo(p1.x, p1.y) } | |
| drawPath(path, color.copy(alpha = alpha), style = Stroke(w)) | |
| } | |
| } | |
| drawTrail(trail1.toList(), beam1.color, beam1.widthPx) | |
| drawTrail(trail2.toList(), beam2.color, beam2.widthPx) | |
| drawTrail(trail3.toList(), beam3.color, beam3.widthPx) | |
| fun DrawScope.headLine( | |
| trail: List<Offset>, | |
| strokeWidth: Float, | |
| beamColor: Color, | |
| whiteStartFraction: Float = 0.35f | |
| ) { | |
| if (trail.size < 2) return | |
| val pLast = trail.last() | |
| val pPrev = trail[trail.lastIndex - 1] | |
| val dx = pLast.x - pPrev.x | |
| val dy = pLast.y - pPrev.y | |
| val len = sqrt(dx * dx + dy * dy) | |
| if (len <= 0f) return | |
| val nx = dx / len | |
| val ny = dy / len | |
| val headLen = max(strokeWidth * 4.0f, 18f) | |
| val base = Offset(pLast.x - nx * headLen, pLast.y - ny * headLen) | |
| val tip = pLast | |
| val brush = Brush.linearGradient( | |
| colorStops = arrayOf( | |
| 0.0f to beamColor.copy(alpha = 0.95f), | |
| whiteStartFraction.coerceIn(0f, 0.95f) to Color.White.copy(alpha = 0.95f), | |
| 1.0f to Color.White | |
| ), | |
| start = base, | |
| end = tip | |
| ) | |
| drawLine( | |
| brush = brush, | |
| start = base, | |
| end = tip, | |
| strokeWidth = strokeWidth * 1.08f, | |
| cap = StrokeCap.Round, | |
| blendMode = BlendMode.SrcOver | |
| ) | |
| } | |
| headLine(trail1.toList(), beam1.widthPx, beam1.color) | |
| headLine(trail2.toList(), beam2.widthPx, beam2.color) | |
| headLine(trail3.toList(), beam3.widthPx, beam3.color) | |
| } | |
| } | |
| } | |
| @Composable | |
| fun OrbitBeamsAnimationScreen( | |
| painter: Painter = rememberVectorPainter(Icons.Default.Star) | |
| ) { | |
| val beam1 = remember { | |
| BeamSpec( | |
| color = Color(0x88FFFFFF), | |
| widthPx = 7f, | |
| speed = 1.15f, | |
| baseRadiusRatio = 0.72f, | |
| trailLength = 110 | |
| ) | |
| } | |
| val beam2 = remember { | |
| BeamSpec( | |
| color = Color(0xFF1ED760), | |
| widthPx = 6f, | |
| speed = 2.6f, | |
| baseRadiusRatio = 0.72f, | |
| subRadiusRatio = 0.038f, | |
| spiralJitter = 0.42f, | |
| spiralFreq = 1.25f, | |
| trailLength = 110 | |
| ) | |
| } | |
| val beam3 = remember { | |
| BeamSpec( | |
| color = Color(0xFFFF3B30), | |
| widthPx = 5.5f, | |
| speed = 3.8f, | |
| baseRadiusRatio = 0.72f, | |
| subRadiusRatio = 0.032f, | |
| spiralJitter = 0.36f, | |
| spiralFreq = 1.6f, | |
| trailLength = 110 | |
| ) | |
| } | |
| OrbitBeamsAnimation( | |
| modifier = Modifier.fillMaxSize(), | |
| imagePainter = painter, | |
| backgroundColor = Color(0xFF111111), | |
| beam1 = beam1, | |
| beam2 = beam2, | |
| beam3 = beam3 | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment