CSS Web UI Demos
repository·main·Indexed 41 days ago
https://github.com/googlechromelabs/css-web-ui-demosA collection of demonstrations by the Chrome DevRel team showcasing web technologies and UI patterns. Includes examples for CSS anchor positioning, the experimental HTML-in-Canvas API (Chrome 148+), Liquid Glass SVG filter effects, and tests for PopStateEvent and NavigateEvent#hasUAVisualTransition.
What's inside CSS Web UI Demos
- This repository is a collection of demos maintained by the CSS and Web UI Chrome DevRel team. Each demo is contained within its own subfolder and includes its own specific setup and usage instructions. To use a specific demo, navigate to its respective directory to find its unique documentation.
Test PopStateEvent and NavigateEvent#hasUAVisualTransition
mainThis project provides test pages to verify the
hasUAVisualTransitionproperty onPopStateEventandNavigateEvent.To test the implementation:
- Launch the project.
- Choose the specific flavor/scenario you want to test.
- Click the provided links several times to build up navigation history.
- Navigate back using either the User Agent (UA) back button or a swipe gesture.
- Observe and verify the reported value for
hasUAVisualTransition.
Liquid Glass Reference file structure
mainThe Liquid Glass reference folder contains the following assets required to replicate the effect:
index.html: Contains the HTML structure for the "Switch" demo (fully functional) and the "Music Player" demo (reference structure).styles.css: A full stylesheet containing utility classes (Tailwind-like) used in the HTML.*.png: The displacement and specular maps required by the SVG filters.
Understand the carousels-gallery project structure
mainThe
carousels-gallerypackage is built using Astro. The project follows a standard Astro directory structure:src/pages/: Contains.astroor.mdfiles. Each file in this directory is automatically exposed as a route based on its filename.src/components/: A directory for reusable components (Astro, React, Vue, Svelte, or Preact).src/layouts/: Contains layout components used to wrap pages.public/: Stores static assets like images and favicons that are served directly.package.json: Defines project dependencies and scripts.
Basic Implementation Pattern for HTML-in-Canvas
mainThe HTML-in-Canvas API allows you to draw DOM content directly into a
<canvas>or a WebGL/WebGPU texture.Key Concepts:
layoutsubtreeattribute: Adding this attribute to a<canvas>element allows it to contain DOM children. These children are processed by the layout engine but are not rendered as standard HTML elements; instead, they are available for drawing into the canvas.ctx.drawElementImage(element, x, y): This method draws a specified DOM element onto the canvas context at the provided coordinates. It returns a transform object.canvas.onpaint: An event handler used to trigger the drawing logic.- Hit-testing: To keep the UI interactable and accessible, you should sync the DOM element's style (e.g.,
transform) with the transform returned bydrawElementImage.
<canvas id="my-canvas" layoutsubtree> <!-- DOM content lives inside, processed by layout engine but not rendered --> <div id="ui-overlay">Hello World</div </canvas> <script> const canvas = document.getElementById('my-canvas'); const ctx = canvas.getContext('2d'); const el = document.getElementById('ui-overlay'); canvas.onpaint = () => { ctx.reset(); // Draw the HTML element at (100, 100) const transform = ctx.drawElementImage(el, 100, 100); // Sync DOM for hit-testing el.style.transform = transform.toString(); }; </script>How the Liquid Glass effect works
mainThe Liquid Glass effect is a visual technique that uses SVG filters to create a distorted, glass-like appearance on elements. It relies on the following mechanism:
- Displacement Map: A grayscale image (e.g.,
displacement-map-*.png) defines how the background is distorted. - Backdrop Filter: The effect is applied to an element using the CSS property
backdrop-filter: url(#filter-id). - SVG Filter Pipeline:
feImageis used to load the displacement map image into the filter context.feDisplacementMapuses that image to perform the actual distortion.- An optional specular map (e.g.,
specular-map-*.png) can be used to add highlights or reflections by compositing it with the result usingfeCompositeandfeBlend.
- Displacement Map: A grayscale image (e.g.,
Run HTML-in-Canvas examples locally
mainTo run the examples in this repository, navigate to the
html-in-canvasdirectory and serve thepublicdirectory using a local static file server.Ensure you are using Chrome 148+ with the
#canvas-draw-elementflag enabled to view the results.# 1. Navigate to the directory cd html-in-canvas # 2. Start a server (Option A: Node.js) npx serve public # OR (Option B: Python) python3 -m http.server --directory publicView Liquid Glass demos
mainTo view the extracted
Open `index.html` in a browser to view the extracted demos.Initialize a new Svelte project
mainUse
npm create svelte@latestto scaffold a new Svelte project. You can either initialize the project in the current directory or specify a directory name as an argument.# create a new project in the current directory npm create svelte@latest # create a new project in my-app npm create svelte@latest my-appDevelop and run the Svelte development server
mainAfter creating your project and installing dependencies via
npm install,pnpm install, oryarn, you can start the development server usingnpm run dev. To automatically open the application in a new browser tab, use the--openflag.npm run dev # or start the server and open the app in a new browser tab npm run dev -- --openRun hasUAVisualTransition locally
mainTo run the demo on your local machine, use
npx serveto serve thesrcdirectory and then access it via your browser.npx serve src # Then visit http://localhost:3000Publish the demo to chrome.dev
mainNote: Publishing is restricted to Googlers with proper access. The demo is published to
https://chrome.dev/has-ua-visual-transition/usinggcloud storage rsyncto sync thesrcdirectory to the Google Cloud Storage bucket.gcloud auth login gcloud storage rsync --recursive --delete-unmatched-destination-objects src gs://chrome-dev-demos/public/has-ua-visual-transition