-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCozyAppPage.jsx
More file actions
427 lines (398 loc) · 14.8 KB
/
CozyAppPage.jsx
File metadata and controls
427 lines (398 loc) · 14.8 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import React, { useState, useEffect, useRef } from 'react';
import { Link } from 'react-router-dom';
import {
ArrowLeftIcon,
CloudRainIcon,
CloudSnowIcon,
FireIcon,
SpeakerHighIcon,
SpeakerSlashIcon,
WindIcon,
} from '@phosphor-icons/react';
import Seo from '../../components/Seo';
const CozyAppPage = () => {
const [mode, setMode] = useState('fireplace'); // fireplace, breathe, snow, rain
const [isMuted, setIsMuted] = useState(true);
const audioCtxRef = useRef(null);
const gainNodeRef = useRef(null);
const sourceNodeRef = useRef(null);
const canvasRef = useRef(null);
const isMouseDownRef = useRef(false); // Ref to hold the current state of isMouseDown
const particlesRef = useRef([]); // New: persistent storage for particles
class Particle {
// Move Particle class definition here
constructor(mode, canvas) {
this.mode = mode;
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.reset();
}
reset() {
if (this.mode === 'fireplace') {
const spread = this.canvas.width * 0.5;
this.x = this.canvas.width / 2 + (Math.random() - 0.5) * spread;
this.y = this.canvas.height;
this.size = Math.random() * 10 + 5;
this.speedY = Math.random() * 5 + 2;
this.speedX = (Math.random() - 0.5) * 2;
this.color = `hsla(${Math.random() * 40 + 10}, 100%, 50%, ${Math.random() * 0.5 + 0.1})`;
this.life = 150;
this.decay = Math.random() * 0.5 + 0.2;
} else if (this.mode === 'snow') {
this.x = Math.random() * this.canvas.width;
this.y = Math.random() * this.canvas.height * -1; // Start above canvas
this.size = Math.random() * 3 + 1;
this.speedY = Math.random() * 1 + 0.5;
this.speedX = (Math.random() - 0.5) * 0.5; // Gentle drift
this.color = `hsla(0, 0%, 100%, ${Math.random() * 0.5 + 0.3})`;
} else if (this.mode === 'rain') {
this.x = Math.random() * this.canvas.width;
this.y = Math.random() * this.canvas.height * -1;
this.size = Math.random() * 20 + 10; // Length of rain drop
this.speedY = Math.random() * 10 + 15; // Fast
this.speedX = 0;
this.color = `hsla(210, 100%, 70%, ${Math.random() * 0.3 + 0.1})`;
}
}
update() {
this.x += this.speedX;
this.y += (this.mode === 'fireplace' ? -1 : 1) * this.speedY; // Fire goes up, rain/snow goes down
if (this.mode === 'fireplace') {
this.size -= 0.1;
this.life -= this.decay;
if (this.size <= 0 || this.life <= 0) this.reset();
} else {
// Wrap around for snow/rain
if (this.y > this.canvas.height) {
this.y = -10;
this.x = Math.random() * this.canvas.width;
}
if (this.x > this.canvas.width) this.x = 0;
if (this.x < 0) this.x = this.canvas.width;
}
}
draw() {
this.ctx.beginPath();
if (this.mode === 'rain') {
this.ctx.moveTo(this.x, this.y);
this.ctx.lineTo(this.x, this.y + this.size);
this.ctx.strokeStyle = this.color;
this.ctx.lineWidth = 1;
this.ctx.stroke();
} else {
this.ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
this.ctx.fillStyle = this.color;
this.ctx.fill();
}
}
}
// --- Audio Engine ---
useEffect(() => {
const cleanupAudio = () => {
if (sourceNodeRef.current) {
sourceNodeRef.current.stop();
sourceNodeRef.current.disconnect();
sourceNodeRef.current = null;
}
if (gainNodeRef.current) {
gainNodeRef.current.disconnect();
}
if (audioCtxRef.current) {
audioCtxRef.current.close();
audioCtxRef.current = null;
}
};
if (isMuted || mode === 'breathe') {
cleanupAudio();
return;
}
// Initialize Audio Context
const AudioContext = window.AudioContext || window.webkitAudioContext;
if (!audioCtxRef.current) {
audioCtxRef.current = new AudioContext();
}
const ctx = audioCtxRef.current;
// Create Master Gain (Volume)
gainNodeRef.current = ctx.createGain();
gainNodeRef.current.connect(ctx.destination);
gainNodeRef.current.gain.value = 0.15; // Master volume
// Generate Noise Buffer (Pink-ish Noise for nature sounds)
const bufferSize = 2 * ctx.sampleRate;
const noiseBuffer = ctx.createBuffer(1, bufferSize, ctx.sampleRate);
const output = noiseBuffer.getChannelData(0);
let lastOut = 0;
for (let i = 0; i < bufferSize; i++) {
const white = Math.random() * 2 - 1;
output[i] = (lastOut + 0.02 * white) / 1.02;
lastOut = output[i];
output[i] *= 3.5;
}
// Source setup
sourceNodeRef.current = ctx.createBufferSource();
sourceNodeRef.current.buffer = noiseBuffer;
sourceNodeRef.current.loop = true;
// Filter setup based on mode
const filter = ctx.createBiquadFilter();
sourceNodeRef.current.connect(filter);
filter.connect(gainNodeRef.current);
if (mode === 'fireplace') {
filter.type = 'lowpass';
filter.frequency.value = 400;
gainNodeRef.current.gain.value = 0.15;
// Simulate crackling? (Too complex for this simple setup, noise provides the "roar")
} else if (mode === 'rain') {
filter.type = 'lowpass';
filter.frequency.value = 800;
gainNodeRef.current.gain.value = 0.15;
} else if (mode === 'snow') {
filter.type = 'lowpass';
filter.frequency.value = 200; // Deep, soft wind rumble
gainNodeRef.current.gain.value = 0.05; // Very quiet and soothing
}
sourceNodeRef.current.start();
return cleanupAudio;
}, [mode, isMuted]);
// --- Particle Engine ---
useEffect(() => {
if (mode === 'breathe') {
particlesRef.current = []; // Clear particles if switching to breathe mode
return;
}
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext('2d');
let animationFrameId;
// Set canvas size
const resizeCanvas = () => {
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
};
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
let baseParticleCount = 100;
// Config based on mode
if (mode === 'fireplace') baseParticleCount = 300;
if (mode === 'snow') baseParticleCount = 200;
if (mode === 'rain') baseParticleCount = 500;
// When mode changes, reset existing particles to adapt to the new mode
particlesRef.current.forEach((p) => {
p.mode = mode; // Update mode reference
p.canvas = canvas; // Update canvas reference
p.ctx = ctx; // Update ctx reference
p.reset();
});
const animate = () => {
// Calculate targetParticleCount inside animate, to react to isMouseDown changes
const targetParticleCount = isMouseDownRef.current
? baseParticleCount * 5
: baseParticleCount;
// Add or remove particles dynamically per frame
if (particlesRef.current.length < targetParticleCount) {
// Add a few particles per frame to smoothly increase
for (
let i = 0;
i < Math.min(15, targetParticleCount - particlesRef.current.length);
i++
) {
particlesRef.current.push(new Particle(mode, canvas));
}
} else if (particlesRef.current.length > targetParticleCount) {
// Remove a few particles per frame to smoothly decrease
for (
let i = 0;
i < Math.min(15, particlesRef.current.length - targetParticleCount);
i++
) {
particlesRef.current.pop();
}
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (mode === 'fireplace') {
// Draw log
ctx.fillStyle = '#3E2723';
ctx.fillRect(
canvas.width * 0.2,
canvas.height - 20,
canvas.width * 0.6,
20,
);
ctx.globalCompositeOperation = 'screen';
} else {
ctx.globalCompositeOperation = 'source-over';
}
particlesRef.current.forEach((p) => {
p.update();
p.draw();
});
// Reset composite operation
ctx.globalCompositeOperation = 'source-over';
animationFrameId = requestAnimationFrame(animate);
};
// Initial fill if particlesRef.current is empty when mode starts
if (particlesRef.current.length === 0) {
for (let i = 0; i < baseParticleCount; i++) {
particlesRef.current.push(new Particle(mode, canvas));
}
}
animate();
return () => {
window.removeEventListener('resize', resizeCanvas);
cancelAnimationFrame(animationFrameId);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mode]); // isMouseDown removed from dependencies
const handleMouseDownIntensify = () => {
if (mode === 'breathe') return;
isMouseDownRef.current = true; // Update ref directly
};
const handleMouseUpIntensify = () => {
isMouseDownRef.current = false; // Update ref directly
};
return (
<div className="min-h-screen bg-gray-900 text-gray-100 flex flex-col transition-colors duration-500">
<Seo
title="Cozy Corner | Fezcodex"
description="A place to relax, watch a fire, snow, rain, or just breathe."
keywords={[
'cozy',
'relax',
'fire',
'snow',
'rain',
'breathe',
'meditation',
]}
/>
<div className="container mx-auto px-4 py-8 flex-grow flex flex-col relative z-10 max-w-4xl">
{/* Header */}
<div className="flex items-center mb-8 justify-between">
<Link
to="/apps"
className="flex items-center gap-2 text-gray-400 hover:text-white transition-colors"
>
<ArrowLeftIcon /> Back to Apps
</Link>
<h1 className="text-3xl font-playfairDisplay font-bold tracking-wide text-amber-500">
Cozy Corner
</h1>
<div className="flex gap-2">
{/* Sound Toggle */}
<button
onClick={() => setIsMuted(!isMuted)}
className={`p-2 rounded-full transition-colors mr-2 ${!isMuted ? 'bg-green-900/50 text-green-400' : 'text-gray-500 hover:bg-gray-800'}`}
title={isMuted ? 'Unmute Sound' : 'Mute Sound'}
>
{isMuted ? (
<SpeakerSlashIcon size={24} />
) : (
<SpeakerHighIcon size={24} weight="fill" />
)}
</button>
{/* Mode Toggles */}
<button
onClick={() => setMode('fireplace')}
className={`p-2 rounded-full transition-colors ${mode === 'fireplace' ? 'bg-amber-900/50 text-amber-400' : 'text-gray-500 hover:bg-gray-800'}`}
title="Fireplace"
>
<FireIcon
size={24}
weight={mode === 'fireplace' ? 'fill' : 'regular'}
/>
</button>
<button
onClick={() => setMode('snow')}
className={`p-2 rounded-full transition-colors ${mode === 'snow' ? 'bg-blue-900/50 text-blue-200' : 'text-gray-500 hover:bg-gray-800'}`}
title="Snow"
>
<CloudSnowIcon
size={24}
weight={mode === 'snow' ? 'fill' : 'regular'}
/>
</button>
<button
onClick={() => setMode('rain')}
className={`p-2 rounded-full transition-colors ${mode === 'rain' ? 'bg-blue-900/50 text-blue-400' : 'text-gray-500 hover:bg-gray-800'}`}
title="Rain"
>
<CloudRainIcon
size={24}
weight={mode === 'rain' ? 'fill' : 'regular'}
/>
</button>
<button
onClick={() => setMode('breathe')}
className={`p-2 rounded-full transition-colors ${mode === 'breathe' ? 'bg-cyan-900/50 text-cyan-400' : 'text-gray-500 hover:bg-gray-800'}`}
title="Breathe"
>
<WindIcon
size={24}
weight={mode === 'breathe' ? 'fill' : 'regular'}
/>
</button>
</div>
</div>
{/* Content Area */}
{/*<div className="w-[830px] h-[800px] relative bg-gray-800 rounded-2xl overflow-hidden shadow-2xl border border-gray-700">*/}
<div
className="flex-grow h-[80vh] relative bg-gray-800 rounded-2xl overflow-hidden shadow-2xl border border-gray-700"
onMouseDown={handleMouseDownIntensify} // Use onMouseDown
onMouseUp={handleMouseUpIntensify} // Use onMouseUp
onMouseLeave={handleMouseUpIntensify} // Reset if mouse leaves while down
>
{mode !== 'breathe' && (
<div className="absolute inset-0 flex flex-col items-center justify-end pb-10">
<canvas
ref={canvasRef}
className="absolute inset-0 w-full h-full"
/>
{mode !== 'breathe' && (
<>
{!isMouseDownRef.current && (
<div className="relative z-10 text-gray-300/35 font-mono text-xs select-none pointer-events-none mb-2">
...hold...
</div>
)}
<div className="relative z-10 text-amber-200/50 font-mono text-sm select-none pointer-events-none mb-4">
{mode === 'fireplace' && 'Warmth & comfort'}
{mode === 'snow' && 'Silent snow'}
{mode === 'rain' && 'Gentle rain'}
</div>
</>
)}
</div>
)}
{mode === 'breathe' && (
<div className="w-full h-full flex flex-col items-center justify-center">
<div className="w-48 h-48 bg-cyan-500/20 rounded-full flex items-center justify-center animate-breathe relative">
<div className="w-32 h-32 bg-cyan-400/30 rounded-full blur-md absolute animate-pulse-slow"></div>
<span className="text-cyan-100 font-playfairDisplay text-xl z-10">
Breathe
</span>
</div>
<p className="mt-8 text-cyan-200/60 font-mono text-sm">
Inhale... Exhale...
</p>
</div>
)}
</div>
</div>
<style>{`
@keyframes breathe {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.5); }
}
.animate-breathe {
animation: breathe 8s ease-in-out infinite;
}
@keyframes pulse-slow {
0%, 100% { opacity: 0.5; }
50% { opacity: 0.8; }
}
.animate-pulse-slow {
animation: pulse-slow 4s ease-in-out infinite;
}
`}</style>
</div>
);
};
export default CozyAppPage;