Skip to content

Instantly share code, notes, and snippets.

@sinasamaki
Created January 24, 2025 11:42
Show Gist options
  • Save sinasamaki/05725557c945c5329fdba4a3494aaecb to your computer and use it in GitHub Desktop.
Save sinasamaki/05725557c945c5329fdba4a3494aaecb to your computer and use it in GitHub Desktop.
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PathMeasure
import androidx.compose.ui.graphics.PointMode
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.VertexMode
import androidx.compose.ui.graphics.Vertices
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.drawscope.scale
import androidx.compose.ui.graphics.lerp
@Composable
fun Modifier.meshGradient(
points: List<List<Pair<Offset, Color>>>,
resolutionX: Int = 1,
resolutionY: Int = 1,
showPoints: Boolean = false,
indicesModifier: (List<Int>) -> List<Int> = { it }
): Modifier {
val pointData by remember(points, resolutionX, resolutionY) {
derivedStateOf {
PointData(points, resolutionX, resolutionY)
}
}
return drawBehind {
drawIntoCanvas { canvas ->
scale(
scaleX = size.width,
scaleY = size.height,
pivot = Offset.Zero
) {
canvas.drawVertices(
vertices = Vertices(
vertexMode = VertexMode.Triangles,
positions = pointData.offsets,
textureCoordinates = pointData.offsets,
colors = pointData.colors,
indices = indicesModifier(pointData.indices)
),
blendMode = BlendMode.Dst,
paint = paint,
)
}
if (showPoints) {
val flattenedPaint = Paint()
flattenedPaint.color = Color.White.copy(alpha = .9f)
flattenedPaint.strokeWidth = 4f * .001f
flattenedPaint.strokeCap = StrokeCap.Round
flattenedPaint.blendMode = BlendMode.SrcOver
scale(
scaleX = size.width,
scaleY = size.height,
pivot = Offset.Zero
) {
canvas.drawPoints(
pointMode = PointMode.Points,
points = pointData.offsets,
paint = flattenedPaint
)
}
}
}
}
}
class PointData(
private val points: List<List<Pair<Offset, Color>>>,
private val stepsX: Int,
private val stepsY: Int,
) {
val offsets: MutableList<Offset>
val colors: MutableList<Color>
val indices: List<Int>
private val xLength: Int = (points[0].size * stepsX) - (stepsX - 1)
private val yLength: Int = (points.size * stepsY) - (stepsY - 1)
private val measure = PathMeasure()
private val indicesBlocks: List<IndicesBlock>
init {
offsets = buildList {
repeat((xLength - 0) * (yLength - 0)) {
add(Offset(0f, 0f))
}
}.toMutableList()
colors = buildList {
repeat((xLength - 0) * (yLength - 0)) {
add(Color.Transparent)
}
}.toMutableList()
indicesBlocks =
buildList {
for (y in 0..yLength - 2) {
for (x in 0..xLength - 2) {
val a = (y * xLength) + x
val b = a + 1
val c = ((y + 1) * xLength) + x
val d = c + 1
add(
IndicesBlock(
indices = buildList {
add(a)
add(c)
add(d)
add(a)
add(b)
add(d)
},
x = x, y = y
)
)
}
}
}
indices = indicesBlocks.flatMap { it.indices }
generateInterpolatedOffsets()
}
private fun generateInterpolatedOffsets() {
for (y in 0..points.lastIndex) {
for (x in 0..points[y].lastIndex) {
this[x * stepsX, y * stepsY] = points[y][x].first
this[x * stepsX, y * stepsY] = points[y][x].second
if (x != points[y].lastIndex) {
val path = cubicPathX(
point1 = points[y][x].first,
point2 = points[y][x + 1].first,
when (x) {
0 -> 0
points[y].lastIndex - 1 -> 2
else -> 1
}
)
measure.setPath(path, false)
for (i in 1..<stepsX) {
measure.getPosition(i / stepsX.toFloat() * measure.length).let {
this[(x * stepsX) + i, (y * stepsY)] = Offset(it.x, it.y)
this[(x * stepsX) + i, (y * stepsY)] =
lerp(
points[y][x].second,
points[y][x + 1].second,
i / stepsX.toFloat(),
)
}
}
}
}
}
for (y in 0..<points.lastIndex) {
for (x in 0..<this.xLength) {
val path = cubicPathY(
point1 = this[x, y * stepsY].let { Offset(it.x, it.y) },
point2 = this[x, (y + 1) * stepsY].let { Offset(it.x, it.y) },
when (y) {
0 -> 0
points[y].lastIndex - 1 -> 2
else -> 1
}
)
measure.setPath(path, false)
for (i in (1..<stepsY)) {
val point3 = measure.getPosition(i / stepsY.toFloat() * measure.length).let {
Offset(it.x, it.y)
}
this[x, ((y * stepsY) + i)] = point3
this[x, ((y * stepsY) + i)] = lerp(
this.getColor(x, y * stepsY),
this.getColor(x, (y + 1) * stepsY),
i / stepsY.toFloat(),
)
}
}
}
}
data class IndicesBlock(val indices: List<Int>, val x: Int, val y: Int)
operator fun get(x: Int, y: Int): Offset {
val index = (y * xLength) + x
return offsets[index]
}
private fun getColor(x: Int, y: Int): Color {
val index = (y * xLength) + x
return colors[index]
}
private operator fun set(x: Int, y: Int, offset: Offset) {
val index = (y * xLength) + x
offsets[index] = Offset(offset.x, offset.y)
}
private operator fun set(x: Int, y: Int, color: Color) {
val index = (y * xLength) + x
colors[index] = color
}
}
private fun cubicPathX(point1: Offset, point2: Offset, position: Int): Path {
val path = Path().apply {
moveTo(point1.x, point1.y)
val delta = (point2.x - point1.x) * .5f
when (position) {
0 -> cubicTo(
point1.x, point1.y,
point2.x - delta, point2.y,
point2.x, point2.y
)
2 -> cubicTo(
point1.x + delta, point1.y,
point2.x, point2.y,
point2.x, point2.y
)
else -> cubicTo(
point1.x + delta, point1.y,
point2.x - delta, point2.y,
point2.x, point2.y
)
}
lineTo(point2.x, point2.y)
}
return path
}
private fun cubicPathY(point1: Offset, point2: Offset, position: Int): Path {
val path = Path().apply {
moveTo(point1.x, point1.y)
val delta = (point2.y - point1.y) * .5f
when (position) {
0 -> cubicTo(
point1.x, point1.y,
point2.x, point2.y - delta,
point2.x, point2.y
)
2 -> cubicTo(
point1.x, point1.y + delta,
point2.x, point2.y,
point2.x, point2.y
)
else -> cubicTo(
point1.x, point1.y + delta,
point2.x, point2.y - delta,
point2.x, point2.y
)
}
lineTo(point2.x, point2.y)
}
return path
}
private val paint = Paint()
@dauni6
Copy link

