aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/app/components/ThreeBackground.tsx
diff options
context:
space:
mode:
author简律纯 <i@jyunko.cn>2025-01-26 23:29:23 +0800
committerGitHub <noreply@github.com>2025-01-26 23:29:23 +0800
commita97b7a38f4a652e7b24e101ef34e4f37b8c3094e (patch)
treec387ca55aa6977499b03346ab46e63a0f7c09b36 /src/app/components/ThreeBackground.tsx
parentd4d7fac39528b978b742420a3cb4e91cea6f5834 (diff)
parentcd8aab3a511091ce378ff7ebcaa42bf979f00882 (diff)
downloadHydroRollSite-main.tar.gz
HydroRollSite-main.zip
Merge pull request #8 from LofiSu/重构官网HEADmain
Refactor: Rewrite Site
Diffstat (limited to 'src/app/components/ThreeBackground.tsx')
-rw-r--r--src/app/components/ThreeBackground.tsx79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/app/components/ThreeBackground.tsx b/src/app/components/ThreeBackground.tsx
new file mode 100644
index 0000000..5040e3c
--- /dev/null
+++ b/src/app/components/ThreeBackground.tsx
@@ -0,0 +1,79 @@
+"use client";
+
+import React, { useEffect, useRef } from "react";
+import * as THREE from "three";
+
+export const ThreeBackground: React.FC = () => {
+ const containerRef = useRef<HTMLDivElement>(null);
+
+ useEffect(() => {
+ if (!containerRef.current) return;
+
+ const scene = new THREE.Scene();
+ const camera = new THREE.PerspectiveCamera(
+ 75,
+ window.innerWidth / window.innerHeight,
+ 0.1,
+ 1000
+ );
+ const renderer = new THREE.WebGLRenderer({ alpha: true });
+
+ renderer.setSize(window.innerWidth, window.innerHeight);
+ containerRef.current.appendChild(renderer.domElement);
+
+ // Create particles
+ const geometry = new THREE.BufferGeometry();
+ const vertices = [];
+
+ for (let i = 0; i < 5000; i++) {
+ vertices.push(
+ THREE.MathUtils.randFloatSpread(2000), // x
+ THREE.MathUtils.randFloatSpread(2000), // y
+ THREE.MathUtils.randFloatSpread(2000) // z
+ );
+ }
+
+ geometry.setAttribute(
+ "position",
+ new THREE.Float32BufferAttribute(vertices, 3)
+ );
+
+ const material = new THREE.PointsMaterial({
+ color: 0x88ccff,
+ size: 2,
+ transparent: true,
+ opacity: 0.5,
+ });
+
+ const particles = new THREE.Points(geometry, material);
+ scene.add(particles);
+
+ camera.position.z = 1000;
+
+ const animate = () => {
+ requestAnimationFrame(animate);
+ particles.rotation.x += 0.0001;
+ particles.rotation.y += 0.0001;
+ renderer.render(scene, camera);
+ };
+
+ animate();
+
+ const handleResize = () => {
+ camera.aspect = window.innerWidth / window.innerHeight;
+ camera.updateProjectionMatrix();
+ renderer.setSize(window.innerWidth, window.innerHeight);
+ };
+
+ window.addEventListener("resize", handleResize);
+
+ return () => {
+ window.removeEventListener("resize", handleResize);
+ containerRef.current?.removeChild(renderer.domElement);
+ };
+ }, []);
+
+ return (
+ <div ref={containerRef} className="fixed top-0 left-0 w-full h-full z-0" />
+ );
+};