Cotton code
57 lines · 2.2 KB · KotlinRaw
package com.cottondb.android.designimport androidx.compose.foundation.Canvasimport androidx.compose.runtime.Composableimport androidx.compose.ui.Modifierimport androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.graphics.Pathimport androidx.compose.ui.graphics.StrokeCapimport androidx.compose.ui.graphics.StrokeJoinimport androidx.compose.ui.graphics.drawscope.Strokeimport com.cottondb.android.generated.CottonMarkimport com.cottondb.android.generated.CottonPalette/** The generated constellation-C geometry, drawn at any Compose size. */@Composablefun ConstellationMark(    modifier: Modifier = Modifier,    ink: Color = CottonPalette.ink,) {    Canvas(modifier = modifier) {        val boundsWidth = CottonMark.contentMaxX - CottonMark.contentMinX        val boundsHeight = CottonMark.contentMaxY - CottonMark.contentMinY        if (boundsWidth <= 0f || boundsHeight <= 0f) return@Canvas        val scale = minOf(size.width / boundsWidth, size.height / boundsHeight)        val offsetX = (size.width - boundsWidth * scale) / 2f - CottonMark.contentMinX * scale        val offsetY = (size.height - boundsHeight * scale) / 2f - CottonMark.contentMinY * scale        fun point(index: Int): Offset {            val source = CottonMark.points[index]            return Offset(source.x * scale + offsetX, source.y * scale + offsetY)        }        val threads = Path().apply {            CottonMark.threads.forEach { thread ->                moveTo(point(thread.from).x, point(thread.from).y)                lineTo(point(thread.to).x, point(thread.to).y)            }        }        drawPath(            path = threads,            color = ink.copy(alpha = 0.35f),            style = Stroke(                width = CottonMark.threadWidth * scale,                cap = StrokeCap.Round,                join = StrokeJoin.Round,            ),        )        CottonMark.points.filter { it.isKnot }.forEach { knot ->            drawCircle(                color = ink,                radius = CottonMark.knotRadius * scale,                center = Offset(knot.x * scale + offsetX, knot.y * scale + offsetY),            )        }    }}