From 54fef7af423b6d2906e8dcfaf153201488af7a2c Mon Sep 17 00:00:00 2001 From: gmook9 Date: Sat, 10 Aug 2024 23:51:23 -0700 Subject: [PATCH 1/2] added radio buttons (stars) to allow user to toggle speed of background --- src/app/home/page.tsx | 46 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/app/home/page.tsx b/src/app/home/page.tsx index c44d69d..e37e027 100644 --- a/src/app/home/page.tsx +++ b/src/app/home/page.tsx @@ -8,6 +8,7 @@ import Avatar from '../components/Avatar'; const Home: React.FC = () => { const [init, setInit] = useState(false); + const [starRating, setStarRating] = useState(1); // State for star rating useEffect(() => { initParticlesEngine(async (engine) => { @@ -68,7 +69,7 @@ const Home: React.FC = () => { default: OutMode.out, }, random: true, - speed: 1, + speed: starRating, // Adjust speed based on star rating straight: false, }, number: { @@ -89,7 +90,7 @@ const Home: React.FC = () => { }, detectRetina: true, }), - [] + [starRating] ); return ( @@ -142,8 +143,47 @@ const Home: React.FC = () => { btnLink="https://st4rdelic.com/" /> +
+
+ setStarRating(1)} + /> + setStarRating(2)} + /> + setStarRating(3)} + /> + setStarRating(4)} + /> + setStarRating(5)} + /> +
+
); }; -export default Home; \ No newline at end of file +export default Home; From 14b0c79107984a938e580717ccdae95f857335e4 Mon Sep 17 00:00:00 2001 From: gmook9 Date: Sat, 10 Aug 2024 23:57:18 -0700 Subject: [PATCH 2/2] moved particle background to component to remove duplicate code --- src/app/components/ParticleBackground.tsx | 104 +++++++++++++++++++++ src/app/home/page.tsx | 94 +------------------ src/app/page.tsx | 109 ++-------------------- 3 files changed, 117 insertions(+), 190 deletions(-) create mode 100644 src/app/components/ParticleBackground.tsx diff --git a/src/app/components/ParticleBackground.tsx b/src/app/components/ParticleBackground.tsx new file mode 100644 index 0000000..bbec95c --- /dev/null +++ b/src/app/components/ParticleBackground.tsx @@ -0,0 +1,104 @@ +'use client' +import React, { useEffect, useMemo, useState } from 'react'; +import Particles, { initParticlesEngine } from '@tsparticles/react'; +import { loadSlim } from '@tsparticles/slim'; +import { type ISourceOptions, MoveDirection, OutMode } from '@tsparticles/engine'; + +interface ParticleBackgroundProps { + speed: number; +} + +const ParticleBackground: React.FC = ({ speed }) => { + const [init, setInit] = useState(false); + + useEffect(() => { + initParticlesEngine(async (engine) => { + await loadSlim(engine); + }).then(() => { + setInit(true); + }); + }, []); + + const particlesLoaded = async (container?: any): Promise => { + console.log(container); + }; + + const options: ISourceOptions = useMemo( + () => ({ + background: { + color: { + value: '#1a202c', + }, + }, + fpsLimit: 120, + interactivity: { + events: { + onClick: { + enable: true, + mode: 'push', + }, + onHover: { + enable: true, + mode: 'repulse', + }, + }, + modes: { + push: { + quantity: 4, + }, + repulse: { + distance: 200, + duration: 0.4, + }, + }, + }, + particles: { + color: { + value: '#ffffff', + }, + links: { + color: '#ffffff', + distance: 150, + enable: true, + opacity: 0.5, + width: 1, + }, + move: { + direction: MoveDirection.none, + enable: true, + outModes: { + default: OutMode.out, + }, + random: true, + speed: speed, // speed prop + straight: false, + }, + number: { + density: { + enable: true, + }, + value: 80, + }, + opacity: { + value: 0.5, + }, + shape: { + type: 'star', + }, + size: { + value: { min: 1, max: 5 }, + }, + }, + detectRetina: true, + }), + [speed] + ); + + return ( + <> + {init && } + + ); +}; + +export default ParticleBackground; \ No newline at end of file diff --git a/src/app/home/page.tsx b/src/app/home/page.tsx index e37e027..50a01db 100644 --- a/src/app/home/page.tsx +++ b/src/app/home/page.tsx @@ -1,103 +1,15 @@ 'use client' -import React, { useEffect, useMemo, useState } from 'react'; -import Particles, { initParticlesEngine } from '@tsparticles/react'; -import { loadSlim } from '@tsparticles/slim'; -import { type ISourceOptions, MoveDirection, OutMode } from '@tsparticles/engine'; +import React, { useState } from 'react'; +import ParticleBackground from '../components/ParticleBackground'; import Card from '../components/Card'; import Avatar from '../components/Avatar'; const Home: React.FC = () => { - const [init, setInit] = useState(false); const [starRating, setStarRating] = useState(1); // State for star rating - useEffect(() => { - initParticlesEngine(async (engine) => { - await loadSlim(engine); - }).then(() => { - setInit(true); - }); - }, []); - - const particlesLoaded = async (container?: any): Promise => { - console.log(container); - }; - - const options: ISourceOptions = useMemo( - () => ({ - background: { - color: { - value: '#1a202c', - }, - }, - fpsLimit: 120, - interactivity: { - events: { - onClick: { - enable: true, - mode: 'push', - }, - onHover: { - enable: true, - mode: 'repulse', - }, - }, - modes: { - push: { - quantity: 4, - }, - repulse: { - distance: 200, - duration: 0.4, - }, - }, - }, - particles: { - color: { - value: '#ffffff', - }, - links: { - color: '#ffffff', - distance: 150, - enable: true, - opacity: 0.5, - width: 1, - }, - move: { - direction: MoveDirection.none, - enable: true, - outModes: { - default: OutMode.out, - }, - random: true, - speed: starRating, // Adjust speed based on star rating - straight: false, - }, - number: { - density: { - enable: true, - }, - value: 80, - }, - opacity: { - value: 0.5, - }, - shape: { - type: 'star', - }, - size: { - value: { min: 1, max: 5 }, - }, - }, - detectRetina: true, - }), - [starRating] - ); - return (
- {init && ( - - )} +
diff --git a/src/app/page.tsx b/src/app/page.tsx index 7f653d4..affa15f 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,108 +1,19 @@ 'use client' -import React, { useEffect, useMemo, useState } from 'react'; -import Particles, { initParticlesEngine } from '@tsparticles/react'; -import { loadSlim } from '@tsparticles/slim'; -import { type ISourceOptions, MoveDirection, OutMode } from '@tsparticles/engine'; +import React from 'react'; +import ParticleBackground from '../app/components/ParticleBackground'; import CodeBlock from '../app/components/CodeBlock'; const HomePage: React.FC = () => { - const [init, setInit] = useState(false); + const starRating = 1; - useEffect(() => { - initParticlesEngine(async (engine) => { - await loadSlim(engine); - }).then(() => { - setInit(true); - }); - }, []); - - const particlesLoaded = async (container?: any): Promise => { - console.log(container); - }; - - const options: ISourceOptions = useMemo( - () => ({ - background: { - color: { - value: '#1a202c', - }, - }, - fpsLimit: 120, - interactivity: { - events: { - onClick: { - enable: true, - mode: 'push', - }, - onHover: { - enable: true, - mode: 'repulse', - }, - }, - modes: { - push: { - quantity: 4, - }, - repulse: { - distance: 200, - duration: 0.4, - }, - }, - }, - particles: { - color: { - value: '#ffffff', - }, - links: { - color: '#ffffff', - distance: 150, - enable: true, - opacity: 0.5, - width: 1, - }, - move: { - direction: MoveDirection.none, - enable: true, - outModes: { - default: OutMode.out, - }, - random: true, - speed: 1, - straight: false, - }, - number: { - density: { - enable: true, - }, - value: 80, - }, - opacity: { - value: 0.5, - }, - shape: { - type: 'star', - }, - size: { - value: { min: 1, max: 5 }, - }, - }, - detectRetina: true, - }), - [] - ); - - if (init) { - return ( -
- -
- -
+ return ( +
+ +
+
- ); - } - - return <>; +
+ ); }; export default HomePage;