Skip to content

Instantly share code, notes, and snippets.

@Rohit-554
Created March 21, 2025 08:42
Show Gist options
  • Select an option

  • Save Rohit-554/299128f3503d2a8d87ef7e4396736d94 to your computer and use it in GitHub Desktop.

Select an option

Save Rohit-554/299128f3503d2a8d87ef7e4396736d94 to your computer and use it in GitHub Desktop.
A simple canvas code for implementing a cute floating cloud and a rainbow over it
@Composable
fun KawaiiCloudWithRainbow() {
// Animation for cloud bounce
val infiniteTransition = rememberInfiniteTransition()
val cloudBounce by infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = 20f,
animationSpec = infiniteRepeatable(
tween(1000, easing = FastOutSlowInEasing),
repeatMode = RepeatMode.Reverse
)
)
// Animation for star twinkle
val starTwinkle by infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = 1f,
animationSpec = infiniteRepeatable(
tween(500, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
)
)
Canvas(
modifier = Modifier.fillMaxSize()
) {
val centerX = size.width / 2
val centerY = size.height / 2
// Draw the cloud
drawCircle(
color = Color.White,
radius = 150f,
center = Offset(centerX, centerY + cloudBounce)
)
drawCircle(
color = Color.White,
radius = 120f,
center = Offset(centerX - 100f, centerY + 50f + cloudBounce)
)
drawCircle(
color = Color.White,
radius = 120f,
center = Offset(centerX + 100f, centerY + 50f + cloudBounce)
)
drawCircle(
color = Color.White,
radius = 100f,
center = Offset(centerX - 150f, centerY + 100f + cloudBounce)
)
drawCircle(
color = Color.White,
radius = 100f,
center = Offset(centerX + 150f, centerY + 100f + cloudBounce)
)
// Draw the face
drawArc(
color = Color.Black,
startAngle = 0f,
sweepAngle = 180f,
useCenter = false,
topLeft = Offset(centerX - 50f, centerY + 50f + cloudBounce),
size = Size(100f, 50f),
style = Stroke(width = 4f)
)
drawCircle(
color = Color.Black,
radius = 10f,
center = Offset(centerX - 30f, centerY + 20f + cloudBounce)
)
drawCircle(
color = Color.Black,
radius = 10f,
center = Offset(centerX + 30f, centerY + 20f + cloudBounce)
)
// Draw the rainbow
val rainbowColors = listOf(
Color.Red,
Color(0xFFFFA500),
Color.Yellow,
Color.Green,
Color.Blue,
Color(0xFF4B0082),
Color(0xFF8A2BE2)
)
val rainbowWidth = 20f
var currentRadius = 200f
rainbowColors.forEach { color ->
drawArc(
color = color,
startAngle = 180f,
sweepAngle = 180f,
useCenter = false,
topLeft = Offset(centerX - currentRadius, centerY - currentRadius + cloudBounce),
size = Size(currentRadius * 2, currentRadius * 2),
style = Stroke(width = rainbowWidth)
)
currentRadius += rainbowWidth
}
// Draw the stars stars
val starPositions = listOf(
Offset(centerX - 200f, centerY - 200f),
Offset(centerX + 250f, centerY - 150f),
Offset(centerX - 300f, centerY - 100f),
Offset(centerX + 350f, centerY - 50f)
)
starPositions.forEach { position ->
drawStar(
position = position,
size = 30f,
color = Color.Yellow.copy(alpha = starTwinkle)
)
}
}
}
// Function to draw a star
fun DrawScope.drawStar(
position: Offset,
size: Float,
color: Color
) {
val starPath = Path().apply {
val outerRadius = size
val innerRadius = size / 2
val spikes = 5
var rotation = Math.PI / 2 * 3
val step = Math.PI / spikes
moveTo(
x = position.x + (outerRadius * cos(rotation)).toFloat(),
y = position.y + (outerRadius * sin(rotation)).toFloat()
)
for (i in 0 until spikes) {
val x = position.x + (outerRadius * cos(rotation)).toFloat()
val y = position.y + (outerRadius * sin(rotation)).toFloat()
lineTo(x, y)
rotation += step
val x2 = position.x + (innerRadius * cos(rotation)).toFloat()
val y2 = position.y + (innerRadius * sin(rotation)).toFloat()
lineTo(x2, y2)
rotation += step
}
close()
}
drawPath(starPath, color)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment