Paint clocks
Four ideas from the element catalogue. Some elements repaint by themselves: an indeterminate progress bar, a
blinking caret, a playing video, an animated image. Each repaint raises paint on the canvas that holds the element. On this
page that event is the only clock. No demo calls requestAnimationFrame(), starts a timer or asks for paints in a loop, so
each effect runs at the rate of its element and stops when the element stops. The line under each canvas counts the paint events.
Loading state that stops by itself
A <progress> bar above an article. Press Start loading: the bar loses its value, its stripes move, and the article sways, grey and blurred, under a band of light. Press Finish: the bar gets a value, and the drawing stops with the finished article.
Nothing has to be cancelled when loading ends. The page has no loop to stop: the bar stops repainting, so paint stops. The stripes are a CSS animation on the bar. The browser's own indeterminate bar repaints too, at a lower rate, which the live checks measure.
Show the code
- The bar and the article are
drawablechildren of the canvas. - Draw the loading effect on each
paintwhile the bar has no value. - Styles. The stripes are a CSS animation on the bar.
Caret as a heartbeat
Tap the field. The caret blinks, and each time it goes on or off the card beats and the trace moves one step. Type, and each letter adds a beat. Tap outside the field and the trace goes flat and stops.
The card has two states and no animation between them, because between two blinks nothing raises paint. The time between paint events printed above is the caret's blink interval on this device.
Show the code
- The card and the field are separate
drawablechildren, so the field can stay still while the card beats. - Treat each
paintthat the field raised as half a beat. - Tell the clock's paint events from the others.
- Styles.
Video as a frame clock for a whole scene
Press Play. The video is drawn in the canvas, and the three cards and the credits are placed from video.currentTime each time the video shows a new frame. Pause, and everything holds. Drag the slider while paused, and the whole scene moves to that moment.
The scene can't run ahead of the film or behind it, because it has no time of its own. A stalled video stalls the scene, and a seek is a seek for everything.
Show the code
- The video, the cards and the credits are
drawablechildren of the canvas. - Place everything from
currentTimeon eachpaint. - Styles.
Animated image as the only clock
The spinner is an animated GIF of 12 frames. Each frame of it moves the wave in the flag by one step. Swap in the slow file and the flag slows down by the same factor. Remove the image and the flag stops. The checkbox gives the image opacity: 0, to see whether a clock that can't be seen still runs.
The handler counts paint events and never reads the time, so the frame delay stored in the image file sets the speed of the scene. A clock that shouldn't be seen can be left out of the drawing: the image raises paint whether or not the handler draws it.
Show the code
- The image and the flag are
drawablechildren of the canvas. - Move the wave one step on each
paint. - Styles.
How it works
A drawable canvas gets a paint event when the picture of one of its drawable descendants changes. An element that animates by itself therefore raises a steady run of paint events, at its own rate, for as long as it animates. Each demo has one paint handler and nothing else that runs over time:
- Where the time comes from. The loading effect reads
performance.now()inside the handler, so it moves at the right speed at any paint rate. The film readsvideo.currentTime. The flag and the heartbeat count paint events, so their speed is the element's rate. - Stopping. There is nothing to stop. When the bar gets a value, the field loses focus, the video pauses or the image is removed, the element stops repainting and the handler isn't called again.
- The clock doesn't have to be drawn. A
drawableelement raisespaintwhether or not the handler draws it. It does have to be painted by the browser at least once: an image that hasopacity: 0from the start raised no events, and an element withvisibility: hiddenhas no picture. An image that was already running kept raising events after it was givenopacity: 0. - One exception. An element has no picture until the browser has painted it once. If a draw fails for that reason, the handler asks for one more paint, at most 30 times, when the page starts.
Show the code
- The paint handler shared by the four demos. It has no frame loop.
- Each demo starts when its section comes near the screen.
- A lost context stops every demo.
- The elements are then shown as plain HTML.
Live checks
The checks wait for a tap and take about 25 seconds, because each one counts paint events for a second or more. Each builds its own small canvas in a corner of the window, and none of them touches the demos above. The caret isn't checked here, because a check must not move focus: its rate is printed under the heartbeat demo.
Findings
Measured in headless Chromium 157 on 2026-11-21. Not yet run on a phone.
- All four elements work as the only clock. Measured paint rates: 60 per second from a bar with a CSS animation, 8 from the browser's own indeterminate bar, 2 from a caret (one event each 500 ms), 30 from a 30 frames per second video, and 12.5 and 4.2 from GIFs with 80 ms and 240 ms frames. A canvas with no changing element got 0 in a second.
- Each effect stops with its element. A bar that gets a value, a field that loses focus, a paused video and a removed image raised no further
paint. The page cancels nothing. - Drawing an element at a new place raises a second
paintone frame later. ItschangedElementsnames the element that was moved, not the clock. Before this was handled, the flag ran at twice the rate of its image and the card beat twice for each blink. Each handler now counts a tick only whenchangedElementsholds the clock, and redraws the same frame for any other event. - The clock has to stay still to be told apart. When the video was drawn with a small tilt that changed each frame, 42 events a second named the video for 30 frames of film. Drawn at a fixed place it gives 30. The caret's field is a separate
drawableelement from the card that beats, for the same reason. - A clock doesn't have to be drawn. An image that the handler never draws raised 12.0 events a second. An image that had
opacity: 0from the start raised none, and an element withvisibility: hiddenhas no picture. An image that was already running kept raising about 12 events a second after it was givenopacity: 0. - A video raises one
paintfor each frame it shows: 46 paint events for 45 frames reported byrequestVideoFrameCallback()in 1.5 s. A seek while paused raisespaintfor the new frame, so the scene follows the slider with noseekedlistener. - A clock with two paint events a second gives an effect two states. Nothing raises
paintbetween two blinks of the caret, so the heartbeat can't ease from one state to the other without a second clock. - Counting events and reading the time give different effects. The flag counts, so the file's frame delay sets its speed. The loading effect reads
performance.now(), so its speed is the same at 8 and at 60 events a second and only its smoothness changes. - Not verified: everything on a phone. In particular the caret's blink interval on Android (printed under the demo), whether the on-screen keyboard changes it, whether Chrome on Android pauses an animated image or a video's paint events when the canvas scrolls off screen, and the paint rate of a video decoded in hardware.