dauni6 commented Feb 23, 2025

Hi! first of all, Thank you for sharing this wonderful code!
I have a problem with that code. when i use the Gradient Modifier in my project, i can only see the mesh gradient in preview but real device.

test device : Pixel 6 API 26 emulator

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MeshGradientsTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Box(modifier = Modifier.padding(innerPadding)) {
                        MeshGradientBox()
                    }
                }
            }
        }
    }
}

@Composable
fun MeshGradientBox(modifier: Modifier = Modifier) {
    val animatedPoint = remember { Animatable(.8f) }

    LaunchedEffect(Unit) {
        while (true) {
            animatedPoint.animateTo(
                targetValue = .1f,
                animationSpec = spring(stiffness = Spring.StiffnessVeryLow)
            )
            animatedPoint.animateTo(
                targetValue = .9f,
                animationSpec = spring(stiffness = Spring.StiffnessVeryLow)
            )
        }
    }

    Box(modifier = Modifier
        .fillMaxSize()
        .meshGradient(
            points = listOf(
                listOf(
                    Offset(0f, 0f) to Color.Magenta,
                    Offset(.5f, 0f) to Color.Magenta,
                    Offset(1f, 0f) to Color.Magenta,
                ),
                listOf(
                    Offset(0f, .5f) to Indigo700,
                    Offset(.5f, animatedPoint.value) to Indigo700,
                    Offset(1f, .5f) to Indigo700,
                ),
                listOf(
                    Offset(0f, 1f) to Color.Red,
                    Offset(.5f, 1f) to Color.Red,
                    Offset(1f, 1f) to Color.Red,
                ),
            ),
            resolutionX = 32,
        )
    ) {

    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MeshGradientsTheme {
        MeshGradientBox()
    }
}

image

and i used real device for testing your mesh gradient button code, but the background of button was just black.
I don't know why and i hope i can get a reply from you. Thank you!

@sinasamaki
Copy link
Author

Interesting! From my quick testing right now, it looks like there is an issue with this code on earlier SDK versions. Unfortunately I have been testing on API 34 and blindly trusted that since the function exists on earlier SDKs, it will work the same 🤦‍♂️🤦‍♂️
Sorry about that. That would have been crucial to add to the article.

But to test my hypothesis, could you please let me know the API level and specs of the real device you were testing on?
And did you also test the code you sent as well on the real device?

@sinasamaki
Copy link
Author

@dauni6 I just tested multiple emulators and seems like the issue starts from API level 28 (Android 9.0) and below.
Could you confirm with your real device as well? What version is it running?

@dauni6
Copy link

dauni6 commented Feb 23, 2025

@sinasamaki the real device which didn't work was Samsung galaxy A51(API 33).
And i just tested with Samsung galaxy Buddy2(API 34), This one works perfectly :)
I hope we can find work-around together.

