-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpirographPage.jsx
More file actions
351 lines (323 loc) · 11.9 KB
/
SpirographPage.jsx
File metadata and controls
351 lines (323 loc) · 11.9 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
import React, { useState, useEffect, useRef, useCallback } from 'react';
import { Link } from 'react-router-dom';
import {
ArrowLeftIcon, // Added ArrowLeftIcon
DownloadSimple,
Eraser,
Play,
Pause,
} from '@phosphor-icons/react';
import Seo from '../../components/Seo';
import { useToast } from '../../hooks/useToast';
import BreadcrumbTitle from '../../components/BreadcrumbTitle';
const SpirographPage = () => {
const { addToast } = useToast();
const canvasRef = useRef(null);
// Parameters
const [outerRadius, setOuterRadius] = useState(150); // R
const [innerRadius, setInnerRadius] = useState(52); // r
const [penOffset, setPenOffset] = useState(50); // d
const [resolution, setResolution] = useState(0.1); // t step
const [speed, setSpeed] = useState(5);
const [color, setColor] = useState('#ec4899'); // Default pink-ish
const [isRainbow, setIsRainbow] = useState(false);
// State
const [isDrawing, setIsDrawing] = useState(false);
const [angle, setAngle] = useState(0);
// Refs for animation loop
const requestRef = useRef();
// Draw helper
const draw = useCallback(
(ctx, currentAngle) => {
const width = ctx.canvas.width;
const height = ctx.canvas.height;
const centerX = width / 2;
const centerY = height / 2;
const R = outerRadius;
const r = innerRadius;
const d = penOffset;
// Current point
const x =
(R - r) * Math.cos(currentAngle) +
d * Math.cos(((R - r) / r) * currentAngle);
const y =
(R - r) * Math.sin(currentAngle) -
d * Math.sin(((R - r) / r) * currentAngle);
// Previous point (to draw line)
// We approximate prev point by subtracting resolution.
// Ideally we store the last point, but for high res this is okayish,
// or we can use moveTo/lineTo in a path.
// Better approach for continuous drawing:
ctx.beginPath();
// Calculate a slightly previous point to connect to
const prevAngle = currentAngle - resolution;
const prevX =
(R - r) * Math.cos(prevAngle) + d * Math.cos(((R - r) / r) * prevAngle);
const prevY =
(R - r) * Math.sin(prevAngle) - d * Math.sin(((R - r) / r) * prevAngle);
ctx.moveTo(centerX + prevX, centerY + prevY);
ctx.lineTo(centerX + x, centerY + y);
ctx.strokeStyle = isRainbow
? `hsl(${(currentAngle * 10) % 360}, 70%, 50%)`
: color;
ctx.lineWidth = 1;
ctx.stroke();
},
[outerRadius, innerRadius, penOffset, resolution, isRainbow, color],
);
const animate = useCallback(() => {
if (!isDrawing) return;
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext('2d');
// Draw multiple steps per frame based on speed
let currentAngle = angle;
for (let i = 0; i < speed; i++) {
currentAngle += resolution;
draw(ctx, currentAngle);
}
setAngle(currentAngle);
// Stop if we've done a lot of cycles?
// For now, let it run infinitely or until user stops.
// Real spirographs close loops, but with floats it might not perfectly align.
// A huge number of rotations usually fills the circle.
if (currentAngle > 600 * Math.PI) {
setIsDrawing(false);
addToast({
title: 'Finished',
message: 'Autostop after 300 cycles',
duration: 2000,
});
} else {
requestRef.current = requestAnimationFrame(animate);
}
}, [isDrawing, angle, speed, resolution, draw, addToast]);
useEffect(() => {
if (isDrawing) {
requestRef.current = requestAnimationFrame(animate);
} else {
cancelAnimationFrame(requestRef.current);
}
return () => cancelAnimationFrame(requestRef.current);
}, [isDrawing, animate]);
// Init canvas
useEffect(() => {
const canvas = canvasRef.current;
if (canvas) {
canvas.width = 800;
canvas.height = 800;
// Optional: Clear on mount or keep? keep blank
}
}, []);
const handleClear = () => {
const canvas = canvasRef.current;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
setAngle(0);
setIsDrawing(false);
};
const handleDownload = () => {
const canvas = canvasRef.current;
const link = document.createElement('a');
link.download = 'spirograph.png';
link.href = canvas.toDataURL();
link.click();
addToast({
title: 'Saved',
message: 'Image downloaded successfully.',
duration: 3000,
});
};
return (
<div className="min-h-screen bg-gray-50 flex flex-col">
<Seo
title="Spirograph Generator | Fezcodex"
description="Create mesmerizing geometric patterns with math."
keywords={['spirograph', 'geometry', 'art', 'generator', 'math art']}
/>
<div className="container mx-auto px-4 py-8 flex-grow flex flex-col max-w-6xl">
{/* Header */}
<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="Spirograph Generator" slug="spiro" />
<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={() => setIsDrawing(!isDrawing)}
className={`flex-1 py-2 px-4 rounded-lg font-medium flex items-center justify-center gap-2 transition-colors ${
isDrawing
? 'bg-yellow-100 text-yellow-700 hover:bg-yellow-200'
: 'bg-green-600 text-white hover:bg-green-700'
}`}
>
{isDrawing ? (
<>
<Pause /> Pause
</>
) : (
<>
<Play /> Draw
</>
)}
</button>
<button
onClick={handleClear}
className="px-4 py-2 bg-gray-100 text-gray-600 rounded-lg hover:bg-gray-200 flex items-center justify-center"
title="Clear Canvas"
>
<Eraser size={24} />
</button>
<button
onClick={handleDownload}
className="px-4 py-2 bg-gray-100 text-gray-600 rounded-lg hover:bg-gray-200 flex items-center justify-center"
title="Download Image"
>
<DownloadSimple size={24} />
</button>
</div>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Outer Radius (R): {outerRadius}
</label>
<input
type="range"
min="10"
max="300"
value={outerRadius}
onChange={(e) => {
setIsDrawing(false);
setOuterRadius(Number(e.target.value));
}}
className="w-full"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Inner Radius (r): {innerRadius}
</label>
<input
type="range"
min="2"
max="200"
value={innerRadius}
onChange={(e) => {
setIsDrawing(false);
setInnerRadius(Number(e.target.value));
}}
className="w-full"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Pen Offset (d): {penOffset}
</label>
<input
type="range"
min="2"
max="200"
value={penOffset}
onChange={(e) => {
setIsDrawing(false);
setPenOffset(Number(e.target.value));
}}
className="w-full"
/>
</div>
<div className="pt-4 border-t border-gray-100">
<label className="block text-sm font-medium text-gray-700 mb-1">
Drawing Speed: {speed}
</label>
<input
type="range"
min="1"
max="50"
value={speed}
onChange={(e) => setSpeed(Number(e.target.value))}
className="w-full accent-purple-600"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Resolution (Quality): {resolution}
</label>
<input
type="range"
min="0.01"
max="0.5"
step="0.01"
value={resolution}
onChange={(e) => setResolution(Number(e.target.value))}
className="w-full accent-blue-600"
/>
<p className="text-xs text-gray-500 mt-1">
Lower is smoother but slower.
</p>
</div>
</div>
<div className="pt-4 border-t border-gray-100">
<div className="flex items-center justify-between mb-2">
<label className="text-sm font-medium text-gray-700">
Pen Color
</label>
<div className="flex items-center gap-2">
<span className="text-xs text-gray-500">Rainbow</span>
<input
type="checkbox"
checked={isRainbow}
onChange={(e) => setIsRainbow(e.target.checked)}
className="rounded text-pink-500 focus:ring-pink-500"
/>
</div>
</div>
<div className="flex gap-2 flex-wrap">
{[
'#ec4899',
'#8b5cf6',
'#3b82f6',
'#10b981',
'#f59e0b',
'#ef4444',
'#111827',
].map((c) => (
<button
key={c}
onClick={() => setColor(c)}
className={`w-8 h-8 rounded-full border-2 ${color === c && !isRainbow ? 'border-gray-900 scale-110' : 'border-transparent hover:scale-105'}`}
style={{ backgroundColor: c }}
/>
))}
<input
type="color"
value={color}
onChange={(e) => setColor(e.target.value)}
className="w-8 h-8 p-0 border-0 rounded-full overflow-hidden cursor-pointer"
/>
</div>
</div>
<div className="bg-blue-50 p-3 rounded-lg text-xs text-blue-800">
<strong>Tip:</strong> Change R, r, or d slightly while drawing to
create evolving patterns!
</div>
</div>
{/* Canvas Area */}
<div className="lg:col-span-2 w-[820px] h-[820px] bg-white rounded-xl shadow-lg border border-gray-200 p-4 flex items-center justify-center overflow-hidden">
<canvas
ref={canvasRef}
className="bg-white" // Removed responsive sizing as parent is fixed
style={{ imageRendering: 'pixelated' }}
/>
</div>
</div>
</div>
</div>
);
};
export default SpirographPage;