-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractWavesPage.jsx
More file actions
282 lines (258 loc) · 8.95 KB
/
AbstractWavesPage.jsx
File metadata and controls
282 lines (258 loc) · 8.95 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import React, { useState, useEffect, useRef, useCallback } from 'react';
import { Link } from 'react-router-dom';
import {
ArrowLeftIcon,
DownloadSimple,
ArrowsClockwise,
} from '@phosphor-icons/react';
import Seo from '../../components/Seo';
import { useToast } from '../../hooks/useToast';
import BreadcrumbTitle from '../../components/BreadcrumbTitle';
import CustomSlider from '../../components/CustomSlider';
const AbstractWavesPage = () => {
const { addToast } = useToast();
const canvasRef = useRef(null);
// --- Parameters ---
const [lineCount, setLineCount] = useState(50);
const [amplitude, setAmplitude] = useState(50);
const [frequency, setFrequency] = useState(0.02);
const [perspective, setPerspective] = useState(10); // Spacing
const [noise, setNoise] = useState(10); // Random offset
const [phase, setPhase] = useState(0);
const [lineWidth, setLineWidth] = useState(2);
const [fill, setFill] = useState(true); // Fill below line to hide lines behind
const [inverted, setInverted] = useState(false);
const drawWaves = useCallback(
(ctx, w, h) => {
const bgColor = inverted ? '#000000' : '#ffffff';
const lineColor = inverted ? '#ffffff' : '#000000';
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, w, h);
ctx.lineWidth = lineWidth;
ctx.strokeStyle = lineColor;
ctx.fillStyle = bgColor; // For hiding lines behind
// Center vertically roughly
const totalHeight = lineCount * perspective;
const startY = (h - totalHeight) / 2;
for (let i = 0; i < lineCount; i++) {
const yBase = startY + i * perspective;
ctx.beginPath();
for (let x = 0; x <= w; x += 5) {
// Step size 5 for performance
// Create a complex wave by combining sine waves and noise
const distFromCenter = Math.abs(x - w / 2);
const dampener = Math.max(0, 1 - distFromCenter / (w / 2)); // 1 at center, 0 at edges
// Main wave
let y =
yBase +
Math.sin(x * frequency + phase + i * 0.1) * amplitude * dampener;
// Add some "noise" or irregularity
y += Math.cos(x * frequency * 2.5 + i * 0.5) * (noise * dampener);
if (x === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
if (fill) {
// Close the path for filling to hide lines behind
ctx.lineTo(w, w); // Down to bottom right (roughly) - actually just needs to be low enough
ctx.lineTo(0, w); // Back to bottom left
ctx.closePath();
ctx.fill();
// Re-stroke the top line
ctx.stroke();
} else {
ctx.stroke();
}
}
},
[
lineCount,
amplitude,
frequency,
perspective,
noise,
phase,
lineWidth,
fill,
inverted,
],
);
// Effect to redraw
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
let rafId = requestAnimationFrame(() => drawWaves(ctx, width, height));
return () => cancelAnimationFrame(rafId);
}, [drawWaves]);
// Init Canvas Size
useEffect(() => {
const canvas = canvasRef.current;
if (canvas) {
canvas.width = 1200;
canvas.height = 800;
}
}, []);
const handleDownload = () => {
const canvas = canvasRef.current;
const link = document.createElement('a');
link.download = `abstract_waves_${Date.now()}.png`;
link.href = canvas.toDataURL();
link.click();
addToast({
title: 'Saved',
message: 'Wave pattern saved to device.',
duration: 3000,
});
};
const randomizeParams = () => {
setLineCount(30 + Math.floor(Math.random() * 50));
setAmplitude(20 + Math.random() * 80);
setFrequency(0.005 + Math.random() * 0.04);
setPerspective(5 + Math.random() * 20);
setNoise(Math.random() * 40);
setPhase(Math.random() * Math.PI * 2);
setInverted(Math.random() > 0.5);
};
return (
<div className="min-h-screen bg-gray-50 flex flex-col">
<Seo
title="Abstract Waves | Fezcodex"
description="Generate mesmerizing black and white abstract wave patterns."
keywords={[
'waves',
'generative art',
'abstract',
'black and white',
'canvas',
]}
/>
<div className="container mx-auto px-4 py-8 flex-grow flex flex-col max-w-6xl">
<Link
to="/apps"
className="group text-primary-400 hover:underline flex items-center justify-center gap-2 text-lg mb-4"
>
<ArrowLeftIcon className="text-xl transition-transform group-hover:-translate-x-1" />{' '}
Back to Apps
</Link>
<BreadcrumbTitle title="Abstract Waves" slug="aw" />
<div className="grid grid-cols-1 lg:grid-cols-3 gap-8 flex-grow">
{/* Controls */}
<div className="space-y-6 bg-white p-6 rounded-xl shadow-md border border-gray-200 h-fit">
<div className="flex gap-2 mb-4">
<button
onClick={randomizeParams}
className="flex-1 py-2 px-4 bg-gray-800 text-white rounded-lg hover:bg-gray-900 flex items-center justify-center gap-2 transition-colors"
>
<ArrowsClockwise size={20} /> Randomize
</button>
<button
onClick={handleDownload}
className="px-4 py-2 bg-gray-100 text-gray-600 rounded-lg hover:bg-gray-200"
title="Download"
>
<DownloadSimple size={24} />
</button>
</div>
{/* Toggles */}
<div className="flex gap-4">
<label className="flex items-center gap-2 cursor-pointer">
<input
type="checkbox"
checked={inverted}
onChange={(e) => setInverted(e.target.checked)}
className="w-4 h-4 accent-black"
/>
<span className="text-sm font-medium text-gray-700">
Invert Colors
</span>
</label>
<label className="flex items-center gap-2 cursor-pointer">
<input
type="checkbox"
checked={fill}
onChange={(e) => setFill(e.target.checked)}
className="w-4 h-4 accent-black"
/>
<span className="text-sm font-medium text-gray-700">
Opaque Lines
</span>
</label>
</div>
<div className="space-y-4">
<CustomSlider
label="Line Count"
min={10}
max={100}
value={lineCount}
onChange={setLineCount}
/>
<CustomSlider
label="Amplitude (Height)"
min={0}
max={150}
value={Math.round(amplitude)}
onChange={setAmplitude}
/>
<CustomSlider
label="Frequency (Width)"
min={0.001}
max={0.05}
step={0.001}
value={frequency}
onChange={setFrequency}
/>
<CustomSlider
label="Spacing"
min={2}
max={40}
value={perspective}
onChange={setPerspective}
/>
<CustomSlider
label="Noise / Distortion"
min={0}
max={100}
value={Math.round(noise)}
onChange={setNoise}
/>
<CustomSlider
label="Phase Shift"
min={0}
max={10}
step={0.1}
value={phase}
onChange={setPhase}
/>
<CustomSlider
label="Line Width"
min={1}
max={10}
value={lineWidth}
onChange={setLineWidth}
/>
</div>
<div className="bg-gray-100 p-3 rounded-lg text-xs text-gray-600">
<strong>Tip:</strong> "Opaque Lines" hides the waves behind the
current one, creating a 3D landscape effect similar to the famous
"Unknown Pleasures" album cover.
</div>
</div>
{/* Canvas */}
<div className="lg:col-span-2 bg-white rounded-xl shadow-lg border border-gray-200 p-4 flex items-center justify-center overflow-hidden">
<canvas
ref={canvasRef}
className="max-w-full h-auto rounded-lg shadow-inner border border-gray-100"
style={{ maxHeight: '600px' }}
/>
</div>
</div>
</div>
</div>
);
};
export default AbstractWavesPage;