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.
Show the code the markup, the two pictures, the 2D sweep, the shader
- The markup. One card. The switch is a checkbox with
role="switch". - Styles.
color-schemeon the card andlight-dark()for every colour. - The switch. Copy the canvas, then change the class.
- The paint. One draw of the card for the new look, then frames that draw only the two pictures.
- The front. A circle centred below the card.
- In 2D. A clip and a gradient.
- 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
- In Chromium 157, setting
color-scheme: darkon an element in a canvas changes itslight-dark()colours and the browser's own button and text field in its picture, and raises apaintevent with no call from the page. - The site's stylesheet gives every
<button>a background, so the first version's button stayed pale in the dark scheme.all: reverton the button gave it back to the browser. - A copy of the canvas made with
drawImage()before the class changes is enough for the old look. A picture fromcaptureElementImage()keeps the old look as well, and I didn't need it. - During the sweep no element is drawn. The card's button was still found by
elementFromPoint()after a frame made only of the two pictures. - In the test browser, which renders in software, the 2D sweep ran at 60 paints a second with a longest gap of 33 ms at device scale 2.625. The shader ran at 26 to 41 paints a second there with a longest gap of 139 ms, most of it the first frame, when the shader is compiled and the texture sent. A phone's GPU should do better. Not verified: the line under the demo reports it, and the same line is sent to this site's report log.
- The shader reads one texture that holds both pictures, one above the other. That let me use the WebGL context the site's glass effect already has, with no second context and no change to its library.
- If something on the card changes during the sweep, such as a caret blinking in the field, it isn't shown until the sweep ends, because the frames come from the two pictures.
- The test browser has no
switchattribute on a checkbox. The switch here is a checkbox withrole="switch", styled as a track and a knob. - Not built: a sweep over the whole page. This one covers a card, because one canvas of the whole page would be larger than the site allows on a phone.