A theme switch that sweeps up the page

Flip the switch on the card. It goes dark from the bottom up, behind an edge of light. The card is one element with a text field, a button and a slider in it, and the switch only changes its color-scheme. The browser draws the controls for the new scheme, and the canvas shows the change.

After Shader Transition by saint, which I found on Memorables.

Reading light

This card has its own color-scheme. The field, the button and the slider are the browser's own, and it draws them again for the dark scheme.

 

Show the code the markup, the two pictures, the 2D sweep, the shader
  1. The markup. One card. The switch is a checkbox with role="switch".
  2. Styles. color-scheme on the card and light-dark() for every colour.
  3. The switch. Copy the canvas, then change the class.
  4. The paint. One draw of the card for the new look, then frames that draw only the two pictures.
  5. The front. A circle centred below the card.
  6. In 2D. A clip and a gradient.
  7. As a shader. The same front, with the picture pushed outwards at the edge by a different amount for each colour.

How it works

The card is one child of the canvas. At rest the canvas draws it with one drawElementImage() call.

Two pictures. When the switch changes, the page copies the canvas into the top half of a plain canvas with drawImage(). That's the old look, as it was on screen. Then it toggles a class that sets color-scheme: dark. On the next paint it draws the card once, which is the new look, and copies the canvas again into the bottom half.

The sweep. For the next second or so the canvas draws no element. Each frame is made from the two pictures. In 2D the new look is drawn everywhere, the old look is drawn again through a clip that leaves out a growing circle, and a ring of light is added on the circle's edge.

The shader. The second route sends the plain canvas to a WebGL context once, as one texture, and runs a fragment shader each frame. Near the edge of the circle it reads the picture from a little further in, which bends the text, and it reads red, green and blue from three slightly different places, which makes the colour fringes. The result is drawn into the visible canvas with drawImage(). The WebGL context isn't made until you choose this route.

Input. The card's hit box is where its last draw put it, and the sweep doesn't draw it, so it stays put. You can flip the switch again in the middle of a sweep: the old look is then whatever was on screen.

Live checks

These run on their own card in the corner of the window, in 2D. The shader isn't checked here, because a check that made a WebGL context would have to wait for a tap.

Findings