File tree Expand file tree Collapse file tree 4 files changed +33
-0
lines changed
compose/src/main/java/nl/dionsegijn/konfetti/compose
core/src/main/java/nl/dionsegijn/konfetti/core/models
xml/src/main/java/nl/dionsegijn/konfetti/xml Expand file tree Collapse file tree 4 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -59,3 +59,6 @@ captures/
5959jarRepositories.xml
6060androidTestResultsUserPreferences.xml
6161migrations.xml
62+
63+ # macOS
64+ .DS_Store
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import androidx.compose.ui.geometry.Offset
88import androidx.compose.ui.geometry.Size
99import androidx.compose.ui.graphics.Color
1010import androidx.compose.ui.graphics.ImageBitmap
11+ import androidx.compose.ui.graphics.Path
1112import androidx.compose.ui.graphics.drawscope.DrawScope
1213import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
1314import androidx.compose.ui.graphics.nativeCanvas
@@ -19,6 +20,7 @@ import nl.dionsegijn.konfetti.core.models.Shape.DrawableShape
1920import nl.dionsegijn.konfetti.core.models.Shape.Rectangle
2021import nl.dionsegijn.konfetti.core.models.Shape.Square
2122import nl.dionsegijn.konfetti.xml.image.ImageStore
23+ import kotlin.math.sqrt
2224
2325/* *
2426 * Draw a shape to `compose canvas`. Implementations are expected to draw within a square of size
@@ -84,5 +86,19 @@ fun Shape.draw(
8486 }
8587 }
8688 }
89+ Shape .Triangle -> {
90+ val triangleWidth = particle.width
91+ val triangleHeight = triangleWidth * sqrt(3.0 ) / 2
92+ val trianglePath =
93+ Path ().apply {
94+ moveTo(particle.x + particle.width / 2 , particle.y)
95+ lineTo(particle.x + triangleWidth, particle.y + triangleHeight.toFloat())
96+ lineTo(particle.x, particle.y + triangleHeight.toFloat())
97+ }
98+ drawScope.drawPath(
99+ color = Color (particle.color),
100+ path = trianglePath,
101+ )
102+ }
87103 }
88104}
Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ sealed interface Shape {
88
99 object Square : Shape
1010
11+ object Triangle : Shape
12+
1113 class Rectangle (
1214 /* * The ratio of height to width. Must be within range [0, 1] */
1315 val heightRatio : Float ,
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import android.graphics.BlendMode
44import android.graphics.BlendModeColorFilter
55import android.graphics.Canvas
66import android.graphics.Paint
7+ import android.graphics.Path
78import android.graphics.PorterDuff
89import android.graphics.RectF
910import android.os.Build
@@ -15,6 +16,7 @@ import nl.dionsegijn.konfetti.core.models.Shape.DrawableShape
1516import nl.dionsegijn.konfetti.core.models.Shape.Rectangle
1617import nl.dionsegijn.konfetti.core.models.Shape.Square
1718import nl.dionsegijn.konfetti.xml.image.ImageStore
19+ import kotlin.math.sqrt
1820
1921/* *
2022 * Draw a shape to `canvas`. Implementations are expected to draw within a square of size
@@ -61,5 +63,15 @@ fun Shape.draw(
6163 drawable.draw(canvas)
6264 }
6365 }
66+ Shape .Triangle -> {
67+ val triangleHeight = size * sqrt(3.0 ) / 2
68+ val trianglePath =
69+ Path ().apply {
70+ moveTo(size / 2 , 0f )
71+ lineTo(size, triangleHeight.toFloat())
72+ lineTo(0f , triangleHeight.toFloat())
73+ }
74+ canvas.drawPath(trianglePath, paint)
75+ }
6476 }
6577}
You can’t perform that action at this time.
0 commit comments