On twitter I suggested that my future blog posts would be sprinkled with animated shaders in the space between paragraphs.
Some people (quite reasonbly) though this was a joke. But I actually had in mind a modern take on the ornate section divide lines you might find in an old book.
I’ve used animated inline shaders as content in earlier blog posts (such as the Ember’s breakdown), so this wasn’t going to need anything new, other than some fresh shaders.
But I can empathise with the worries you might have about this. Shaders on the web means using WebGL, and more specifically creating a canvas and WebGL context for each element in the page.
Since browsers tend to limit the total number of concurrent WebGL contexts, the code to manage those needs to be ready to recreate them at any time, say when an element scrolls back in screen.
Combined with a live preview of the blog (I’m using
hugo server -w), that makes sketching inline shaders quick and easy (and it also means that if you are not reading this on your phone, you can see the GLSL fragment-shader source of these images by checking the HTML source of this page in the developer view).
So this was going to be a pure exercise in creative coding.
The starting point was some googling for inspiration. A good set of keywords turned out to be “baroque divide lines”, which gives a nice selection of images in the kind of style I had in mind.
These are generally ornate, black figures on white pages. They typically feature thin lines, curves & bulges, horizontal symmetry, and often also vertical symmetry. Being page dividers, they also have an extreme aspect-ratio: very wide, but not tall.
Of course those traditional designs are static, not animated. So an interesting question here beyond replicating the style, was how to bring them alive with animation.
I picked a standard canvas render resolution for all the images of 512 wide x 64 high, meaning an 8:1 aspect ratio, using the CSS style to rescale the result to 100% of the column width.
All the shaders start with a 2D-coordinate field having (0.0, 0.0) at the centre, and (4.0, 0.5) at the corners, like this:
vec2 a = abs(gl_FragCoord.xy/64. - vec2(4.,.5));
The shader code needs to generate a mask value in the range 0.0-1.0 for each pixel, which is then used as the (pre-multiplied) output alpha (transparency) value for a black pixel. For example:
To give the well defined smooth lines I want, the shader needs to approximate anti-aliasing of edges, with alpha values in between 0 and 1 representing the average (coverage percentage) of pixels containing both line and background.
To do this, I used a selection of common distance-field rendering approaches.
For example, to draw a smooth horizontal line around the vertical centre, I start with a 2D vector value based on the y-coordinate (a.y), and feed that through a smoothstep function with two parameters representing the line width, and the width of the edge to be anti-aliased:
float width = 0.05;
float edge = 0.01;
float m = smoothstep(width+edge, width, a.y);
It gives this as the result:
Next, the line width value can be changed from a constant, into a function to derive a value from the x coordinate. For example:
float width = (0.12+0.1*sin(7.0*a.x))
This one gives:
By taking in a time value (for the current frame being rendered) as an additional input, we can add in subtle evolving animation to bring the figures alive.
Here, we have two triangle waves added together, with sine waves modulating their phases at different frequencies:
float t = .2*abs((fract(a.x+sin(time*.3))*2.)-1.)
t += .1*abs((fract(3.*a.x+sin(time*.1))*2.)-1.)
float l = smoothstep(.02+t,.02+t-.03,a.y)
This gives the (slightly more modern looking) result:
If you found this interesting, inspiring, or have any other thoughts or questions, please send them my way on twitter.