File size: 21,210 Bytes
7d51cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f86798f
7d51cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Frame Studio</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Poppins', sans-serif;
            background-color: #f9fafb; /* gray-50 */
        }
        /* Custom scrollbar for the new theme */
        ::-webkit-scrollbar { width: 8px; }
        ::-webkit-scrollbar-track { background: #e5e7eb; } /* gray-200 */
        ::-webkit-scrollbar-thumb { background: #f472b6; border-radius: 4px; } /* pink-400 */
        ::-webkit-scrollbar-thumb:hover { background: #ec4899; } /* pink-500 */

        /* Custom styles for range inputs - Light Mode */
        input[type="range"] {
            -webkit-appearance: none; appearance: none; width: 100%; height: 6px;
            background: #e5e7eb; /* gray-200 */ border-radius: 3px;
            outline: none; transition: background 0.2s;
        }
        input[type="range"]::-webkit-slider-thumb {
            -webkit-appearance: none; appearance: none; width: 20px; height: 20px;
            border-radius: 50%; background: #f9fafb; /* gray-50 */ cursor: pointer;
            border: 4px solid #ec4899; /* pink-500 */
            transition: transform 0.2s ease-in-out;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        input[type="range"]:hover::-webkit-slider-thumb { transform: scale(1.1); }
        
        /* Custom styles for color inputs - Light Mode */
        input[type="color"] {
            -webkit-appearance: none; -moz-appearance: none; appearance: none;
            width: 48px; height: 48px; background-color: transparent;
            border: none; cursor: pointer; border-radius: 0.5rem;
        }
        input[type="color"]::-webkit-color-swatch {
            border-radius: 0.5rem;
            border: 2px solid #d1d5db; /* gray-300 */
        }
        
        /* Modal styles */
        #saveModal { display: none; }
    </style>
</head>
<body class="text-gray-800">

    <div class="container mx-auto p-4 lg:p-8">
        <!-- Header -->
        <header class="text-center mb-8 lg:mb-12">
            <h1 class="text-4xl lg:text-5xl font-bold text-gray-900 tracking-tight">Frame Studio</h1>
            <p class="text-lg text-gray-500 mt-2">Craft your perfect checkerboard frame.</p>
        </header>

        <main class="grid grid-cols-1 lg:grid-cols-5 gap-8">
            <!-- Controls Column -->
            <div class="lg:col-span-2 space-y-8">
                <!-- Appearance Controls -->
                <div class="bg-white p-6 rounded-2xl border border-gray-200 shadow-sm">
                    <h2 class="text-2xl font-semibold text-gray-900 mb-5">Appearance</h2>
                    <div class="flex items-center justify-around space-x-4">
                        <!-- Color 1 -->
                        <div class="text-center space-y-2">
                            <input id="color1" type="color" value="#6b7280">
                            <label for="color1" class="block text-sm font-medium text-gray-600">Color 1</label>
                            <span id="color1Value" class="font-mono text-xs text-pink-600">#6b7280</span>
                            <label class="flex items-center justify-center gap-2 text-gray-500 text-sm pt-1"><input id="color1Transparent" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-pink-500 focus:ring-pink-500"> Transp.</label>
                        </div>
                        <!-- Swap & Randomize Buttons -->
                        <div class="space-y-4">
                            <button id="swapBtn" title="Swap Colors" class="p-3 bg-gray-100 hover:bg-pink-100 rounded-full transition-all duration-200 group">
                               <svg class="w-6 h-6 text-gray-500 group-hover:text-pink-600 transition-transform group-hover:rotate-180" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4"></path></svg>
                            </button>
                             <button id="randomizeBtn" title="Randomize" class="p-3 bg-gray-100 hover:bg-pink-100 rounded-full transition-all duration-200 group">
                                <svg class="w-6 h-6 text-gray-500 group-hover:text-pink-600 transition-transform group-hover:rotate-90" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6V4m0 16v-2m8-6h-2m-16 0H4m14.485 8.485l-1.414-1.414M5.929 5.929L4.515 4.515m14.97 14.97l-1.414-1.414M5.929 18.071l-1.414 1.414M12 18a6 6 0 100-12 6 6 0 000 12z"></path></svg>
                            </button>
                        </div>
                        <!-- Color 2 -->
                        <div class="text-center space-y-2">
                            <input id="color2" type="color" value="#d1d5db">
                            <label for="color2" class="block text-sm font-medium text-gray-600">Color 2</label>
                            <span id="color2Value" class="font-mono text-xs text-pink-600">#d1d5db</span>
                            <label class="flex items-center justify-center gap-2 text-gray-500 text-sm pt-1"><input id="color2Transparent" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-pink-500 focus:ring-pink-500"> Transp.</label>
                        </div>
                    </div>
                </div>
                
                <!-- Pattern Controls -->
                <div class="bg-white p-6 rounded-2xl border border-gray-200 shadow-sm space-y-6">
                    <h2 class="text-2xl font-semibold text-gray-900 mb-2">Pattern</h2>
                    <div>
                        <label for="frameThickness" class="flex justify-between items-center font-medium text-gray-600 mb-2">
                            <span>Frame Thickness</span>
                            <span id="frameThicknessValue" class="font-mono text-pink-600 bg-pink-50 px-2 py-1 text-sm rounded-md">3</span>
                        </label>
                        <input id="frameThickness" type="range" min="1" max="20" value="3" class="w-full">
                    </div>
                    <div>
                        <label for="checkerSize" class="flex justify-between items-center font-medium text-gray-600 mb-2">
                            <span>Checker Size</span>
                            <span id="checkerSizeValue" class="font-mono text-pink-600 bg-pink-50 px-2 py-1 text-sm rounded-md">25</span>
                        </label>
                        <input id="checkerSize" type="range" min="5" max="100" value="25" class="w-full">
                    </div>
                </div>
                
                <!-- Export Button -->
                <button id="showSaveModalBtn" class="w-full text-lg font-bold py-4 px-4 rounded-xl transition-all duration-300 bg-gradient-to-r from-pink-500 to-orange-400 hover:from-pink-600 hover:to-orange-500 text-white shadow-lg hover:shadow-pink-500/30 transform hover:scale-105">
                    Export Images...
                </button>
            </div>

            <!-- Canvas Column -->
            <div class="lg:col-span-3">
                <div class="aspect-square bg-white rounded-2xl overflow-hidden border-2 border-gray-200 shadow-sm sticky top-8">
                    <canvas id="checkerboardCanvas"></canvas>
                </div>
            </div>
        </main>
    </div>

    <!-- Save Modal -->
    <div id="saveModal" class="fixed inset-0 bg-gray-900/60 backdrop-blur-sm flex items-center justify-center p-4">
        <div id="modalContent" class="bg-white rounded-2xl shadow-2xl p-8 w-full max-w-lg border border-gray-200">
            <!-- View 1: Options -->
            <div id="modalOptionsView">
                <h2 class="text-2xl font-bold text-gray-900 mb-2">Export Options</h2>
                <p class="text-gray-500 mb-6">Select sizes to generate. You can pick multiple.</p>
                <div id="sizeOptions" class="grid grid-cols-2 sm:grid-cols-3 gap-3 mb-6"></div>
                <div class="mb-6 space-y-4">
                    <label class="block text-gray-600 font-medium">Custom Size</label>
                    <div class="flex items-center gap-3">
                        <input type="number" id="customWidth" placeholder="Width" class="w-full bg-gray-100 border border-gray-300 rounded-md px-3 py-2 text-gray-800 placeholder-gray-400 focus:ring-pink-500 focus:border-pink-500">
                        <span class="text-gray-400">x</span>
                        <input type="number" id="customHeight" placeholder="Height" class="w-full bg-gray-100 border border-gray-300 rounded-md px-3 py-2 text-gray-800 placeholder-gray-400 focus:ring-pink-500 focus:border-pink-500">
                    </div>
                </div>
                <div class="flex gap-4">
                    <button id="cancelSaveBtn" class="w-full bg-gray-200 hover:bg-gray-300 text-gray-700 font-bold py-3 px-4 rounded-lg transition-colors">Cancel</button>
                    <button id="generateBtn" class="w-full bg-gradient-to-r from-pink-500 to-orange-400 hover:from-pink-600 hover:to-orange-500 text-white font-bold py-3 px-4 rounded-lg transition-all">Generate</button>
                </div>
            </div>

            <!-- View 2: Results -->
            <div id="modalResultsView" class="hidden">
                <h2 class="text-2xl font-bold text-gray-900 mb-2">Your Images</h2>
                <p class="text-gray-500 mb-6">Long-press or right-click an image to save.</p>
                <div id="generatedImagesContainer" class="space-y-4 max-h-[60vh] overflow-y-auto p-1 -mr-4 pr-4"></div>
                <div class="flex mt-6">
                    <button id="backToOptionsBtn" class="w-full bg-gray-200 hover:bg-gray-300 text-gray-700 font-bold py-3 px-4 rounded-lg transition-colors">Back</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            // --- STATE MANAGEMENT ---
            const state = {
                frameThickness: 3,
                checkerSize: 25,
                color1: '#6b7280', // gray-500
                color2: '#d1d5db', // gray-300
                color1Transparent: false,
                color2Transparent: false,
            };

            // --- DOM ELEMENT SELECTION ---
            const dom = {
                canvas: document.getElementById('checkerboardCanvas'),
                controls: {
                    frameThickness: document.getElementById('frameThickness'),
                    checkerSize: document.getElementById('checkerSize'),
                    color1: document.getElementById('color1'),
                    color2: document.getElementById('color2'),
                    color1Transparent: document.getElementById('color1Transparent'),
                    color2Transparent: document.getElementById('color2Transparent'),
                    swapBtn: document.getElementById('swapBtn'),
                    randomizeBtn: document.getElementById('randomizeBtn'),
                },
                values: {
                    frameThickness: document.getElementById('frameThicknessValue'),
                    checkerSize: document.getElementById('checkerSizeValue'),
                    color1: document.getElementById('color1Value'),
                    color2: document.getElementById('color2Value'),
                },
                modal: {
                    element: document.getElementById('saveModal'),
                    content: document.getElementById('modalContent'),
                    showBtn: document.getElementById('showSaveModalBtn'),
                    optionsView: document.getElementById('modalOptionsView'),
                    cancelBtn: document.getElementById('cancelSaveBtn'),
                    generateBtn: document.getElementById('generateBtn'),
                    sizeOptions: document.getElementById('sizeOptions'),
                    customWidth: document.getElementById('customWidth'),
                    customHeight: document.getElementById('customHeight'),
                    resultsView: document.getElementById('modalResultsView'),
                    imagesContainer: document.getElementById('generatedImagesContainer'),
                    backBtn: document.getElementById('backToOptionsBtn'),
                }
            };
            const ctx = dom.canvas.getContext('2d');

            // --- CONFIGURATION ---
            const PREDEFINED_SIZES = [
                { w: 256, h: 256 }, { w: 512, h: 512 }, { w: 1024, h: 1024 },
                { w: 1920, h: 1080 }, { w: 1080, h: 1920 }, { w: 1600, h: 900 }
            ];

            // --- CORE LOGIC ---
            const drawPattern = (targetCtx, width, height, config) => {
                const { frameThickness, checkerSize, color1, color2, color1Transparent, color2Transparent } = config;
                const c1 = color1Transparent ? 'transparent' : color1;
                const c2 = color2Transparent ? 'transparent' : color2;
                
                const cols = Math.ceil(width / checkerSize);
                const rows = Math.ceil(height / checkerSize);
                targetCtx.clearRect(0, 0, width, height);

                for (let i = 0; i < rows; i++) {
                    for (let j = 0; j < cols; j++) {
                        const isFrame = i < frameThickness || i >= rows - frameThickness || j < frameThickness || j >= cols - frameThickness;
                        if (isFrame) {
                            targetCtx.fillStyle = (i + j) % 2 === 0 ? c1 : c2;
                            if (targetCtx.fillStyle !== 'transparent') {
                                targetCtx.fillRect(j * checkerSize, i * checkerSize, checkerSize, checkerSize);
                            }
                        }
                    }
                }
            };

            const drawLiveCanvas = () => {
                const boardSize = dom.canvas.parentElement.clientWidth;
                dom.canvas.width = boardSize;
                dom.canvas.height = boardSize;
                const liveConfig = { ...state };
                drawPattern(ctx, boardSize, boardSize, liveConfig);
            };

            const updateUI = () => {
                for (const key in dom.controls) {
                    if (state.hasOwnProperty(key)) {
                        const el = dom.controls[key];
                        const value = state[key];
                        if (el.type === 'checkbox') el.checked = value;
                        else el.value = value;
                    }
                }
                for (const key in dom.values) {
                    if (state.hasOwnProperty(key)) {
                        dom.values[key].textContent = state[key];
                    }
                }
                drawLiveCanvas();
            };

            // --- EVENT HANDLERS ---
            const handleControlInput = (e) => {
                const { id, value, type, checked } = e.target;
                state[id] = type === 'checkbox' ? checked : value;
                if(type === 'range') state[id] = parseInt(value, 10);
                // BUG FIX: Directly update the UI and redraw on every single input change.
                updateUI();
            };

            const handleSwap = () => {
                [state.color1, state.color2] = [state.color2, state.color1];
                [state.color1Transparent, state.color2Transparent] = [state.color2Transparent, state.color1Transparent];
                updateUI();
            };

            const handleRandomize = () => {
                const randomHex = () => '#' + Math.floor(Math.random()*16777215).toString(16).padStart(6, '0');
                state.color1 = randomHex();
                state.color2 = randomHex();
                state.frameThickness = Math.floor(Math.random() * 19) + 1;
                state.checkerSize = Math.floor(Math.random() * 95) + 5;
                state.color1Transparent = false;
                state.color2Transparent = Math.random() < 0.2;
                updateUI();
            };

            const handleGenerate = () => {
                const sizesToGenerate = [];
                document.querySelectorAll('#sizeOptions input:checked').forEach(cb => {
                    const [w, h] = cb.value.split('x').map(Number);
                    sizesToGenerate.push({ w, h });
                });
                
                // BUG FIX: Added robust parsing for custom sizes to prevent crashes.
                const customW = parseInt(dom.modal.customWidth.value, 10) || 0;
                const customH = parseInt(dom.modal.customHeight.value, 10) || 0;
                if (customW > 0 && customH > 0) {
                    sizesToGenerate.push({ w: customW, h: customH });
                }

                if (sizesToGenerate.length === 0) { 
                    // Add a visual cue that nothing was selected
                    dom.modal.generateBtn.textContent = "Select a size!";
                    dom.modal.generateBtn.classList.add('bg-red-500');
                    setTimeout(() => {
                        dom.modal.generateBtn.textContent = "Generate";
                        dom.modal.generateBtn.classList.remove('bg-red-500');
                    }, 2000);
                    return;
                }

                dom.modal.imagesContainer.innerHTML = '';
                sizesToGenerate.forEach(size => {
                    const offscreenCanvas = document.createElement('canvas');
                    offscreenCanvas.width = size.w;
                    offscreenCanvas.height = size.h;
                    drawPattern(offscreenCanvas.getContext('2d'), size.w, size.h, state);
                    
                    const imageCard = document.createElement('div');
                    imageCard.className = 'bg-gray-100 p-3 rounded-lg border border-gray-200';
                    imageCard.innerHTML = `
                        <p class="text-gray-700 font-semibold mb-2">${size.w} x ${size.h} px</p>
                        <img src="${offscreenCanvas.toDataURL('image/png')}" alt="Generated frame" class="rounded-md border border-gray-300 w-full">`;
                    dom.modal.imagesContainer.appendChild(imageCard);
                });

                dom.modal.customWidth.value = dom.modal.customHeight.value = '';
                document.querySelectorAll('#sizeOptions input:checked').forEach(cb => cb.checked = false);
                dom.modal.optionsView.classList.add('hidden');
                dom.modal.resultsView.classList.remove('hidden');
            };
            
            // --- MODAL ---
            const openModal = () => {
                dom.modal.element.style.display = 'flex';
            };
            const closeModal = () => {
                dom.modal.element.style.display = 'none';
                dom.modal.resultsView.classList.add('hidden');
                dom.modal.optionsView.classList.remove('hidden');
            };

            // --- INITIALIZATION ---
            const initialize = () => {
                PREDEFINED_SIZES.forEach(size => {
                    const val = `${size.w}x${size.h}`;
                    dom.modal.sizeOptions.innerHTML += `
                        <label class="flex items-center gap-2 p-3 bg-gray-100 rounded-lg cursor-pointer hover:bg-gray-200 border border-gray-200 transition-colors">
                            <input type="checkbox" value="${val}" class="h-4 w-4 rounded border-gray-300 text-pink-500 focus:ring-pink-500">
                            <span class="text-gray-700 text-sm font-medium">${val}</span>
                        </label>`;
                });
                
                Object.values(dom.controls).forEach(el => el.addEventListener('input', handleControlInput));
                dom.controls.swapBtn.addEventListener('click', handleSwap);
                dom.controls.randomizeBtn.addEventListener('click', handleRandomize);
                dom.modal.showBtn.addEventListener('click', openModal);
                dom.modal.cancelBtn.addEventListener('click', closeModal);
                dom.modal.generateBtn.addEventListener('click', handleGenerate);
                dom.modal.backBtn.addEventListener('click', () => {
                    dom.modal.resultsView.classList.add('hidden');
                    dom.modal.optionsView.classList.remove('hidden');
                });

                new ResizeObserver(drawLiveCanvas).observe(dom.canvas.parentElement);
                updateUI();
            };

            initialize();
        });
    </script>
</body>
</html>