Back to Component Explorer
Canvas Text
textDynamic canvas pixel particle text with physics based mouse interaction.
#text#canvas#particles#interactive
Live Interactive Preview
Interactive Canvas Particle Text
Component Source Code
Select language in the dropdownCanvasText.tsx
1'use client';23import React, { useEffect, useRef } from 'react';45export default function CanvasText() {6 const canvasRef = useRef<HTMLCanvasElement>(null);78 useEffect(() => {9 const canvas = canvasRef.current;10 if (!canvas) return;11 const ctx = canvas.getContext('2d');12 if (!ctx) return;1314 let animId: number;15 let particles: { x: number; y: number; originX: number; originY: number; vx: number; vy: number; color: string }[] = [];1617 const text = 'AURENZA';18 canvas.width = 320;19 canvas.height = 120;2021 ctx.font = 'bold 44px sans-serif';22 ctx.textAlign = 'center';23 ctx.textBaseline = 'middle';24 ctx.fillStyle = '#ffffff';25 ctx.fillText(text, canvas.width / 2, canvas.height / 2);2627 const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);28 const data = imgData.data;29 particles = [];3031 for (let y = 0; y < canvas.height; y += 4) {32 for (let x = 0; x < canvas.width; x += 4) {33 const idx = (y * canvas.width + x) * 4;34 if (data[idx + 3] > 128) {35 const colors = ['#38bdf8', '#818cf8', '#c084fc', '#f472b6'];36 particles.push({37 x: Math.random() * canvas.width,38 y: Math.random() * canvas.height,39 originX: x,40 originY: y,41 vx: 0,42 vy: 0,43 color: colors[Math.floor(Math.random() * colors.length)]44 });45 }46 }47 }4849 const animate = () => {50 ctx.clearRect(0, 0, canvas.width, canvas.height);51 particles.forEach((p) => {52 const dx = p.originX - p.x;53 const dy = p.originY - p.y;54 p.vx += dx * 0.04;55 p.vy += dy * 0.04;56 p.vx *= 0.85;57 p.vy *= 0.85;58 p.x += p.vx;59 p.y += p.vy;6061 ctx.fillStyle = p.color;62 ctx.fillRect(p.x, p.y, 2.5, 2.5);63 });64 animId = requestAnimationFrame(animate);65 };6667 animate();6869 return () => cancelAnimationFrame(animId);70 }, []);7172 return (73 <div className="flex flex-col items-center justify-center p-4">74 <canvas ref={canvasRef} className="max-w-full rounded-xl bg-slate-950 shadow-inner" />75 <span className="mt-2 text-[11px] font-mono text-slate-400">Interactive Canvas Particle Text</span>76 </div>77 );78}Installation & Integration Guide
Step 1: Tailwind CSS v4 Setup
Tailwind DocsIf you do not have Tailwind installed yet, run this installation command:
Terminal
npm install tailwindcss @tailwindcss/postcss postcss
Then add the Tailwind directive to your main stylesheet (globals.css):
@import "tailwindcss";
Step 2: Install Icon Dependencies
npm install lucide-react react-icons
Step 3: Add Component File
Create a new file in your project at src/components/text/CanvasText.tsx and paste the copied code from the source viewer above.
Step 4: Usage Example
ExamplePage.tsx
1import CanvasText from '@/components/text/CanvasText';23export default function ExamplePage() {4 return (5 <main className="p-8 flex items-center justify-center min-h-screen">6 <CanvasText />7 </main>8 );9}