DanielMartinus/Konfetti

Add support for Confetti modifiers

teobaranga opened this issue · 1 comments

Hi! I've recently started using this library and it works great! I did come across a scenario which currently isn't supported by the library but which could be a great addition. I am trying to add a fade in effect to each particle and, while this could be implemented specifically, I believe a more generic approach may be better. What I had in mind is adding support for confetti modifiers, which is essentially behaviour that is applied to each confetti individually right before it is drawn on the Canvas.

This would involve adding an interface such as

interface ConfettiModifier {
    fun apply(confetti: Confetti)
}

A list of such modifiers can be provided by consumers when building the ParticleSystem which can then be propagated down to the Confetti level.

The modifiers would then be applied before the shape is drawn, following something like

class Confetti(
    ...
    val modifiers: List<ConfettiModifier> = emptyList(),
) {
    ...
    private fun display(canvas: Canvas) {
        ...
        for (modifier in modifiers) {
            modifier.apply(confetti = this)
        }
        shape.draw(canvas, paint, width)
        ...
    }
}

The modifiers themselves would need access to properties that are currently private so that constraint would need to be relaxed. For this particular fading in scenario, the paint property would need to be public.

The above code sample is simply a suggestion but I am happy to open a PR or discuss this further if this is something of interest!

Thanks!

Interesting idea @teobaranga! The use case you give with modifying the alpha sounds like a good one indeed. Though it would become complex when doing any internal modifications later and the unforeseen complications it will have on everyone who's using Modifiers. I.e. the alpha value is currently tightly coupled with lifetime, that should in some way act in a separate way.
If in the future anything changes on the alpha calculations it will be hard to test what it will do with anyone who's implementing modifications, what do you think?

I love the idea though for having a way to alter the deeply nested properties for own use by an abstraction like you suggested.