-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathApp.js
More file actions
executable file
Β·206 lines (201 loc) Β· 7.28 KB
/
App.js
File metadata and controls
executable file
Β·206 lines (201 loc) Β· 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import * as THREE from 'three'
import React, { Suspense, useEffect, useRef, useState, useCallback, useLayoutEffect } from 'react'
import { Canvas, useThree, useFrame, useLoader } from 'react-three-fiber'
import { Flex, Box, useFlexSize } from '../../src/index'
import { useAspect } from 'drei/misc/useAspect'
import { Line } from 'drei/abstractions/Line'
import { Loader } from 'drei/prototyping/Loader'
import Effects from './components/Effects'
import Text from './components/Text'
import Geo from './components/Geo'
import state from './state'
function HeightReporter({ onReflow }) {
const size = useFlexSize()
useLayoutEffect(() => onReflow && onReflow(...size), [onReflow, size])
return null
}
function Page({ text, tag, images, textScaleFactor, onReflow, left = false }) {
const textures = useLoader(THREE.TextureLoader, images)
const { viewport } = useThree()
const boxProps = {
centerAnchor: true,
grow: 1,
marginTop: 1,
marginLeft: left * 1,
marginRight: !left * 1,
width: 'auto',
height: 'auto',
minWidth: 3,
minHeight: 3,
maxWidth: 6,
maxHeight: 6,
}
return (
<Box dir="column" align={left ? 'flex-start' : 'flex-end'} justify="flex-start" width="100%" height="auto" minHeight="100%">
<HeightReporter onReflow={onReflow} />
<Box dir="row" width="100%" height="auto" justify={left ? 'flex-end' : 'flex-start'} margin={0} grow={1} wrap="wrap">
{textures.map((texture, index) => (
<Box key={index} {...boxProps}>
{(width, height) => (
<mesh>
<planeBufferGeometry args={[width, height]} />
<meshBasicMaterial map={texture} toneMapped={false} />
</mesh>
)}
</Box>
))}
</Box>
<Box marginLeft={1.5} marginRight={1.5} marginTop={2}>
<Text position={[left ? 1 : -1, 0.5, 1]} fontSize={textScaleFactor} lineHeight={1} letterSpacing={-0.05} maxWidth={(viewport.width / 4) * 3}>
{tag}
<meshBasicMaterial color="#cccccc" toneMapped={false} />
</Text>
</Box>
<Box marginLeft={left ? 1.5 : 1} marginRight={left ? 1 : 1.5} marginBottom={1}>
<Text
bold
position-z={0.5}
textAlign={left ? 'left' : 'right'}
fontSize={1.5 * textScaleFactor}
lineHeight={1}
letterSpacing={-0.05}
color="black"
maxWidth={(viewport.width / 4) * 3}>
{text}
</Text>
</Box>
</Box>
)
}
function Layercard({ depth, boxWidth, boxHeight, text, textColor, color, map, textScaleFactor }) {
const ref = useRef()
const { viewport, size } = useThree()
const pageLerp = useRef(state.top / size.height)
useFrame(() => {
const page = (pageLerp.current = THREE.MathUtils.lerp(pageLerp.current, state.top / size.height, 0.15))
if (depth >= 0) ref.current.opacity = page < state.threshold * 1.7 ? 1 : 1 - (page - state.threshold * 1.7)
})
return (
<>
<mesh position={[boxWidth / 2, -boxHeight / 2, depth]}>
<planeBufferGeometry args={[boxWidth, boxHeight]} />
<meshBasicMaterial ref={ref} color={color} map={map} toneMapped={false} transparent opacity={1} />
</mesh>
<Text
bold
position={[boxWidth / 2, -boxHeight / 2, depth + 1.5]}
maxWidth={(viewport.width / 4) * 1}
anchorX="center"
anchorY="middle"
fontSize={0.6 * textScaleFactor}
lineHeight={1}
letterSpacing={-0.05}
color={textColor}>
{text}
</Text>
</>
)
}
function Content({ onReflow }) {
const group = useRef()
const { viewport, size } = useThree()
const [bW, bH] = useAspect('cover', 1920, 1920, 0.5)
const texture = useLoader(THREE.TextureLoader, state.depthbox[0].image)
const vec = new THREE.Vector3()
const pageLerp = useRef(state.top / size.height)
useFrame(() => {
const page = (pageLerp.current = THREE.MathUtils.lerp(pageLerp.current, state.top / size.height, 0.15))
const y = page * viewport.height
const sticky = state.threshold * viewport.height
group.current.position.lerp(vec.set(0, page < state.threshold ? y : sticky, page < state.threshold ? 0 : page * 1.25), 0.15)
})
const handleReflow = useCallback((w, h) => onReflow((state.pages = h / viewport.height + 5.5)), [onReflow, viewport.height])
const sizesRef = useRef([])
const scale = Math.min(1, viewport.width / 16)
return (
<group ref={group}>
<Flex dir="column" position={[-viewport.width / 2, viewport.height / 2, 0]} size={[viewport.width, viewport.height, 0]} onReflow={handleReflow}>
{state.content.map((props, index) => (
<Page
key={index}
left={!(index % 2)}
textScaleFactor={scale}
onReflow={(w, h) => {
sizesRef.current[index] = h
state.threshold = Math.max(4, (4 / (15.8 * 3)) * sizesRef.current.reduce((acc, e) => acc + e, 0))
}}
{...props}
/>
))}
<Box dir="row" width="100%" height="100%" align="center" justify="center">
<Box centerAnchor>
{state.lines.map((props, index) => (
<Line key={index} {...props} />
))}
<Text
bold
position-z={0.5}
anchorX="center"
anchorY="middle"
fontSize={1.5 * scale}
lineHeight={1}
letterSpacing={-0.05}
color="black"
maxWidth={(viewport.width / 4) * 3}>
{state.depthbox[0].text}
</Text>
</Box>
</Box>
<Box dir="row" width="100%" height="100%" align="center" justify="center">
<Box>
<Layercard {...state.depthbox[0]} text={state.depthbox[1].text} boxWidth={bW} boxHeight={bH} map={texture} textScaleFactor={scale} />
<Geo position={[bW / 2, -bH / 2, state.depthbox[1].depth]} />
</Box>
</Box>
</Flex>
</group>
)
}
export default function App() {
const scrollArea = useRef()
const onScroll = (e) => (state.top = e.target.scrollTop)
useEffect(() => void onScroll({ target: scrollArea.current }), [])
const [pages, setPages] = useState(0)
return (
<>
<Canvas
concurrent
colorManagement
shadowMap
noEvents
pixelRatio={2}
camera={{ position: [0, 0, 10], far: 1000 }}
gl={{ powerPreference: 'high-performance', alpha: false, antialias: false, stencil: false, depth: false }}
onCreated={({ gl }) => gl.setClearColor('#f5f5f5')}>
<pointLight position={[-10, -10, -10]} intensity={1} />
<ambientLight intensity={0.4} />
<spotLight
castShadow
angle={0.3}
penumbra={1}
position={[0, 10, 20]}
intensity={5}
shadow-mapSize-width={1024}
shadow-mapSize-height={1024}
/>
<Suspense fallback={null}>
<Content onReflow={setPages} />
</Suspense>
<Effects />
</Canvas>
<div
className="scrollArea"
ref={scrollArea}
onScroll={onScroll}
onPointerMove={(e) => (state.mouse = [(e.clientX / window.innerWidth) * 2 - 1, (e.clientY / window.innerHeight) * 2 - 1])}>
<div style={{ height: `${pages * 100}vh` }} />
</div>
<Loader />
</>
)
}