Working on scroll interactivity, and mouse events

This commit is contained in:
Frank Delaguila
2022-03-15 23:56:59 -06:00
parent 902844cdd1
commit 6c8911796b
3 changed files with 40 additions and 17 deletions

View File

@@ -1,14 +1,15 @@
import '../styles/styles.scss';
import * as THREE from 'three';
import { useRef, useState } from 'react';
import { useFrame } from '@react-three/fiber';
import { Text, Environment, MeshReflectorMaterial, useTexture, MeshDistortMaterial, Html } from '@react-three/drei';
import { useFrame, useThree } from '@react-three/fiber';
import { Text, Environment, MeshReflectorMaterial, useTexture, Html, Image, Scroll, useScroll, ScrollControls } from '@react-three/drei';
import { gsap } from 'gsap';
import dreiVideo from '../assets/videos/drei.mp4';
import floorTexture from '../assets/textures/texture1.jpg';
import floorTextureNormal from '../assets/textures/texture1_normal.jpg';
import font from '../assets/fonts/Inter-Bold.woff';
import logo from '../assets/logos/fd..svg';
const Floor = () => {
const [floor, normal] = useTexture([floorTexture, floorTextureNormal]);
@@ -34,9 +35,29 @@ const Floor = () => {
);
};
const Dog = ({position, scale}) => {
const ref = useRef();
const { offset } = useScroll();
const [hovered, setHovered] = useState(false);
useFrame((state, delta) => {
// ref.current.position.x = THREE.MathUtils.damp(ref.current.position.x, position[0] - 2, 6, delta);
ref.current.scale.x = THREE.MathUtils.damp(ref.current.scale.x, hovered ? 0.9 : scale[0], 6, delta);
ref.current.scale.y = THREE.MathUtils.damp(ref.current.scale.y, hovered ? scale[1] + 0.2 : scale[1], 8, delta);
// ref.current.position.x = (position[0] - 1) * scroll.current;
// console.log( ref.current.position.x );
});
return (
<Image onPointerOver={() => setHovered(true)} onPointerOut={() => setHovered(false)} ref={ref} url="https://picsum.photos/id/237/1920/1024" position={position} rotation={[0, 0, 0]} scale={scale}/>
);
};
const App = () => {
const textRef = useRef();
const cloudRef = useRef();
const { width } = useThree( (state) => state.viewport );
const scroll = useScroll();
const [video] = useState(() => {
const vid = document.createElement('video');
vid.src = dreiVideo;
@@ -52,13 +73,19 @@ const App = () => {
gsap.to( cloudRef.current.position, {x: "3"} );
};
useFrame((state) => {
// console.log( state );
});
const xW = 0.7 + 0.15;
return (
<>
<color attach="background" args={["black"]} />
<fog attach="fog" args={["black", 1, 4]} />
<ScrollControls horizontal damping={10} pages={5}>
<Scroll>
<Dog position={[1.5 * xW, 0.5, -2.5]} scale={[0.7, 2, 1]} />
<Dog position={[2.5 * xW, 0.5, -2.5]} scale={[0.7, 2, 1]} />
<Dog position={[3.5 * xW, 0.5, -2.5]} scale={[0.7, 2, 1]} />
</Scroll>
</ScrollControls>
<group position={[0, -1, 0]}>
<Text
ref={textRef}
@@ -78,18 +105,12 @@ const App = () => {
/>
</meshStandardMaterial>
</Text>
<Html position={[-1.5, 1.4, -3.2]} rotation={[0, 0, 0]}>
<Html position={[0, 0, 0]} rotation={[0, 0, 0]}>
<nav className="flex items-center justify-center w-100">
<img src={logo} alt="" />
<h1 onClick={meshAnimation} className="text-4xl font-bold text-white cursor-pointer">HELLO</h1>
</nav>
</Html>
<mesh position={[2, 1.8, -3.2]} ref={cloudRef} fog={false}>
<sphereBufferGeometry attach="geometry" />
<MeshDistortMaterial
attach="material"
color="#EDEDED"
distort={0.5} // Strength, 0 disables the effect (default=1)
speed={1} // Speed (default=1)
/>
</mesh>
<pointLight position={[-1, 0, 0]} />
<Floor />
</group>

View File

@@ -7,7 +7,7 @@ import App from '../components/App';
render(
<>
<Suspense fallback={null}>
<Canvas mode='concurrent' dpr={[1, 1.5]} camera={{ position: [0, 0, 0]}} id="canvas-container" style={{position: 'absolute'}}>
<Canvas mode='concurrent' gl={{ antialias: true }} dpr={[1, 1.5]} camera={{ position: [0, 0, 0]}} id="canvas-container" style={{position: 'absolute'}}>
<App />
</Canvas>
</Suspense>

View File

@@ -6,6 +6,8 @@ html,
body {
margin: 0;
padding: 0;
font-family: 'Museo Sans';
overscroll-behavior: none;
}
#root {