Controls that join and split like drops
Three native controls whose shapes behave like liquid. Hover or tap the button and its arrow pinches off. Move over the dock and it bulges round the nearest link. Tap a link and it leaves the bar as a drop, then falls back in as the current one. Open the menu and its items pour out of the button.
Each one is a <button>, a <nav> of links or a <details>, drawn by a canvas, and the keyboard works on all of them.
A button whose arrow pinches off
After Liquid Arrow Button by Kopp.
Show the code the button, its paint, the field
- The markup. One
<button>. Its box covers the pill and the space the disc moves into. - The paint. The pill is the element's picture and the disc is a circle the canvas draws.
- The field, shared by all three. Blur a copy, draw it back through a threshold, then draw the elements again with
source-atop. - The threshold. One
feColorMatrixthatctx.filteruses by id.
A dock that bulges round a link
After Reshaping Dock by Matt Ölschlegel.
Show the code the links, their paint, the field
- The markup. A
<nav>of links, each withdrawable. - The paint. The bar and a disc for each link make the field. Each link is drawn where its disc is.
- The field, shared by all three. Blur a copy, draw it back through a threshold, then draw the elements again with
source-atop. - The threshold. One
feColorMatrixthatctx.filteruses by id.
A menu that pours out of its button
After Liquid UI Components by Kopp and the Gooey Sticky Menu by Manoela Ilic.
Show the code the details, its paint, closing, the field
- The markup. A
<details>with a drawable summary and drawable buttons. - The paint. Each item is drawn between the summary and its own place.
- Closing. A closed
<details>has nothing to draw, so the page holds it open until the items are back. - The field, shared by all three. Blur a copy, draw it back through a threshold, then draw the elements again with
source-atop. - The threshold. One
feColorMatrixthatctx.filteruses by id.
Show the code the styles and the whole library
- Styles, including the pressed, open and chosen states.
- The whole library file.
How it works
I found seven demos in one month that join and split shapes this way (they're on the list of other people's work). The usual way to do it on the web is a filter on a wrapper: a blur, then a contrast high enough to turn the soft edge back into a hard one. Two blurred shapes that are close overlap, so the hard edge runs round both. Lucas Bebber wrote it up on Codrops in 2015. The filter treats a label like any other shape, so the text has to live in a second layer on top that is moved in step by hand. I haven't read the source of the seven, so I can't say which of them do that. Choose "The field alone" under the menu to see what the filter does to the labels here.
A canvas can do the two steps to its own pixels and then draw the elements a second time. On each paint event:
- The page draws everything that should join: the elements with
drawElementImage(), and plain circles and rounded rectangles for the parts that have no element. - The library copies the canvas into a small plain canvas with
drawImage()throughctx.filter = 'blur(8px)'. An element's picture can only be drawn into its own canvas, so the copy is of the canvas and not of the elements. - It clears the canvas and draws the copy back through an SVG filter that multiplies alpha by 24 and subtracts 11.5. That is the joined outline, filled with the blurred colours.
- With
globalCompositeOperation = 'source-atop'the page draws the elements again. They appear only where the outline is, and they are sharp.
The last draw of an element is where its hit box is, so a link that is 72 pixels above the dock, in mid-air, is tapped where you see it. Each link moves by one translate and one scale. Nothing here bends an element, which is why input keeps working.
The button is one element. Its box is wider than the pill, and the canvas moves a disc with an arrow through the spare space. The disc isn't an element, and it doesn't need to be: a tap on it is a tap on the button.
The dock's <nav> isn't drawn at all. The canvas draws the bar, and a disc under each link that rises with the pointer. The link with aria-current is a bulge in the accent colour. A tap sends a link up far enough to break away, and it falls back and joins the bar again.
The menu is a <details>. The summary and each button have drawable, so the canvas can draw each one between the summary and its place in the layout. Closing is the awkward part. A closed <details> has no boxes for its content and drawElementImage() throws, so the page cancels the click that would close it, runs the items back, and then sets open = false. Escape does the same.
What was pressed. The states are CSS on the elements: :active darkens a pill, details[open] darkens the summary and turns its arrow, and the item chosen last keeps a tick. A change of style raises a paint event, so the canvas doesn't track any of it.
With reduced motion on, the shapes still join and nothing animates.
Live checks
These run on their own canvases in the corner of the window.
Findings
- In Chromium 157 the whole effect is four 2D calls.
ctx.filtertakes an SVGfeColorMatrixbyurl(#id)and applies it todrawImage(), which gives a threshold on alpha with no shader and no pixel read. drawImage()of a drawable canvas inside its ownpainthandler copies the element pictures drawn earlier in that handler.source-atopclips adrawElementImage()to what is already on the canvas. The label inside the joined shape is the element's own picture: the check below compares it with a plain draw.- A
<summary drawable>and<button drawable>elements inside one<details>draw one by one while it's open. The buttons throw once it has closed. - The threshold eats anything thin. A focus ring drawn outside an element's box disappears, so the rings here are drawn inside the pills with a negative
outline-offset. A shape narrower than about twice the blur shrinks or vanishes. - The colours in a neck between two shapes are the blur of both. Where a label sits near the edge of its element, a trace of its colour can show in the neck.
- The blurred copy is one pixel per CSS pixel whatever the screen's scale, and the threshold is applied at full size. I haven't seen a soft edge from that at scale 2.625 in the test browser.
- Not verified: the frame rate on a phone. The line under the Draw menu reports the paints in the last animation on the device you're using. Also not verified: what a screen reader says. The elements and their states are the native ones, and the disc and the bar are decoration.
- Not built: the shader route, and a real
popoverfor the menu. An open popover that is a child of a canvas can't be drawn (/blocked/).