1+ 'use client' ;
2+
3+ import React , { useEffect , useRef } from 'react' ;
4+ import "./pixelatedBackground.css" ;
5+
6+ // 2. Define Vertex Shader (Draws a full-screen quad)
7+ const vsSource = `
8+ attribute vec2 position;
9+ void main() {
10+ gl_Position = vec4(position, 0.0, 1.0);
11+ }
12+ ` ;
13+
14+ // 3. Define Fragment Shader (Generates pixelated procedural waves)
15+ const fsSource = `
16+ precision mediump float;
17+ uniform vec2 u_resolution;
18+ uniform float u_time;
19+
20+ void main() {
21+ // Pixel grid sizing: Adjust this number to change pixel size
22+ float pixelSize = 12.0; // 8.0
23+
24+ // Quantize coordinates to create the pixelated effect
25+ vec2 uv = floor(gl_FragCoord.xy / pixelSize) * pixelSize / u_resolution.xy;
26+
27+ // Create an example retro procedural animation pattern
28+ float colorA = sin(uv.x * 4.0 + u_time) * 0.5 + 0.5;
29+ float colorB = cos(uv.y * 4.0 - u_time) * 0.5 + 0.5;
30+
31+ gl_FragColor = vec4(colorA, colorB, 0.8, 1.0);
32+ }
33+ ` ;
34+
35+ export default function PixelatedBackground ( ) {
36+ const canvasRef = useRef < HTMLCanvasElement | null > ( null ) ;
37+ const contextRef = useRef < WebGLRenderingContext | null > ( null ) ;
38+ const shaderProgramRef = useRef < WebGLProgram | null > ( null ) ;
39+ const positionLocationRef = useRef < number | null > ( null ) ;
40+ const timeUniformLocationRef = useRef < WebGLUniformLocation | null > ( null ) ;
41+ const resolutionUniformLocationRef = useRef < WebGLUniformLocation | null > ( null ) ;
42+ const positionBufferRef = useRef < WebGLBuffer | null > ( null ) ;
43+ const frameRef = useRef < number | null > ( null ) ;
44+
45+ // Render loop
46+ function render ( time : number ) {
47+ const canvas = canvasRef . current ;
48+ if ( ! canvas ) return ;
49+ const gl = contextRef . current ;
50+ if ( ! gl ) return ;
51+ const program = shaderProgramRef . current ;
52+ if ( ! program ) return ;
53+
54+ const positionLocation = positionLocationRef . current ;
55+ const positionBuffer = positionBufferRef . current ;
56+ if ( positionLocation === null || positionLocation === undefined || ! positionBuffer ) return ;
57+
58+ const seconds = time * 0.001 ;
59+
60+ gl . clearColor ( 0 , 0 , 0 , 1 ) ;
61+ gl . clear ( gl . COLOR_BUFFER_BIT ) ;
62+
63+ gl . useProgram ( program ) ;
64+ gl . uniform2f ( resolutionUniformLocationRef . current , canvas . width , canvas . height ) ;
65+ gl . uniform1f ( timeUniformLocationRef . current , seconds ) ;
66+
67+ gl . bindBuffer ( gl . ARRAY_BUFFER , positionBuffer ) ;
68+ gl . enableVertexAttribArray ( positionLocation ) ;
69+ gl . vertexAttribPointer ( positionLocation , 2 , gl . FLOAT , false , 0 , 0 ) ;
70+
71+ gl . drawArrays ( gl . TRIANGLES , 0 , 6 ) ;
72+
73+ frameRef . current = requestAnimationFrame ( render ) ;
74+ }
75+
76+ function setup ( ) {
77+ const canvas = canvasRef . current ;
78+ if ( ! canvas ) return ;
79+ const gl = contextRef . current ;
80+ if ( ! gl ) return ;
81+
82+ const vertexShader = createShader ( gl , gl . VERTEX_SHADER , vsSource ) ;
83+ const fragmentShader = createShader ( gl , gl . FRAGMENT_SHADER , fsSource ) ;
84+
85+ if ( ! vertexShader || ! fragmentShader ) return ;
86+
87+ const program = gl . createProgram ( ) ;
88+ if ( ! program ) return ;
89+
90+ gl . attachShader ( program , vertexShader ) ;
91+ gl . attachShader ( program , fragmentShader ) ;
92+ gl . linkProgram ( program ) ;
93+
94+ if ( ! gl . getProgramParameter ( program , gl . LINK_STATUS ) ) {
95+ console . error ( gl . getProgramInfoLog ( program ) ) ;
96+ return ;
97+ }
98+
99+ shaderProgramRef . current = program ;
100+ positionLocationRef . current = gl . getAttribLocation ( program , 'position' ) ;
101+ resolutionUniformLocationRef . current = gl . getUniformLocation ( program , 'u_resolution' ) ;
102+ timeUniformLocationRef . current = gl . getUniformLocation ( program , 'u_time' ) ;
103+
104+ const positionBuffer = gl . createBuffer ( ) ;
105+ if ( ! positionBuffer ) return ;
106+
107+ positionBufferRef . current = positionBuffer ;
108+ gl . bindBuffer ( gl . ARRAY_BUFFER , positionBuffer ) ;
109+ gl . bufferData (
110+ gl . ARRAY_BUFFER ,
111+ new Float32Array ( [
112+ - 1 , - 1 , 1 , - 1 , - 1 , 1 ,
113+ - 1 , 1 , 1 , - 1 , 1 , 1 ,
114+ ] ) ,
115+ gl . STATIC_DRAW ,
116+ ) ;
117+ }
118+
119+ // Resize handler to match screen dimensions
120+ function resize ( ) {
121+ const canvas = canvasRef . current ;
122+ if ( ! canvas ) return ;
123+ const gl = contextRef . current ;
124+ if ( ! gl ) return ;
125+
126+ const scale = 4 ;
127+ const width = Math . floor ( window . innerWidth / scale ) ;
128+ const height = Math . floor ( window . innerHeight / scale ) ;
129+
130+ if ( canvas . width !== width || canvas . height !== height ) {
131+ canvas . width = width ;
132+ canvas . height = height ;
133+ gl . viewport ( 0 , 0 , width , height ) ;
134+ }
135+ }
136+
137+ // Helper function to compile shaders
138+ function createShader ( gl : WebGLRenderingContext , type : number , source : string ) {
139+ const shader = gl . createShader ( type ) ;
140+ if ( ! shader ) return null ;
141+ gl . shaderSource ( shader , source ) ;
142+ gl . compileShader ( shader ) ;
143+ if ( ! gl . getShaderParameter ( shader , gl . COMPILE_STATUS ) ) {
144+ console . error ( gl . getShaderInfoLog ( shader ) ) ;
145+ gl . deleteShader ( shader ) ;
146+ return null ;
147+ }
148+ return shader ;
149+ }
150+
151+ useEffect ( ( ) => {
152+ const canvas = canvasRef . current ;
153+ if ( ! canvas ) return ;
154+
155+ const gl = canvas . getContext ( 'webgl' , { antialias : false } ) ;
156+ if ( ! gl ) {
157+ console . error ( 'WebGL not supported' ) ;
158+ return ;
159+ }
160+
161+ contextRef . current = gl ;
162+
163+ window . addEventListener ( 'resize' , resize ) ;
164+ resize ( ) ;
165+ setup ( ) ;
166+
167+ frameRef . current = requestAnimationFrame ( render ) ;
168+
169+ return ( ) => {
170+ window . removeEventListener ( 'resize' , resize ) ;
171+ if ( frameRef . current !== null ) {
172+ cancelAnimationFrame ( frameRef . current ) ;
173+ }
174+ if ( shaderProgramRef . current ) {
175+ gl . deleteProgram ( shaderProgramRef . current ) ;
176+ }
177+ if ( positionBufferRef . current ) {
178+ gl . deleteBuffer ( positionBufferRef . current ) ;
179+ }
180+ contextRef . current = null ;
181+ } ;
182+ } , [ ] ) ;
183+
184+ return (
185+ < canvas
186+ id = "webgl-canvas"
187+ ref = { canvasRef }
188+ className = "pixelated fixed top-0 left-0 w-screen h-screen -z-100 pointer-events-none"
189+ style = { { imageRendering : 'pixelated' } }
190+ />
191+ ) ;
192+ }
0 commit comments