Touch Confetti

Touch Confetti

Tap anywhere to burst confetti from your finger, or fire cannons from the bottom corners. Hundreds of tumbling pieces with gravity, air drag, sway and 3D flips, rendered in a single Canvas.

Luiz Mello
  • animation
  • interactive
  • gesture

snippet.swift259 lines

4 files • Scroll to see all →
/// Drives a confetti overlay. Keep one in `@State`, attach it with/// `.touchConfetti(_:)`, then call `burst(at:)` or `fireCannons()`.////// Pieces are pure physics functions of time (gravity + air drag + sway),/// so a single `Canvas` renders hundreds of them without per-frame state.@MainActor@Observablepublic final class TouchConfettiEmitter {    public var palette: [Color]    /// Downward acceleration, points/s².    public var gravity: CGFloat    /// Upper bound on live pieces.    public var maxPieces: Int​    fileprivate private(set) var pieces: [TouchConfettiPiece] = []    /// Global frame of the overlay, used to aim the cannons.    @ObservationIgnored fileprivate var bounds: CGRect = .zero    fileprivate private(set) var burstCount = 0    fileprivate private(set) var cannonCount = 0    private var cleanup: Task<Void, Never>?​// ... (click "Show full code" to see more)
Viewing: TouchConfettiEmitter.swift•1 of 4