Skip to content

Instantly share code, notes, and snippets.

@KatieBarnett
Last active March 17, 2024 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatieBarnett/8859f927f43d6012a75fc8668fbd501e to your computer and use it in GitHub Desktop.
Save KatieBarnett/8859f927f43d6012a75fc8668fbd501e to your computer and use it in GitHub Desktop.
Zig Zag Line
@Composable
fun ZigZagLine(color: Color, modifier: Modifier = Modifier) {
val shapeWidth = LINE_WIDTH
val density = LocalDensity.current
val zigZagPath = remember {
with(density) {
Path().apply {
val zigZagWidth = shapeWidth.toPx()
val zigZagHeight = shapeWidth.toPx()
val zigZagLineWidth = (1.dp).toPx()
val shapeVerticalOffset = (zigZagHeight / 2) / 2
val shapeHorizontalOffset = (zigZagHeight / 2) / 2
moveTo(0f, 0f)
lineTo(zigZagWidth / 2, zigZagHeight / 2)
lineTo(zigZagWidth, 0f)
lineTo(zigZagWidth, 0f + zigZagLineWidth)
lineTo(zigZagWidth / 2, zigZagHeight / 2 + zigZagLineWidth)
lineTo(0f, 0f + zigZagLineWidth)
translate(Offset(-shapeHorizontalOffset, -shapeVerticalOffset))
}
}
}
Canvas(modifier) {
val pathEffect = PathEffect.stampedPathEffect(
shape = zigZagPath,
advance = shapeWidth.toPx(),
phase = 0f,
style = StampedPathEffectStyle.Translate
)
drawLine(
color = color,
start = Offset(0f, 0f),
end = Offset(size.width, 0f),
pathEffect = pathEffect,
strokeWidth = shapeWidth.toPx()
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment