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

Use dynamic frameRate instead of locking to 60fps #319

Merged
merged 3 commits into from
Dec 10, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Confetti(
private var rotationWidth = width

// Expected frame rate
private var speedF = 60f
private var frameRate = 60f
private var gravity = Vector(0f, 0.02f)

var alpha: Int = 255
Expand Down Expand Up @@ -61,6 +61,9 @@ class Confetti(
}

private fun update(deltaTime: Float, drawArea: Rect) {
// Calculate frameRate dynamically, fallback to 60fps in case deltaTime is 0
frameRate = if (deltaTime > 0) 1f / deltaTime else 60f

if (location.y > drawArea.height()) {
alpha = 0
return
Expand All @@ -69,18 +72,18 @@ class Confetti(
velocity.add(acceleration)
velocity.mult(damping)

location.addScaled(velocity, deltaTime * speedF * pixelDensity)
location.addScaled(velocity, deltaTime * frameRate * pixelDensity)

lifespan -= (deltaTime * 1000).toLong()
if (lifespan <= 0) updateAlpha(deltaTime)

// 2D rotation around the center of the confetti
rotation += rotationSpeed2D * deltaTime * speedF
rotation += rotationSpeed2D * deltaTime * frameRate
if (rotation >= 360) rotation = 0f

// 3D rotation effect by decreasing the width and make sure that rotationSpeed is always
// positive by using abs
rotationWidth -= abs(rotationSpeed3D) * deltaTime * speedF
rotationWidth -= abs(rotationSpeed3D) * deltaTime * frameRate
if (rotationWidth < 0) rotationWidth = width

scaleX = abs(rotationWidth / width - 0.5f) * 2
Expand All @@ -91,7 +94,7 @@ class Confetti(

private fun updateAlpha(deltaTime: Float) {
alpha = if (fadeOut) {
val interval = 5 * deltaTime * speedF
val interval = 5 * deltaTime * frameRate
(alpha - interval.toInt()).coerceAtLeast(0)
} else {
0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.PorterDuff
import android.os.Build
import nl.dionsegijn.konfetti.core.Particle
import nl.dionsegijn.konfetti.core.models.Shape
import nl.dionsegijn.konfetti.core.models.Shape.Circle
import nl.dionsegijn.konfetti.core.models.Shape.Circle.rect
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.core.models.Shape.Text

/**
* Draw a shape to `canvas`. Implementations are expected to draw within a square of size
Expand Down
Loading