Bubble (Vanilla)
A vanilla TypeScript WebGL2 effect that draws a glassy, metaball-style droplet following the cursor over a live HTML subtree, refracting that page content. It exposes createBubble for attaching the effect to a canvas pair and returns a controller for option updates, resizing, and teardown.


shadcn add · no workarounds · theme neutral (none shipped)Installation
Requires @canvas-ui in the registries of your components.json (shadcn adds official directory namespaces automatically).
Usage
import { useEffect, useRef } from 'react';import { createBubble } from '@/components/canvasui/BubbleVanilla';export function BubbleHero() { const sourceRef = useRef<HTMLCanvasElement>(null); const contentRef = useRef<HTMLDivElement>(null); const outputRef = useRef<HTMLCanvasElement>(null); useEffect(() => { const source = sourceRef.current; const content = contentRef.current; const output = outputRef.current; if (!source || !content || !output) return; const bubble = createBubble({ source, content, output }); return () => bubble?.destroy(); }, []); return ( <canvas ref={sourceRef} className='relative h-64 w-full'> <div ref={contentRef} className='h-full w-full'> <h1>Hello</h1> </div> <canvas ref={outputRef} className='pointer-events-none absolute inset-0 h-full w-full' /> </canvas> );}- Adding a playful, glassmorphic cursor follower or hover highlight to a hero section, CTA, or card where the live page should remain visible through the effect
- Decorating interactive surfaces (buttons, navigation, feature tiles) with a liquid lens look without blocking clicks or text selection
- Plain-JavaScript or non-React projects, or React projects that want direct control over the canvas lifecycle, since the component has no dependencies
Examples
Glassy CTA button with default look
Wraps a button-sized region with the bubble effect. createBubble returns null when WebGL2 is unavailable, so the optional chaining in cleanup is required.
import { useEffect, useRef } from 'react';import { createBubble } from '@/components/canvasui/BubbleVanilla';export function GlassCta() { const sourceRef = useRef<HTMLCanvasElement>(null); const contentRef = useRef<HTMLDivElement>(null); const outputRef = useRef<HTMLCanvasElement>(null); useEffect(() => { const source = sourceRef.current; const content = contentRef.current; const output = outputRef.current; if (!source || !content || !output) return; const bubble = createBubble({ source, content, output }); return () => bubble?.destroy(); }, []); return ( <canvas ref={sourceRef} className='relative inline-block'> <div ref={contentRef}> <button type='button' className='rounded-full bg-slate-900 px-6 py-3 text-white'> Get started </button> </div> <canvas ref={outputRef} className='pointer-events-none absolute inset-0 h-full w-full' /> </canvas> );}Tinted droplet with live option updates
Passes custom options at creation and updates them later with setOptions, which only restarts rendering when a value actually changes.
import { useEffect, useRef } from 'react';import { createBubble, type BubbleInstance, type BubbleOptions } from '@/components/canvasui/BubbleVanilla';const warm: BubbleOptions = { tint: [1, 0.85, 0.7], tintStrength: 0.35, frost: 0.2, size: 40 };export function WarmLens({ intensity }: { intensity: number }) { const sourceRef = useRef<HTMLCanvasElement>(null); const contentRef = useRef<HTMLDivElement>(null); const outputRef = useRef<HTMLCanvasElement>(null); const instanceRef = useRef<BubbleInstance | null>(null); useEffect(() => { const source = sourceRef.current; const content = contentRef.current; const output = outputRef.current; if (!source || !content || !output) return; instanceRef.current = createBubble({ source, content, output }, warm); return () => { instanceRef.current?.destroy(); instanceRef.current = null; }; }, []); useEffect(() => { instanceRef.current?.setOptions({ intensity }); }, [intensity]); return ( <canvas ref={sourceRef} className='relative h-48 w-full'> <div ref={contentRef} className='h-full w-full p-6'> <p>Hover to see the warm lens.</p> </div> <canvas ref={outputRef} className='pointer-events-none absolute inset-0 h-full w-full' /> </canvas> );}Feature detection with a plain fallback
Checks supportsHtmlInCanvas before mounting and renders the static content when the experimental API is missing, so the page degrades cleanly.
import { useEffect, useRef, useState } from 'react';import { createBubble, supportsHtmlInCanvas } from '@/components/canvasui/BubbleVanilla';export function SafeBubbleCard() { const [supported, setSupported] = useState(false); const sourceRef = useRef<HTMLCanvasElement>(null); const contentRef = useRef<HTMLDivElement>(null); const outputRef = useRef<HTMLCanvasElement>(null); useEffect(() => { setSupported(supportsHtmlInCanvas()); }, []); useEffect(() => { if (!supported) return; const source = sourceRef.current; const content = contentRef.current; const output = outputRef.current; if (!source || !content || !output) return; const bubble = createBubble( { source, content, output }, { fallbackOpacity: 0.6, shine: 0.4 }, ); return () => bubble?.destroy(); }, [supported]); return ( <canvas ref={sourceRef} className='relative w-full'> <div ref={contentRef} className='p-8'> <h2>Card title</h2> </div> <canvas ref={outputRef} className='pointer-events-none absolute inset-0 h-full w-full' /> </canvas> );}API reference
Accessibility
- The effect is purely decorative; the wrapped HTML remains in the DOM and keeps its semantics, focus order, and native hit-testing because the output canvas should use pointer-events: none.
- Users with prefers-reduced-motion: reduce get a non-animated time clock and a faster settle, and the component listens for live changes to that media query.
- Rendering pauses when the output canvas leaves the viewport via IntersectionObserver, which reduces CPU and battery use for off-screen instances.
- Where HTML-in-canvas or WebGL2 is unavailable, createBubble degrades to a fallback (or returns null for missing WebGL2), so the content must remain fully usable without the effect.
- Keep text contrast sufficient on the refracted content, since frosting, tint, and dispersion alter the rendered appearance of text under the droplet.
- Call destroy() on unmount to remove listeners, observers, and WebGL resources, and avoid mounting many instances at once.
Docs written by anthropic:claude-haiku-5-5 from the registry source.
Use with Coding Agent
Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.
Use the "Bubble (Vanilla)" component (canvas-ui/bubble-vanilla) from its shadcn registry.1. Install it with: npx shadcn@latest add @canvas-ui/bubble-vanilla2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: elements.source, elements.content, elements.output, options.size, options.trail, options.follow, options.blend, options.speed, options.refraction, options.dispersion, options.frost, options.shine, options.rim, options.iridescence, options.intensity, options.tint, options.tintStrength, options.colorA, options.colorB, options.fallbackOpacity.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { useEffect, useRef } from 'react';import { createBubble } from '@/components/canvasui/BubbleVanilla';export function BubbleHero() { const sourceRef = useRef<HTMLCanvasElement>(null); const contentRef = useRef<HTMLDivElement>(null); const outputRef = useRef<HTMLCanvasElement>(null); useEffect(() => { const source = sourceRef.current; const content = contentRef.current; const output = outputRef.current; if (!source || !content || !output) return; const bubble = createBubble({ source, content, output }); return () => bubble?.destroy(); }, []); return ( <canvas ref={sourceRef} className='relative h-64 w-full'> <div ref={contentRef} className='h-full w-full'> <h1>Hello</h1> </div> <canvas ref={outputRef} className='pointer-events-none absolute inset-0 h-full w-full' /> </canvas> );}```Files & dependencies
- components/canvasui/BubbleVanilla.ts→ components/canvasui/BubbleVanilla.ts