Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for triangle shape #327

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ captures/
jarRepositories.xml
androidTestResultsUserPreferences.xml
migrations.xml

# macOS
.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
Expand All @@ -19,6 +20,7 @@ import nl.dionsegijn.konfetti.core.models.Shape.DrawableShape
import nl.dionsegijn.konfetti.core.models.Shape.Rectangle
import nl.dionsegijn.konfetti.core.models.Shape.Square
import nl.dionsegijn.konfetti.xml.image.ImageStore
import kotlin.math.sqrt

/**
* Draw a shape to `compose canvas`. Implementations are expected to draw within a square of size
Expand Down Expand Up @@ -84,5 +86,19 @@ fun Shape.draw(
}
}
}
Shape.Triangle -> {
val triangleWidth = particle.width
val triangleHeight = triangleWidth * sqrt(3.0) / 2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Height of an equilateral triangle is width * √(3) / 2

val trianglePath =
Path().apply {
moveTo(particle.x + particle.width / 2, particle.y)
lineTo(particle.x + triangleWidth, particle.y + triangleHeight.toFloat())
lineTo(particle.x, particle.y + triangleHeight.toFloat())
}
drawScope.drawPath(
color = Color(particle.color),
path = trianglePath,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ sealed interface Shape {

object Square : Shape

object Triangle : Shape

class Rectangle(
/** The ratio of height to width. Must be within range [0, 1] */
val heightRatio: Float,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.BlendMode
import android.graphics.BlendModeColorFilter
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PorterDuff
import android.graphics.RectF
import android.os.Build
Expand All @@ -15,6 +16,7 @@ import nl.dionsegijn.konfetti.core.models.Shape.DrawableShape
import nl.dionsegijn.konfetti.core.models.Shape.Rectangle
import nl.dionsegijn.konfetti.core.models.Shape.Square
import nl.dionsegijn.konfetti.xml.image.ImageStore
import kotlin.math.sqrt

/**
* Draw a shape to `canvas`. Implementations are expected to draw within a square of size
Expand Down Expand Up @@ -61,5 +63,15 @@ fun Shape.draw(
drawable.draw(canvas)
}
}
Shape.Triangle -> {
val triangleHeight = size * sqrt(3.0) / 2
val trianglePath =
Path().apply {
moveTo(size / 2, 0f)
lineTo(size, triangleHeight.toFloat())
lineTo(0f, triangleHeight.toFloat())
}
canvas.drawPath(trianglePath, paint)
}
}
}