Game of Life
bucharitesh/game-of-lifeFreeComponent
A game of life component for Next.js apps with next-themes and Tailwind CSS, supporting system, light, and dark modes.
Install it
npx shadcn@latest add https://bucharitesh.in/r/game-of-life.jsonSource
1'use client';23import { cn } from '@/lib/utils';4import * as React from 'react';56interface GameOfLifeProps7 extends React.CanvasHTMLAttributes<HTMLCanvasElement> {8 size?: number;9 interval?: number;10 backgroundColor?: string;11 cellColor?: string;12 density?: number; // Value between 0 and 1, default 0.1 (10% cells alive)13}1415const GameOfLife = React.forwardRef<HTMLCanvasElement, GameOfLifeProps>(16 (17 {18 className,19 size = 12,20 interval = 150,21 backgroundColor = '#000000',22 cellColor = '#1e1e1e',23 density = 0.1,24 ...props25 },26 ref27 ) => {28 const canvasRef = React.useRef<HTMLCanvasElement | null>(null);29 const frameRef = React.useRef<number>(0);30 const gridRef = React.useRef<boolean[][]>([]);31 const lastUpdateRef = React.useRef<number>(0);32 const transitionRef = React.useRef<{33 from: boolean[][];34 to: boolean[][];35 progress: number;36 } | null>(null);37 const [isReady, setIsReady] = React.useState(false);38 const isInitialRender = React.useRef(true);3940 React.useEffect(() => {41 const canvas = canvasRef.current;42 if (!canvas) return;4344 const ctx = canvas.getContext('2d', { alpha: false });45 if (!ctx) return;4647 const parent = canvas.parentElement;48 if (!parent) return;4950 // Keep size constant51 const cellSize = size;52 let width = parent.clientWidth;53 let height = parent.clientHeight;54 let cols = Math.floor(width / cellSize);55 let rows = Math.floor(height / cellSize);5657 const createGrid = (): boolean[][] => {58 const parent = canvas.parentElement;59 if (!parent)60 return Array.from({ length: cols }, () =>61 new Array(rows).fill(false)62 );6364 width = parent.clientWidth;65 height = parent.clientHeight;66 cols = Math.floor(width / cellSize);67 rows = Math.floor(height / cellSize);6869 // Update canvas size to match parent70 canvas.width = width;71 canvas.height = height;7273 // Create a random initial pattern based on density prop74 const grid = Array.from({ length: cols }, () =>75 Array.from({ length: rows }, () => Math.random() < density)76 );7778 return grid;79 };8081 const updateGrid = (grid: boolean[][]): boolean[][] => {82 const next: boolean[][] = Array.from({ length: cols }, () =>83 new Array(rows).fill(false)84 );8586 for (let i = 0; i < cols; i++) {87// … truncated
Files it writes
More from bucharitesh
All 26 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Bookbook | bucharitesh | Components | Free | 1 dep · 2 files | |
| Magical Mousemagical-mouse | bucharitesh | Components | Free | 1 dep | |
| Metallic Buttonmetallic-button | bucharitesh | Components | Free | 2 deps | |
| Modern Progressmodern-progress | bucharitesh | Components | Free | 1 dep | |
| Pixel Iconpixel-icon | bucharitesh | Components | Free | no deps | |
| Split Text Effectsplit-text-effect | bucharitesh | Components | Free | 1 dep | |
| Text Highlighttext-highlight | bucharitesh | Components | Free | 1 dep | |
| View Magnifierview-magnifier | bucharitesh | Components | Free | 1 dep |
