-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
144 lines (114 loc) · 4.29 KB
/
App.tsx
File metadata and controls
144 lines (114 loc) · 4.29 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
import React, { useState, useEffect, useRef, useCallback } from 'react';
import { DEFAULT_CONFIG, ArtConfig } from './types';
import { Controls } from './components/Controls';
import { drawComposition } from './services/artEngine';
function App() {
const [config, setConfig] = useState<ArtConfig>(DEFAULT_CONFIG);
const canvasRef = useRef<HTMLCanvasElement>(null);
const containerRef = useRef<HTMLDivElement>(null);
// To handle window resizing debouncing
const [containerSize, setContainerSize] = useState({ width: 800, height: 600 });
// Calculate canvas dimensions based on container and aspect ratio
const getCanvasDimensions = useCallback(() => {
if (!containerSize.width) return { width: 800, height: 800 };
const { width: maxW, height: maxH } = containerSize;
const padding = 40;
const availW = maxW - padding * 2;
const availH = maxH - padding * 2;
let targetW, targetH;
const [rw, rh] = config.aspectRatio.split(':').map(Number);
const ratio = rw / rh;
if (availW / ratio <= availH) {
targetW = availW;
targetH = availW / ratio;
} else {
targetH = availH;
targetW = availH * ratio;
}
return { width: targetW, height: targetH };
}, [containerSize, config.aspectRatio]);
// Handle resize observer
useEffect(() => {
if (!containerRef.current) return;
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
setContainerSize({
width: entry.contentRect.width,
height: entry.contentRect.height
});
}
});
resizeObserver.observe(containerRef.current);
return () => resizeObserver.disconnect();
}, []);
// Main Draw Loop
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const { width, height } = getCanvasDimensions();
// Handle High DPI displays
const dpr = window.devicePixelRatio || 1;
canvas.width = width * dpr;
canvas.height = height * dpr;
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
const ctx = canvas.getContext('2d');
if (!ctx) return;
ctx.scale(dpr, dpr);
// Draw
try {
drawComposition(ctx, width, height, config);
} catch (error) {
console.error('Error drawing composition:', error);
}
}, [config, containerSize, getCanvasDimensions]);
const handleGenerate = () => {
// Just changing seed triggers regen
setConfig(prev => ({ ...prev, seed: Math.random().toString(36).substring(7) }));
};
const handleDownload = () => {
// Create an offscreen canvas for high-res export (3000px on shortest side approx)
const exportCanvas = document.createElement('canvas');
const [rw, rh] = config.aspectRatio.split(':').map(Number);
// Base size 3000px
const baseSize = 3000;
const width = rw >= rh ? baseSize : baseSize * (rw/rh);
const height = rh > rw ? baseSize : baseSize * (rh/rw);
exportCanvas.width = width;
exportCanvas.height = height;
const ctx = exportCanvas.getContext('2d');
if(ctx) {
drawComposition(ctx, width, height, config);
const link = document.createElement('a');
link.download = `constructivist-${config.seed}-${Date.now()}.png`;
link.href = exportCanvas.toDataURL('image/png');
link.click();
}
};
return (
<div className="flex h-screen w-screen overflow-hidden text-gray-800">
{/* Sidebar Controls - Fixed width on Desktop, full on Mobile */}
<div className="w-80 h-full flex-shrink-0 z-10 shadow-xl">
<Controls
config={config}
onChange={setConfig}
onGenerate={handleGenerate}
onDownload={handleDownload}
/>
</div>
{/* Main Canvas Area */}
<div ref={containerRef} className="flex-1 h-full bg-gray-100 flex items-center justify-center overflow-hidden relative">
{/* Decorative background grid pattern for the 'desk' area */}
<div className="absolute inset-0 opacity-5 pointer-events-none" style={{
backgroundImage: 'radial-gradient(#000 1px, transparent 1px)',
backgroundSize: '20px 20px'
}}></div>
<canvas
ref={canvasRef}
className="shadow-2xl bg-white transition-all duration-300 ease-in-out"
/>
</div>
</div>
);
}
export default App;