Each gradient is painted at random using HTML Canvas and can be exported/download to various image types.
First, we need to paint a small canvas based on the colours input and scale this with CSS:
To download, we scale the canvas iteratively then export:
Notes:
I tried several canvas sizes and found a 8x8 provided consistent results without compromising on detail, anything smaller didn’t look meshy enough and anything larger could result in broken/dodgy gradients when dynamically painting the canvas.
I found using triangles to paint the canvas was most effective in producing ‘meshy’ gradients without harsh edges or linear appearance.
I also found applying a small blur would help the browser interpolation by starting the blend of any sharp edges in the 8x8 canvas.
The White Stag website is written in StencilJS and UIKit so the example below contains code and classes specific to these libraries, but thes functions can be lifted and editred to work in any stack that supports HTML Canvas.