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.

Volume 30
Show the code the input, the pull, the strips
  1. The markup. One range input.
  2. The pull. How far the pointer is above or below the track while the browser drags the thumb.
  3. The paint. Strips 3 px wide, each raised and sheared to follow a string held at both ends and pulled at the thumb.
  4. One picture. The input drawn once and copied into a plain canvas.
  5. 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.

Level 50
Show the code the input, the bars
  1. The markup.
  2. 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.
  3. Drawing strips.

Ticks that swell under the thumb

After Ruler Picker by Nitish Khagwal.

40
Show the code the input, the lens
  1. The markup. A range input and an <output>.
  2. 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.
  3. Drawing strips.

 

Show the code the styles and the whole library
  1. Styles. The three inputs are styled with ::-webkit-slider-runnable-track and ::-webkit-slider-thumb.
  2. 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