Form effects driven by the paint event
Five form ideas from the element catalogue. In each one the effect has no listener on the control. A key press, a
pressed button, a new colour, a new progress value or a focus ring changes the control's picture, the browser raises paint on
the canvas, and the paint handler reads the control's state (value, :active, position,
:focus) and draws the effect from it. I wanted to know how far that goes before a listener is needed. It went all the way
for four of the five. The pressed button needed a listener for touch.
Per-keystroke ink
A <textarea>. Tap it and type. Each new letter arrives wet: the canvas draws the textarea a second time, blurred and enlarged, in a circle at the letter, and a stain spreads and dries.
The wet letter is the textarea's own picture, drawn again through a blur filter. The place of the letter comes from a range over the textarea's value. Deleting makes no ink, because the handler only acts when the value got longer.
Show the code
- Compare the value's length in the paint handler and draw the ink.
- Styles.
Button press that dents the surface
A keypad of real buttons. Press and hold one: the panel sinks round it. Let go, and a ring travels out across the other keys. Space or Enter on a focused key does the same. The line under the canvas counts which route reported each press.
While the surface moves, the panel is drawn as a mesh of 144 triangles, each a clipped draw of the same element with its own affine transform. The keys are still the buttons: a draw that is clipped away at the end puts the hit region back on the flat panel.
Show the code
- One
drawablepanel that holds the buttons. - Read
:activein the paint handler and draw the panel as a mesh. - The second route, for a touch press.
- Styles.
Colour input that recolours its own scene
The round lamp is an <input type="color">. Tap it to choose a colour, or use the buttons, which only write its value. The light on the wall and on the card is that colour.
The card is drawn in its own colours and then multiplied by the light, so a red light turns the green and blue pigments nearly black. The input is drawn last, so it shows the colour that was chosen and not the lit one.
Show the code
- Read
input.valuein the paint handler and multiply the scene by it. - Styles.
Liquid progress bar
A <progress> element drawn as a tank. The buttons write its value, or remove it to make the bar indeterminate. The level follows on a spring and the surface is stirred by how fast it moves.
The tank's outline is the element's own: the element is drawn first and the liquid is filled with source-atop, which keeps it inside the pixels the element painted. The percentage is read from progress.position.
Show the code
- Read
positionin the paint handler and fill inside the element's picture. - Styles.
Search field that focuses the scene
A search field above six links. Tap the field: the links blur, dim and shrink. Type, for example se, and the links whose text matches come back sharp. Tap outside the field to leave it.
The links are real links at every stage, and a blurred link can still be tapped. The focus ring that widens round the field is drawn by the canvas, because an outline is outside an element's picture.
Show the code
- The field and each link are
drawablechildren of the canvas. - Read
:focusand the value in the paint handler, and blur what doesn't match. - Styles.
How it works
The browser raises paint on a drawable canvas when the picture of one of its drawable descendants changes. Typing changes a textarea's picture, :active and :focus styles change a button's or a field's, and a new value changes a colour swatch or a progress bar. So the paint handler is called at the moments an effect needs, and it can read the state from the control:
- The handler compares.
paintsays that something changed, andevent.changedElementssays which element, but not what. The ink demo keeps the last length of the value, the dent demo keeps whether a button was active, and the others ease towards the current state. - The state must be in the picture. A state that changes no pixel raises no
paint. The pressed style of the keys, the focus border of the fields and the faint browser bar inside the tank are there for that reason. - Animation is asked for from inside the handler. While an effect is still moving, the handler asks for one more
paintwithrequestPaint()in the next frame. When it has settled the page draws nothing until the control changes again. - Extra draws move the hit region. The ink and the dent draw their element several times with different transforms, and input goes to the place of the last draw. Each ends with a draw that is clipped away completely at the flat position.
Show the code
- The paint loop and the draw helpers shared by the five demos.
- Each demo starts when its section comes near the screen.
- A lost context stops every demo.
- The controls are then shown as plain HTML.
Live checks
The checks wait for a tap. Each one builds its own small canvas in a corner of the window, and none of them touches the demos above. Pixels are read from a drawImage() copy, not from the drawable canvas.
Findings
Measured in headless Chromium 157 on 2026-11-21, with a mouse, a keyboard and emulated touch. Not yet run on a phone.
- Four of the five effects need no listener. Typing, a value written by script, a new colour, a new progress value, focus and blur each raised
paint, and the handler read the state from the control. After each effect settled the page drew nothing: 0 paint events in 0.8 s for the lamp and the tank, and 2 in 0.7 s for the textarea, whose caret blinks. - A touch press didn't set
:activeon a button drawn in a canvas. With a mouse and with Space on a focused key the button matched:activeand the dent ran frompaintalone. With an emulated touch press held for 0.45 s no button matched:active, though the tap still clicked the key. A button outside the canvas matched:activefor about 20 ms after the same touch. The dent has apointerdownlistener as a second route, and the line under the demo counts which route saw each press. - A state that changes no pixel raises no
paint. A hidden input's value and an attribute with no style changed with no paint event. The pressed style, the focus border and the faint browser bar in the tank are what make the state visible to the canvas. event.changedElementsnames the control. It held the textarea after its value changed and the colour input after its value changed. It says which element changed, not what changed, so the handler keeps the last value to compare.ctx.filterapplies todrawElementImage(). A box drawn throughblur(6px)had alpha 42 at 5 px outside its edge. The wet ink and the links set aside by the search are the element drawn again through a blur.createValueRange()gives the box of a typed character at the drawn place. The ink is put at the middle of that box. The textarea is drawn several times with different scales, so the box is read before the extra draws of the next frame move it.- A draw inside an empty clip moves the hit region and adds no pixels. The ink and the dent end each frame with one at the flat position. After the ink, a tap near the start of the first line put the caret at index 1.
- A triangle mesh bends a panel of buttons in a 2D context. 144 clipped draws of the same element, each with the affine transform of its triangle. A clip 0.6 px larger than each triangle hides most of the lines between neighbours. Faint diagonal lines can still be seen on the dark panel.
source-atopafter one draw of an element keeps later fills inside its picture. The liquid has the rounded outline of the<progress>element with no clip path: alpha 0 in the rounded corner.- Removing
valuefrom a<progress>raisespaintwithappearance: nonetoo (1 event), andpositionis then -1. The browser's own indeterminate look raised 4 paint events in 0.5 s. - A link drawn blurred and dimmed is still found at its drawn place by
elementFromPoint(). - Not verified: everything on a phone. In particular whether a touch press sets
:activeon Chrome 159 on Android (the line under the keypad says), the on-screen keyboard over the ink and the search field, the Android colour dialog opened from the drawn lamp, and the speed of 144 draws per frame.