Sliders that bend, dip and swell
Three range inputs that change shape as you use them. Drag the first and pull it up or down: the track bends like a string and twangs when you let go. The second is cut into bars that dip round the thumb. The third is a ruler whose ticks swell under the thumb.
Each is a native <input type="range">, dragged by the browser and moved by the arrow keys. The canvas only draws it.
A track that bends like a string
After Elastic Volume Slider by Gabriel Obholz.
Show the code the input, the pull, the strips
- The markup. One range input.
- The pull. How far the pointer is above or below the track while the browser drags the thumb.
- The paint. Strips 3 px wide, each raised and sheared to follow a string held at both ends and pulled at the thumb.
- One picture. The input drawn once and copied into a plain canvas.
- Drawing strips. A piece of that copy for each, or one clipped draw of the whole input.
Bars that dip round the thumb
After Wave Slider by Shiv.
Show the code the input, the bars
- The markup.
- The paint. Strips 5 px wide with 3 px between them. Each is lower and shorter the nearer it is to a point that follows the thumb on a spring.
- Drawing strips.
Ticks that swell under the thumb
After Ruler Picker by Nitish Khagwal.
Show the code the input, the lens
- The markup. A range input and an
<output>. - The paint. Strips 2 px wide. Each is drawn wider and taller the nearer it is to the thumb, and the strips after it move along to make room.
- Drawing strips.
Show the code the styles and the whole library
- Styles. The three inputs are styled with
::-webkit-slider-runnable-trackand::-webkit-slider-thumb. - The whole library file.
How it works
A 2D context has one transform for a whole draw, so it can move, scale and shear a picture and can't bend one. What it can do is draw the picture many times. Each draw shows one vertical strip under its own transform. With strips 2 or 3 pixels wide the steps between them can't be seen.
The first version drew the input itself for every strip, clipped, 100 to 150 times a frame. The genie page did the same and was slow on my phone, so this page now does what fixed that one. Each frame it draws the input once, copies those pixels into a plain canvas with drawImage(), clears, and draws the strips as pieces of the copy. The box under the demos switches back to the first way, and the line above it reports the paints and the longest gap between two of them.
All three demos are the same loop with a different rule for where a strip goes.
The string. While you drag, the page reads how far the pointer is above or below the track, to 46 pixels at most. A strip is raised by that much at the thumb and by less towards each end, where the string is held. Each strip is sheared by the slope of the string at that point, so it meets the next one. When you let go the pull goes to zero on a spring with little damping, which is the twang. The arrow keys pluck it too.
The bars. The input is styled as a block 44 pixels tall. The canvas shows 5 pixels of every 8, which makes the bars, and draws a bar lower and shorter the nearer it is to the dip. The dip follows the thumb on a spring, so a quick drag leaves it behind for a moment. The thumb is drawn once more on top, whole.
The ruler. A strip near the thumb is drawn up to 1.9 times wider and 1.5 times taller, and every strip beyond it is pushed along by the extra width. The ends of the ruler move out by about 30 pixels. The <output> is a second element in the same canvas, drawn above the thumb.
Input. An element's hit box is where its last draw put it. With the copy, the only draw of the input is the one at its place in the layout, so the box is already right. When every strip is a draw of the input, each frame ends with one more draw, with no alpha, at that place. The browser's own drag then works on a straight slider while the picture bends. Under the lens the thumb's picture and its hit box are in the same place, because the lens is centred on the thumb.
Live checks
These run on their own inputs in the corner of the window.
Findings
- In Chromium 157 a range input styled with
::-webkit-slider-runnable-trackand::-webkit-slider-thumbis drawn with those styles bydrawElementImage(). - 100 strips of a 300 pixel input (150 for the ruler) drew at 60 paints a second in the test browser, from a copy and from the element. The test browser renders in software, so that says little about a phone. The line under the demos reports the number for the device you're using and sends it to this site's report log.
- The movement used to be stepped by at most a thirtieth of a second a frame, so a slow device also played it slowly. It's now stepped by real time.
- After any number of clipped, sheared and scaled draws, one draw at alpha 0 at the layout place puts the hit box back on the straight line. Native dragging and the arrow keys work while the picture is bent.
- A change to
valueraises apaintevent with no help from the page. - The bend is a picture only. If the string is pulled 46 pixels up, the thumb you see is 46 pixels away from the place a new press has to land. It lasts while a drag is already under way, so I didn't find a case where it matters. A slider that stayed bent would need its input mapped by script.
- The test browser has no
<rangegroup>element, so the two-thumb version from the Open UI proposal isn't here. The check below will pass in a browser that has it. - Pulling the string by touch worked in touch emulation: the track bent 40 pixels and the page didn't scroll, because the input has
touch-action: none. Not verified with a finger on a phone. - Not built: digits that roll in the
<output>, which the Ruler Picker has.