How to Create an Animated Space / Stars Background Effect in React / Next.js
Jay @ Designly
Posted on November 6, 2023
In a digital world brimming with interactivity and motion, here is a stellar example of how simplicity can breathe life into web design. This React component not only brings a sprinkle of cosmic wonder to any project but does so with elegance and ease, inviting both developers and users on an interstellar journey right from their screens!
If you want to just get started right away with react-starfield
, then simply run npm i react-starfield
. If you're interested in learning how it works, or would like to implement a customized version, then read on!
React-starfield is nothing more than an HTML canvas element with a little bit of math in a useEffect
:
'use client';
import React, { useEffect } from 'react';
interface Props {
speedFactor?: number;
backgroundColor?: string;
starColor?: [number, number, number];
starCount?: number;
}
export default function Starfield(props: Props) {
const { speedFactor = 0.05, backgroundColor = 'black', starColor = [255, 255, 255], starCount = 5000 } = props;
useEffect(() => {
const canvas = document.getElementById('starfield') as HTMLCanvasElement;
if (canvas) {
const c = canvas.getContext('2d');
if (c) {
let w = window.innerWidth;
let h = window.innerHeight;
const setCanvasExtents = () => {
canvas.width = w;
canvas.height = h;
};
setCanvasExtents();
window.onresize = () => {
setCanvasExtents();
};
const makeStars = (count: number) => {
const out = [];
for (let i = 0; i < count; i++) {
const s = {
x: Math.random() * 1600 - 800,
y: Math.random() * 900 - 450,
z: Math.random() * 1000,
};
out.push(s);
}
return out;
};
let stars = makeStars(starCount);
const clear = () => {
c.fillStyle = backgroundColor;
c.fillRect(0, 0, canvas.width, canvas.height);
};
const putPixel = (x: number, y: number, brightness: number) => {
const rgb =
'rgba(' + starColor[0] + ',' + starColor[1] + ',' + starColor[2] + ',' + brightness + ')';
c.fillStyle = rgb;
c.fillRect(x, y, 1, 1);
};
const moveStars = (distance: number) => {
const count = stars.length;
for (var i = 0; i < count; i++) {
const s = stars[i];
s.z -= distance;
while (s.z <= 1) {
s.z += 1000;
}
}
};
let prevTime: number;
const init = (time: number) => {
prevTime = time;
requestAnimationFrame(tick);
};
const tick = (time: number) => {
let elapsed = time - prevTime;
prevTime = time;
moveStars(elapsed * speedFactor);
clear();
const cx = w / 2;
const cy = h / 2;
const count = stars.length;
for (var i = 0; i < count; i++) {
const star = stars[i];
const x = cx + star.x / (star.z * 0.001);
const y = cy + star.y / (star.z * 0.001);
if (x < 0 || x >= w || y < 0 || y >= h) {
continue;
}
const d = star.z / 1000.0;
const b = 1 - d * d;
putPixel(x, y, b);
}
requestAnimationFrame(tick);
};
requestAnimationFrame(init);
// add window resize listener:
window.addEventListener('resize', function () {
w = window.innerWidth;
h = window.innerHeight;
setCanvasExtents();
});
} else {
console.error('Could not get 2d context from canvas element');
}
} else {
console.error('Could not find canvas element with id "starfield"');
}
return () => {
window.onresize = null;
};
}, [starColor, backgroundColor, speedFactor, starCount]);
return (
<canvas
id="starfield"
style={{
padding: 0,
margin: 0,
position: 'fixed',
top: 0,
right: 0,
bottom: 0,
left: 0,
zIndex: 10,
opacity: 1,
pointerEvents: 'none',
mixBlendMode: 'screen',
}}
></canvas>
);
}
You can simply import this component anywhere in your app (preferably in your main app component) and place it right before the end of your </body>
. It's good to keep things that are not semantic HTML out of the way of actual content.
Here's an example usage:
import Starfield from 'react-starfield';
function App() {
return (
<div className="App">
<Starfield
starCount={1000}
starColor={[255, 255, 255]}
speedFactor={0.05}
backgroundColor="black"
/>
{/* Other components */}
</div>
);
}
export default App;
Well, that's about it. If you like my component, then please install and... ahem STAR it on Github!
Links
Thank you for taking the time to read my article and I hope you found it useful (or at the very least, mildly entertaining). For more great information about web dev, systems administration and cloud computing, please read the Designly Blog. Also, please leave your comments! I love to hear thoughts from my readers.
If you want to support me, please follow me on Spotify!
Looking for a web developer? I'm available for hire! To inquire, please fill out a contact form.
Posted on November 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 6, 2023