Layout and positioning
Size and place a shader in your page with CSS, as a background, a fill, or an inline block.
Sizing the canvas
A <Shader> renders one <div> with one <canvas> inside that fills it. However many components you add, there's still one canvas. The classes and styles you pass go on the <div>, so you size it like any other block element:
In the JS API you pass your own <canvas>, so you style it directly.
Covering the whole page
To fix a shader behind all page content, covering the viewport, give it fixed inset-0 and a negative z-index:
Filling a section
To fill one section without affecting the rest of the page, position the shader absolutely inside it. The section needs position: relative and overflow: hidden to contain the canvas, and isolation: isolate (isolate) so the negative z-index stays inside the section instead of dropping behind its background:
Filling a card
The same pattern works as the background of a card or panel. overflow: hidden also clips the canvas to the card's rounded corners:
Placing a shader inline
Without positioning, a shader flows in the document like an image or video. Give it a width and an aspect ratio:
Letting clicks pass through
The canvas captures pointer events like any element. When the shader is decorative, add pointer-events: none so clicks and hovers reach the content underneath:
Cursor effects such as CursorTrail and CursorRipples keep working with pointer-events: none, because they listen on the window, not the canvas.
Stacking content above the canvas
z-index only works on positioned elements. Give any content you want above the canvas position: relative (or another position) and a higher z-index:
<div class="relative">
<!-- canvas at z-0 -->
<canvas class="absolute inset-0"></canvas>
<!-- content above canvas -->
<div class="relative z-10">I appear above the canvas</div>
</div>
Resizing at breakpoints
The canvas follows any resize that comes from CSS, including breakpoints, aspect ratios, and viewport units:
Styling the canvas
Borders, radii, shadows and CSS filters apply as they would to any element:
Next steps
- Transforms: position, size, and rotate a layer inside the canvas.
- Performance: what a large or offscreen canvas costs.
- Composing effects: stack and nest the components inside a
<Shader>.