Fade in and fade out a sketch, or fade between sketches

Hi all,

I’m looking for a technique in p5.js, either p5 1.x.x or p5 2.x.x, to “fade in” a moving sketch from a black screen to normal display. The converse would “fade out” the sketch, and what I’m really after is fading between sketches, in some sort of rotating demonstration of different sketches.

I’ve searched various phrases like “fade” and “fade out” here but nothing looked relevant.

I’m wondering if there is a global canvas control like myCanvas.globalBrightness or globalAlpha which would provide a fade as required. I guess that would impose an extra computational overhead on every primitive, while active, but if we have some “head room” per frame it could be ok.

I’m guessing there’s no access from p5 to the physical “brightness” level of a display, which would provide a cost-free method. Unless an HTML canvas has a simple brightness parameter. Just rambling here to show I’ve thought a (tiny) bit about this.

Appreciate any responses ! Many thanks.

Greg.

Hi @grege2,

That’s a fun challenge!

If you’re planning to show multiple sketches on the same page, you may want to look into p5.js instance mode. It helps keep each sketch separate and avoids conflicts between them:

For fading between sketches, one simple approach is to place each sketch in its own HTML element and animate the element’s opacity with CSS.

You could also experiment with noLoop to stop the sketch when it is not visible.

Raphaël

Thanks, I have not used Instance Mode yet, other than running a demo or two ages ago, will look at it. Also didn’t know that an HTML element can have it’s own opacity, but I was hoping for a global control of that kind. I’m ok with noLoop(), I use that a lot.

Great name by the way !

Cheers, Greg E

You won’t need to convert your Global Mode sketches to Instance 1s if you create a separate <iframe> for each of them instead:

Thanks @GoToLoop I will check all that.

Another simple approach is to draw each sketch to its own PGraphics buffer and then blend them using tint() with an alpha value that changes over time. That makes smooth crossfades pretty easy and keeps the transition logic separate from the sketch code itself.

Thanks @AlviaHong that looks a tidy way, will check it out.