Konfetti

repository·main·Indexed 25 days ago

https://github.com/danielmartinus/konfetti

A library for creating confetti particle effects in Android applications, supporting both Jetpack Compose and XML-based projects. It provides a flexible system to define particle emission via the Party and Emitter classes, allowing customization of speed, damping, spread, colors, and shapes (Circle, Square, Rectangle, and DrawableShape). Developers can control spawn locations using Absolute, Relative, or Between positions and manage 2D/3D rotation effects.

Tokens
3.5K
Snippets
4
Records
28
Agent score
85%

What's inside Konfetti

  1. Install Konfetti for Compose or XML projects

    main

    Depending on your project type, add the corresponding dependency to your build.gradle file.

    For Jetpack Compose projects:

    dependencies {
        implementation 'nl.dionsegijn:konfetti-compose:2.0.5'
    }

    For View-based (XML) projects:

    dependencies {
        implementation 'nl.dionsegijn:konfetti-xml:2.0.5'
    }
    dependencies {
        implementation 'nl.dionsegijn:konfetti-compose:2.0.5'
    }
  2. Configure a confetti Party

    main

    A Party object defines how confetti is generated and behaves. Most properties have default values, so you only need to provide an Emitter for a basic effect.

    Minimal configuration:

    Party(
        emitter = Emitter(duration = 5, TimeUnit.SECONDS).perSecond(30)
    )

    Customized configuration example:

    Party(
        speed = 0f,
        maxSpeed = 30f,
        damping = 0.9f,
        spread = 360,
        colors = listOf(0xfce18a, 0xff726d, 0xf4306d, 0xb48def),
        position = Position.Relative(0.5, 0.3),
        emitter = Emitter(duration = 100, TimeUnit.MILLISECONDS).max(100)
    )
    Party(
        emitter = Emitter(duration = 5, TimeUnit.SECONDS).perSecond(30)
    )
  3. Use KonfettiView in XML layouts

    main

    For traditional Android View-based projects, add the KonfettiView to your XML layout and trigger it via code.

    XML Layout:

    <nl.dionsegijn.konfetti.xml.KonfettiView
        android:id:@+id/konfettiView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    Triggering the confetti in Kotlin:

    val party = Party(
        speed = 0f,
        maxSpeed = 30f,
        damping = 0.9f,
        spread = 360,
        colors = listOf(0xfce18a, 0xff726d, 0xf4306d, 0xb48def),
        emitter = Emitter(duration = 100, TimeUnit.MILLISECONDS).max(100),
        position = Position.Relative(0.5, 0.3)
    )
    viewKonfetti.start(party)
    <nl.dionsegijn.konfetti.xml.KonfettiView
        android:id="@+id/konfettiView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  4. Reference Party configuration options

    main

    The following properties are available in the Party object to customize the confetti effect:

    • Angle (Int, default: 0): Direction of the shot (0-360). Presets: Angle.TOP, Angle.RIGHT (0), Angle.BOTTOM, Angle.LEFT.
    • spread (Int, default: 360): Width of the shot (0-360). 1 is a straight line, 360 is a circle.
    • speed (Float, default: 30f): Starting speed.
    • maxSpeed (Float, default: 0f): Random speed between speed and maxSpeed. Set to -1 to disable.
    • damping (Float, default: 0.9f): Rate at which speed decreases after shooting.
    • size (List<Size>, default: SMALL, MEDIUM, LARGE): Use Size.SMALL, Size.MEDIUM, Size.LARGE or custom Size.
    • colors (List<Int>, default: 0xfce18a, 0xff726d, 0xf4306d, 0xb48def): Colors to pick from.
    • shapes (List<Shape>, default: Shape.Square, Shape.Circle): Use Shape.DrawableShape for custom shapes.
    • timeToLive (Long, default: 2000): Milliseconds a particle stays alive.
    • fadeOutEnabled (Boolean, default: true): If true, particles fade out when timeToLive expires; if false, they disappear instantly.
    • position (Position, default: Position.Relative(0.5, 0.5)): Spawn location. Use Position.Absolute, Position.Relative(0.0-1.0), or Position.between.
    • delay (Int, default: 0): Milliseconds to wait before rendering starts.
    • rotation (Rotation, default: Rotation): 3D rotation configuration. Use Rotation.enabled() or Rotation.disabled().
    • emitter (EmitterConfig): Instructions for spawning. Use Emitter(duration, timeUnit).max(amount) or Emitter(duration, timeUnit).perSecond(amount).
  5. Manage confetti parties with PartySystem

    main

    The PartySystem class is the core engine responsible for managing the lifecycle of a confetti party. It handles requesting particles from the emitter and updating their state every frame.

    To use it, you must provide a Party configuration object and the pixelDensity of the display. You can enable or disable the system using the enabled property.

  6. Configure a Party using PartyFactory

    main
    Use PartyFactory to build a Party instance with specific configurations. This factory is particularly useful for Java implementations to provide a builder-like pattern. You must provide an EmitterConfig when initializing the factory. The factory supports chaining methods to configure angle, spread, speed, damping, position, sizes, colors, shapes, time-to-live, and more, before calling .build() to retrieve the final Party object.
  7. Configure a confetti party with the Party class

    main

    The Party data class is the primary configuration object for a confetti animation. It defines how particles are spawned, their movement, appearance, and lifespan.

    Key properties include:

    • angle: Direction of the shot (use Angle constants for convenience).
    • spread: Width of the shot in degrees (0-360).
    • speed & maxSpeed: Initial velocity. Setting maxSpeed higher than speed adds natural randomness.
    • damping: Rate at which speed decreases.
    • size: List of Size objects (e.g., Size.SMALL, Size.MEDIUM, Size.LARGE).
    • colors: List of color integers.
    • shapes: List of Shape objects.
    • timeToLive: Lifespan in milliseconds.
    • fadeOutEnabled: If true, particles fade out smoothly instead of disappearing instantly.
    • position: Where particles spawn (use Position.Absolute, Position.Relative, or Position.Between).
    • emitter: Configuration for how many and how often particles spawn.
  8. Configure Party position with PartyFactory

    main

    The PartyFactory provides several ways to set the position of the confetti:

    • Absolute Position (Float): Use position(x: Float, y: Float) or position(minX: Float, minY: Float, maxX: Float, maxY: Float) to define a specific area using float coordinates.
    • Relative Position (Double): Use position(x: Double, y: Double) or position(minX: Double, minY: Double, maxX: Double, maxY: Double) to define position relative to the view using double coordinates.
    • Position Object: Pass a pre-constructed Position object using position(position: Position).
  9. Configure Party appearance with PartyFactory

    main

    Use these methods to define what the confetti looks like:

    • sizes(vararg sizes: Size) or sizes(size: List<Size>): Sets the particle sizes.
    • colors(colors: List<Int>): Sets the particle colors using a list of integers.
    • shapes(vararg shapes: Shape) or shapes(shapes: List<Shape>): Sets the particle shapes.
    • rotation(rotation: Rotation): Sets the rotation configuration.
  10. Configure Party physics and timing with PartyFactory

    main

    Use the following methods on PartyFactory to control the movement and lifecycle of the confetti:

    • angle(angle: Int): Sets the launch angle.
    • spread(spread: Int): Sets the spread of the particles.
    • setSpeed(speed: Float): Sets a constant speed.
    • setSpeedBetween(minSpeed: Float, maxSpeed: Float): Sets a speed range.
    • setDamping(damping: Float): Sets the damping (air resistance).
    • timeToLive(timeToLive: Long): Sets how long each particle lasts.
    • delay(delay: Int): Sets a delay before the party starts.
    • fadeOutEnabled(fadeOutEnabled: Boolean): Enables or disables the fade-out effect.