-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.tsx
155 lines (141 loc) · 5.25 KB
/
App.tsx
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
import React, { useState } from 'react';
import ParticleImage, { forces, Array2D, RGBA, ParticleOptions, Vector } from 'react-particle-image'
import ReactResizeDetector from 'react-resize-detector';
import Sidebar from './Sidebar'
import Hamburger from './Hamburger'
import ArrowButton from './ArrowButton'
import Controls, { Values as ControlsValues} from './Controls'
import { pad } from './utils'
import styles from './App.module.css'
const REACT_LOGO_URL = 'logo512.png'
const COFFEE_URL = 'coffee.png'
const COLOR_WHEEL_URL = 'color-wheel.jpeg'
const defaultScale = Math.min(Math.max(window.innerWidth / 1300, .5), .85)
const sources = [REACT_LOGO_URL, COFFEE_URL, COLOR_WHEEL_URL]
interface ParticleOptionParams {
x: number;
y: number;
image: Array2D<RGBA>;
}
type ParticleOptionsMap = {
[key: string]: ParticleOptions
}
const particleOptionsMap: ParticleOptionsMap = {
[REACT_LOGO_URL]: {
radius: () => (Math.random()*1.4 + .5) * defaultScale,
mass: () => 40,
filter: ({x, y, image}: ParticleOptionParams) => {
const pixel = image.get(x, y)
return pixel.b > 50
},
color: () => '#61dafb',
friction: () => .15,
initialPosition: ({canvasDimensions}) => {
return new Vector(canvasDimensions.width / 2, canvasDimensions.height / 2)
}
},
[COFFEE_URL]: {
radius: () => (Math.random()*1.4 + .5) * defaultScale,
mass: () => 80,
filter: ({x, y, image}: ParticleOptionParams) => {
const pixel = image.get(x, y)
const magnitude = (pixel.r + pixel.g + pixel.b) / 3 / 255 * pixel.a / 255
return magnitude < .9
},
color: ({x, y, image}: ParticleOptionParams) => {
return 'white'
},
friction: () => .15,
initialPosition: ({canvasDimensions}) => {
return new Vector(canvasDimensions.width / 2, canvasDimensions.height / 2)
}
},
[COLOR_WHEEL_URL]: {
radius: ({x, y, image}: ParticleOptionParams) => {
const center = new Vector(image.getWidth() / 2, image.getHeight() / 2)
return (center.subtract(new Vector(x, y)).getMagnitude() / 200 + .1) * defaultScale
},
mass: () => 50,
filter: ({x, y, image}: ParticleOptionParams) => {
const pixel = image.get(x, y)
const magnitude = (pixel.r + pixel.g + pixel.b) / 3 / 255 * pixel.a / 255
return magnitude < .8
},
color: ({x, y, image}: ParticleOptionParams) => {
return `orange`
},
friction: () => .23,
initialVelocity: ({image}) => new Vector((Math.random() - .5)*1000, (Math.random() - 1)*1000)
}
}
type Dimensions = {
width: number,
height: number
}
const App: React.FC = () => {
const [srcIndex, setSrcIndex] = useState(0)
const [sidebarOpen, setSidebarOpen] = useState(false)
const [params, setParams] = useState<ControlsValues>({entropy: 20, scale: defaultScale, numParticles: 4500})
const setParam = (key: string, val: any) => {
setParams(prev => ({...prev, [key]: val}))
}
const next = () => {
setSrcIndex(prev => (prev + 1) % sources.length)
}
const prev = () => {
setSrcIndex(prev => {
if (prev === 0) {
return sources.length - 1
}
else {
return (prev - 1) % sources.length
}
})
}
const src: string = sources[srcIndex]
return (
<div className={styles.content} onScroll={e => {e.stopPropagation(); e.preventDefault()}}>
<div className={styles.header}>
<p className={styles.title}>PARTICLE IMAGE</p>
<Hamburger className={styles.hamburger} onClick={() => setSidebarOpen(prev => !prev)} open={sidebarOpen}/>
</div>
<Sidebar open={sidebarOpen}>
<div className={styles.sidebar}>
<Controls values={params} onChange={setParam}/>
</div>
</Sidebar>
<ReactResizeDetector handleWidth handleHeight>
{({ width, height }: Dimensions) => {
if (width && height) {
return (
<ParticleImage
key={`${params.numParticles}`}
backgroundColor="rgb(31, 31, 31)"
src={src}
maxParticles={params.numParticles}
height={height}
width={width}
particleOptions={particleOptionsMap[src]}
scale={params.scale}
entropy={params.entropy}
mouseMoveForce={(x: number, y: number) => forces.disturbance(x, y, 6)}
touchMoveForce={(x: number, y: number) => forces.disturbance(x, y, 6)}
mouseDownForce={(x: number, y: number) => forces.disturbance(x, y, 50)}
/>
)
}
return <div />
}}
</ReactResizeDetector>
<div className={styles.footer}>
<p className={styles.imgInfo}>{pad(sources.indexOf(src) + 1, 2)} / {pad(sources.length, 2)}</p>
<div className={styles.nav}>
<ArrowButton onClick={prev} className={styles.navArrow} arrowPlacement="left" arrowDirection="left" text="PREV" />
<ArrowButton onClick={next} className={styles.navArrow} arrowPlacement="right" arrowDirection="right" text="NEXT" />
</div>
<a href="https://github.com/malerba118/react-particle-image" target="_blank" rel="noopener noreferrer" className={styles.links}>GITHUB</a>
</div>
</div>
);
}
export default App;