mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-16 01:01:38 +00:00
feat(3d): add camera animation
This commit is contained in:
43
components/models/home/desk-camera.tsx
Normal file
43
components/models/home/desk-camera.tsx
Normal file
@ -0,0 +1,43 @@
|
||||
import * as THREE from 'three';
|
||||
import { JSX, useRef } from 'react';
|
||||
import { useFrame } from '@react-three/fiber';
|
||||
import { easing } from 'maath';
|
||||
import { useMediaQuery } from 'react-responsive';
|
||||
|
||||
const DeskCamera = ({ children }: { children: JSX.Element }) => {
|
||||
const groupRef = useRef<THREE.Group>(null);
|
||||
|
||||
const isMobile = useMediaQuery({ query: '(max-width: 768px)' });
|
||||
useFrame((state, delta) => {
|
||||
easing.damp3(state.camera.position, [0, 0, 26], 0.25, delta);
|
||||
|
||||
if (!isMobile && groupRef.current) {
|
||||
const x = (() => {
|
||||
const _x = state.pointer.y / 3;
|
||||
if (_x < 0) {
|
||||
if (_x <= -0.14) {
|
||||
return -0.14;
|
||||
} else {
|
||||
return _x;
|
||||
}
|
||||
} else {
|
||||
if (_x >= 0.14) {
|
||||
return 0.14;
|
||||
} else {
|
||||
return _x;
|
||||
}
|
||||
}
|
||||
})();
|
||||
easing.dampE(
|
||||
groupRef.current.rotation,
|
||||
[-x, -state.pointer.x / 5, 0],
|
||||
0.25,
|
||||
delta,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return <group ref={groupRef}>{children}</group>;
|
||||
};
|
||||
|
||||
export default DeskCamera;
|
Reference in New Issue
Block a user