canvas camera moving

This commit is contained in:
2026-03-04 18:45:42 -06:00
parent 0b66096fc2
commit 6d5e6b5202
3 changed files with 23 additions and 15 deletions

View File

@@ -1,20 +1,23 @@
import {motion} from "framer-motion"; import {motion} from "framer-motion";
import "./canvas.scss"; import "./canvas.scss";
import {Canvas as ThreeCanvas, useFrame} from "@react-three/fiber"; import {Canvas as ThreeCanvas, useFrame} from "@react-three/fiber";
import {useMemo, useRef} from "react"; import React, {useMemo, useRef} from "react";
import {Mesh} from "three"; import {Mesh, Vector3} from "three";
import {Moon} from "./Moon"; import {Moon} from "./Moon";
type Coordinate = [number, number, number];
export default function Canvas() { export default function Canvas() {
return ( return (
<main> <main>
<ThreeCanvas camera={{position: [5,5,5]}} shadows="basic"> <ThreeCanvas gl={{antialias: true, alpha: true}} >
<Sphere/> <Sphere/>
<Frame/>
<Moon radius={8} color="pink" step={0.01}/> <Moon radius={8} color="pink" step={0.01}/>
<Moon radius={6} color="grey" step={0.02}/> <Moon radius={6} color="grey" step={0.02}/>
<Moon radius={4} color="green" step={0.03}/> <Moon radius={4} color="green" step={0.03}/>
<Stars/> <Stars/>
<directionalLight color="#E3A857" args={[1,10]} position={[100,10,100]} /> <directionalLight color="#E3A857" args={[1,10]} position={[100,10,100]} />
</ThreeCanvas> </ThreeCanvas>
<motion.h1 className="welcome" animate={{ rotate: 360 }}> <motion.h1 className="welcome" animate={{ rotate: 360 }}>
Welcome to my blog Welcome to my blog
@@ -23,6 +26,21 @@ export default function Canvas() {
) )
} }
function Frame() {
useFrame(state => {
const { x, y } = state.pointer
// Smoothly interpolate the camera position
// We multiply by a factor (e.g., 5) to increase the range of movement
state.camera.position.lerp(new Vector3(x * 10, y * 10, 5), 0.1)
// Always keep the camera looking at the center
state.camera.lookAt(0, 0, 0)
})
return <></>
}
function Sphere() { function Sphere() {
const meshRef = useRef<Mesh>(null); const meshRef = useRef<Mesh>(null);
@@ -42,7 +60,6 @@ function Sphere() {
const RADIUS = 20; const RADIUS = 20;
const COUNT = 100; const COUNT = 100;
type Coordinate = [number, number, number];
function Stars() { function Stars() {
const positions = useMemo(() => { const positions = useMemo(() => {
const positions: Coordinate[] = []; const positions: Coordinate[] = [];

View File

@@ -6,17 +6,11 @@ main {
align-items: center; align-items: center;
background: black; background: black;
color: white; color: white;
position: absolute;
top: 0;
z-index: -999; z-index: -999;
} }
.welcome { .welcome {
text-align: center; text-align: center;
z-index: 99;
position: absolute; position: absolute;
top: 50%; top: 50%;
} }
.canvas {
}

View File

@@ -41,10 +41,7 @@ $menu: #282c34;
flex-grow: 1; flex-grow: 1;
} }
.wave { .wave {
position: absolute;
z-index: 999;
user-select: none; user-select: none;
pointer-events: none; pointer-events: none;
left: -10%; position: absolute;
width: 110%;
} }