@sinasamaki
Copy link
Author

@dauni6 Thanks for the device info.
It seems to me that the canvas.drawVertices() function is not even working on api 28 and below. That is the basis of all this, so it's very crucial. Here is the sample code that I am using, if you can confirm that it is not working for you too:

Box(
    modifier = modifier
        .fillMaxSize()
        .drawBehind {
            val positions = listOf(
                Offset(0f, 0f),
                Offset(size.width, 0f),
                Offset(0f, size.height),
                Offset(size.width, size.height),
            )

            val colors = listOf(
                Color.Green,
                Color.Magenta,
                Color.Black,
                Color.Yellow,
            )

            drawIntoCanvas { canvas ->
                canvas.drawVertices(
                    vertices = Vertices(
                        vertexMode = VertexMode.Triangles,
                        positions = positions,
                        textureCoordinates = positions,
                        colors = colors,
                        indices = listOf(0, 1, 2, 2, 3, 1)
                    ),
                    blendMode = BlendMode.SrcOver,
                    paint = Paint().apply {
                        color = Color.Black
                    },
                )
            }
        }
)

Although I am unsure how to proceed at the moment for this, if it's on a compose, or device, level.

@dauni6
Copy link

dauni6 commented Mar 4, 2025

@sinasamaki sorry for my late reply. Yes, the code doesn't work too on Samsung galaxy A51(API 33.

@rock3r
Copy link

rock3r commented Mar 5, 2025

@dauni6 what if you set the Paint colour to white? Cause this seems similar to this Samsung bug... and maybe the workaround for that one works here, too

@dauni6
Copy link

dauni6 commented Mar 11, 2025

@rock3r I just tested @sinasamaki's workaround on Android Studio emulator(API 33) and it worked well. So, It's the Samsung bug :(
Thank you @sinasamaki for the workaround and @rock3r for telling me the issue 👍

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