veltrixcode commited on
Commit
6d42a19
·
verified ·
1 Parent(s): 9e7ba0c

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +612 -549
templates/index.html CHANGED
@@ -4,7 +4,7 @@
4
  <head>
5
  <meta charset="UTF-8">
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- <title>Remove Background - Fast & Free</title>
8
  <!-- Google Fonts: Inter -->
9
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
10
  <!-- Google Material Symbols -->
@@ -13,341 +13,406 @@
13
 
14
  <style>
15
  :root {
16
- --primary: #0066ff;
17
- --primary-hover: #075ce0;
18
- --bg-body: #f9fafb;
19
  --surface: #ffffff;
20
- --text-main: #191c1f;
21
- --text-secondary: #5e6b76;
22
- --border: #e6e8eb;
23
- --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.05);
24
- --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
25
- --radius-lg: 16px;
26
- --radius-full: 9999px;
 
 
 
 
 
 
 
 
 
27
  }
28
 
29
  body {
30
  font-family: 'Inter', sans-serif;
31
- background-color: var(--bg-body);
32
  margin: 0;
33
- padding: 0;
34
  color: var(--text-main);
35
- min-height: 100vh;
36
  display: flex;
37
  flex-direction: column;
 
 
38
  }
39
 
40
- /* Navbar */
41
  nav {
 
42
  background: var(--surface);
43
- padding: 1rem 1.5rem;
44
- box-shadow: var(--shadow-sm);
45
  display: flex;
46
  align-items: center;
47
  justify-content: space-between;
48
- position: sticky;
49
- top: 0;
50
- z-index: 100;
51
  }
52
 
53
  .brand {
54
  display: flex;
55
  align-items: center;
56
- gap: 8px;
57
  font-weight: 700;
58
- font-size: 1.25rem;
59
- color: #383e45;
60
  text-decoration: none;
 
61
  }
62
 
63
- .brand-icon {
64
- color: var(--text-secondary);
65
- font-variation-settings: 'FILL' 1;
66
  }
67
 
68
- /* Hero / Main Area */
69
  main {
70
  flex: 1;
71
  display: flex;
 
 
 
 
 
 
 
 
 
72
  flex-direction: column;
73
  align-items: center;
74
  justify-content: center;
75
- padding: 1.5rem;
76
- width: 100%;
77
- max-width: 1200px;
78
- margin: 0 auto;
79
- box-sizing: border-box;
80
  }
81
 
82
- .hero-content {
83
  text-align: center;
84
- margin-bottom: 2rem;
85
- max-width: 600px;
86
  }
87
 
88
- h1 {
89
- font-size: 2.75rem;
90
- line-height: 1.2;
91
- margin-bottom: 0.75rem;
92
- color: var(--text-main);
93
- letter-spacing: -0.02em;
 
94
  }
95
 
96
- .subtitle {
97
- color: var(--text-secondary);
98
- font-size: 1.125rem;
99
  margin: 0;
100
  }
101
 
102
- /* Card Container */
103
- .card {
104
- background: var(--surface);
105
- border-radius: var(--radius-lg);
106
- box-shadow: var(--shadow-md);
107
  width: 100%;
108
- max-width: 1000px;
109
- overflow: hidden;
110
- transition: transform 0.2s ease, box-shadow 0.2s ease;
111
- }
112
-
113
- /* Upload State */
114
- .upload-view {
115
- padding: 2rem;
116
  max-width: 600px;
117
- margin: 0 auto;
118
- }
119
-
120
- .upload-area {
121
- padding: 3rem 2rem;
122
- display: flex;
123
- flex-direction: column;
124
- align-items: center;
125
- justify-content: center;
126
- border: 2px dashed #dbe0e6;
127
- margin: 2rem 0;
128
- border-radius: 12px;
129
- background-color: #fcfcfd;
130
- transition: all 0.2s ease;
131
  cursor: pointer;
 
 
132
  }
133
 
134
- .upload-area.dragover {
 
135
  border-color: var(--primary);
136
- background-color: #f0f7ff;
137
- }
138
-
139
- .upload-area:hover {
140
- border-color: #b0b8c1;
141
  }
142
 
143
- .btn-primary {
144
- background-color: var(--primary);
145
  color: white;
146
- padding: 14px 32px;
147
- border-radius: var(--radius-full);
148
  font-weight: 600;
149
- font-size: 1.125rem;
 
150
  border: none;
151
  cursor: pointer;
152
- transition: background 0.2s, transform 0.1s;
153
  display: inline-flex;
154
  align-items: center;
155
- gap: 8px;
156
- box-shadow: 0 4px 12px rgba(0, 102, 255, 0.25);
 
157
  }
158
 
159
- .btn-primary:hover {
160
- background-color: var(--primary-hover);
161
- transform: translateY(-1px);
162
  }
163
 
164
  .upload-hint {
165
- margin-top: 1rem;
166
- color: var(--text-secondary);
167
  font-size: 0.95rem;
168
- display: flex;
169
- align-items: center;
170
- gap: 6px;
171
  }
172
 
173
- /* Result Editor State */
174
- .result-view {
175
  display: none;
 
 
 
176
  flex-direction: row;
177
- height: 600px;
178
  }
179
 
180
- /* Left: Preview */
181
- .preview-area {
182
- flex: 2;
183
- background: #f3f5f7;
184
  display: flex;
185
  align-items: center;
186
  justify-content: center;
187
  padding: 2rem;
188
  position: relative;
189
- overflow: hidden;
 
 
 
 
 
 
 
 
190
  }
191
 
192
- .preview-container {
193
  position: relative;
194
- max-width: 100%;
195
- max-height: 100%;
196
- border-radius: 8px;
197
  overflow: hidden;
198
- box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
199
- background-color: white;
200
- /* Base fallback */
201
- /* Checkerboard pattern base */
202
- background-image:
203
- linear-gradient(45deg, #e5e7eb 25%, transparent 25%),
204
- linear-gradient(-45deg, #e5e7eb 25%, transparent 25%),
205
- linear-gradient(45deg, transparent 75%, #e5e7eb 75%),
206
- linear-gradient(-45deg, transparent 75%, #e5e7eb 75%);
207
- background-size: 20px 20px;
208
- background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
209
  }
210
 
211
- .bg-layer {
212
  position: absolute;
213
- top: 0;
214
- left: 0;
215
- width: 100%;
216
- height: 100%;
217
- z-index: 1;
218
  background-size: cover;
219
  background-position: center;
220
- background-repeat: no-repeat;
221
  }
222
 
223
- .subject-layer {
224
  position: relative;
225
  z-index: 2;
226
- max-height: 500px;
227
  display: block;
228
- pointer-events: none;
 
229
  }
230
 
231
- /* Right: Sidebar */
232
- .sidebar {
233
- flex: 1;
234
- min-width: 320px;
235
  background: var(--surface);
236
  border-left: 1px solid var(--border);
237
  display: flex;
238
  flex-direction: column;
 
 
239
  }
240
 
241
- .sidebar-header {
242
  padding: 1.5rem;
243
  border-bottom: 1px solid var(--border);
 
 
 
244
  }
245
 
246
- .sidebar-title {
247
  font-weight: 700;
248
- font-size: 1.125rem;
 
249
  margin: 0;
250
  }
251
 
252
- .tabs {
 
253
  display: flex;
254
- padding: 0 1rem;
255
- gap: 1rem;
256
- border-bottom: 1px solid var(--border);
 
257
  }
258
 
259
- .tab {
260
- padding: 1rem 0.5rem;
261
- color: var(--text-secondary);
 
 
 
 
 
262
  cursor: pointer;
263
- font-weight: 500;
264
- position: relative;
265
- }
266
-
267
- .tab.active {
268
- color: var(--primary);
269
  }
270
 
271
- .tab.active::after {
272
- content: '';
273
- position: absolute;
274
- bottom: -1px;
275
- left: 0;
276
- right: 0;
277
- height: 2px;
278
- background: var(--primary);
279
  }
280
 
281
- .sidebar-content {
 
282
  flex: 1;
283
  overflow-y: auto;
284
- padding: 1.5rem;
285
  }
286
 
287
- .sidebar-subtitle {
288
- font-size: 0.85rem;
289
- font-weight: 600;
 
290
  text-transform: uppercase;
291
  letter-spacing: 0.5px;
292
- color: var(--text-secondary);
293
- margin-bottom: 12px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  }
295
 
296
- .swatch-grid {
 
297
  display: grid;
298
- grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
299
  gap: 12px;
300
  }
301
 
302
- .swatch {
303
- width: 48px;
304
- height: 48px;
305
- border-radius: 8px;
306
  cursor: pointer;
307
  border: 2px solid transparent;
308
- box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
309
  position: relative;
310
- overflow: hidden;
311
  background-size: cover;
312
  background-position: center;
 
313
  }
314
 
315
- .swatch.selected {
316
- border-color: var(--primary);
317
- box-shadow: 0 0 0 2px rgba(0, 102, 255, 0.2);
318
  }
319
 
320
- .swatch.none {
321
- background: white;
322
- background-image:
323
- linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc),
324
- linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc);
325
- background-size: 10px 10px;
326
- background-position: 0 0, 5px 5px;
327
  }
328
 
329
- .swatch.upload-btn {
330
- border: 2px dashed #ccc;
331
  display: flex;
 
332
  align-items: center;
333
  justify-content: center;
334
- color: var(--text-secondary);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  }
336
 
337
- .sidebar-footer {
 
338
  padding: 1.5rem;
339
  border-top: 1px solid var(--border);
340
  display: flex;
341
  gap: 1rem;
 
342
  }
343
 
344
- .btn-download {
345
  flex: 1;
346
  background: var(--primary);
347
  color: white;
348
- padding: 12px;
349
- border-radius: 8px;
350
- text-align: center;
351
  font-weight: 600;
352
  border: none;
353
  cursor: pointer;
@@ -355,63 +420,48 @@
355
  align-items: center;
356
  justify-content: center;
357
  gap: 8px;
 
 
358
  }
359
 
360
- .btn-outline {
361
- padding: 12px;
362
- border: 1px solid var(--border);
363
- border-radius: 8px;
364
- background: transparent;
365
- cursor: pointer;
366
- color: var(--text-main);
367
- font-weight: 500;
368
- }
369
-
370
- /* Gradient Builder */
371
- .grad-builder {
372
- display: flex;
373
- gap: 8px;
374
- align-items: center;
375
- margin-bottom: 1.5rem;
376
- padding: 12px;
377
- background: #f3f5f7;
378
- border-radius: 8px;
379
  }
380
 
381
- .grad-builder input[type="color"] {
382
- width: 32px;
383
- height: 32px;
 
 
384
  border: none;
385
- padding: 0;
386
- border-radius: 4px;
387
  cursor: pointer;
388
- background: none;
389
  }
390
 
391
- .grad-builder input[type="number"] {
392
- width: 40px;
393
- padding: 6px;
394
- border: 1px solid var(--border);
395
- border-radius: 4px;
396
- font-family: inherit;
397
  }
398
 
399
- /* Loading */
400
  .loader-overlay {
 
 
 
 
401
  display: none;
402
  flex-direction: column;
403
  align-items: center;
404
  justify-content: center;
405
- padding: 4rem;
406
  }
407
 
408
- .spinner-ring {
409
- width: 48px;
410
- height: 48px;
411
- border: 4px solid #f3f3f3;
412
  border-top-color: var(--primary);
413
  border-radius: 50%;
414
- animation: spin 1s linear infinite;
415
  }
416
 
417
  @keyframes spin {
@@ -420,21 +470,38 @@
420
  }
421
  }
422
 
 
 
 
 
 
 
 
 
 
 
 
 
423
  /* Mobile */
424
  @media (max-width: 900px) {
425
- .result-view {
 
 
 
 
426
  flex-direction: column;
427
- height: auto;
428
  }
429
 
430
- .preview-area {
431
- min-height: 400px;
 
432
  }
433
 
434
- .sidebar {
435
  width: 100%;
436
- border-left: none;
437
- border-top: 1px solid var(--border);
 
438
  }
439
  }
440
  </style>
@@ -444,411 +511,407 @@
444
 
445
  <nav>
446
  <a href="#" class="brand">
447
- <span class="material-symbols-rounded brand-icon">layers</span>
448
  RemoveBG
449
  </a>
 
 
 
 
 
450
  </nav>
451
 
452
  <main>
453
 
454
- <div id="hero" class="hero-content">
455
- <h1>Remove Image Background</h1>
456
- <p class="subtitle">100% Automatic and Free</p>
 
 
 
 
 
 
 
 
 
 
 
 
457
  </div>
458
 
459
- <div class="card">
 
460
 
461
- <!-- Upload View -->
462
- <div id="uploadView" class="upload-view">
463
- <div class="upload-area" id="dropZone" onclick="document.getElementById('fileInput').click()">
464
- <button class="btn-primary">
465
- <span class="material-symbols-rounded">upload_file</span>
466
- Upload Image
467
- </button>
468
- <div class="upload-hint">
469
- <span class="material-symbols-rounded" style="font-size: 18px;">drag_indicator</span>
470
- or drop a file
471
- </div>
472
  </div>
473
- <input type="file" id="fileInput" name="file" accept="image/*" hidden>
474
  </div>
475
 
476
- <!-- Loader -->
477
- <div id="loaderView" class="loader-overlay">
478
- <div class="spinner-ring"></div>
479
- <div style="margin-top: 1rem; color: var(--text-secondary);">Removing background...</div>
480
- </div>
481
 
482
- <!-- Editor View -->
483
- <div id="resultView" class="result-view">
484
- <!-- Left: Preview -->
485
- <div class="preview-area">
486
- <div class="preview-container" id="previewContainer">
487
- <div id="bgLayer" class="bg-layer"></div>
488
- <img id="subjectImg" class="subject-layer" src="" alt="Subject">
489
- </div>
490
  </div>
491
 
492
- <!-- Right: Controls -->
493
- <div class="sidebar">
494
- <div class="sidebar-header">
495
- <h3 class="sidebar-title">Background</h3>
496
- </div>
497
-
498
- <div class="tabs">
499
- <div class="tab active" onclick="switchTab('color')">Color</div>
500
- <div class="tab" onclick="switchTab('image')">Image</div>
501
- </div>
502
-
503
- <div class="sidebar-content" id="tabContent">
504
- <!-- Filled by JS -->
505
- </div>
506
-
507
- <div class="sidebar-footer">
508
- <button class="btn-outline" onclick="resetUI()">New</button>
509
- <button class="btn-download" onclick="downloadImage()">
510
- <span class="material-symbols-rounded">download</span>
511
- Download
512
- </button>
513
- </div>
514
  </div>
515
  </div>
 
516
 
 
 
 
 
517
  </div>
518
 
519
  </main>
520
 
521
  <script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522
  // Elements
523
- const fileInput = document.getElementById('fileInput');
524
  const uploadView = document.getElementById('uploadView');
525
- const loaderView = document.getElementById('loaderView');
526
- const resultView = document.getElementById('resultView');
527
  const subjectImg = document.getElementById('subjectImg');
528
  const bgLayer = document.getElementById('bgLayer');
529
- const tabContent = document.getElementById('tabContent');
530
- const hero = document.getElementById('hero');
531
-
532
- let currentBg = { type: 'none', value: '' };
533
-
534
- // Data
535
- const colors = [
536
- 'transparent', '#ffffff', '#000000', '#f3f4f6',
537
- '#ef4444', '#f97316', '#f59e0b', '#84cc16',
538
- '#10b981', '#06b6d4', '#3b82f6', '#6366f1',
539
- '#8b5cf6', '#d946ef', '#f43f5e', '#881337'
540
- ];
541
-
542
- const gradients = [
543
- 'linear-gradient(to right, #ffecd2 0%, #fcb69f 100%)',
544
- 'linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%)',
545
- 'linear-gradient(120deg, #d4fc79 0%, #96e6a1 100%)',
546
- 'linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%)',
547
- 'linear-gradient(to top, #fbc2eb 0%, #a6c1ee 100%)'
548
- ];
549
-
550
- // Init
551
- switchTab('color');
552
-
553
- // Drag & Drop
554
  const dropZone = document.getElementById('dropZone');
555
- dropZone.addEventListener('dragover', (e) => { e.preventDefault(); dropZone.classList.add('dragover'); });
 
 
556
  dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
557
- dropZone.addEventListener('drop', (e) => {
558
  e.preventDefault();
559
  dropZone.classList.remove('dragover');
560
- if (e.dataTransfer.files.length) handleFile(e.dataTransfer.files[0]);
561
  });
562
-
563
- fileInput.addEventListener('change', (e) => {
564
- if (e.target.files.length) handleFile(e.target.files[0]);
565
  });
566
 
567
  function handleFile(file) {
568
- if (!file.type.startsWith('image/')) return alert('Image only!');
569
 
570
- uploadView.style.display = 'none';
571
- hero.style.display = 'none';
572
- loaderView.style.display = 'flex';
573
 
574
- const formData = new FormData();
575
- formData.append('file', file);
576
-
577
- fetch('/', { method: 'POST', body: formData })
578
- .then(res => res.blob())
579
  .then(blob => {
580
  const url = URL.createObjectURL(blob);
581
  subjectImg.src = url;
582
-
583
- // Reset BG
584
- setBackground('color', 'transparent');
585
-
586
- loaderView.style.display = 'none';
587
- resultView.style.display = 'flex';
 
 
 
 
 
588
  })
589
- .catch(() => {
590
- alert('Error processing image');
591
- resetUI();
 
592
  });
593
  }
594
 
595
- // Tabs Logic
596
- function switchTab(name) {
597
- document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
598
- const tabs = document.querySelectorAll('.tab');
599
- if (name === 'color') tabs[0].classList.add('active');
600
- else tabs[1].classList.add('active'); // assuming 2 tabs
601
-
602
- renderSidebar(name);
603
  }
604
 
605
- function renderSidebar(mode) {
606
- tabContent.innerHTML = '';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607
 
608
- if (mode === 'color') {
609
- // --- SOLID COLORS ---
610
- const colorTitle = document.createElement('div');
611
- colorTitle.className = 'sidebar-subtitle';
612
- colorTitle.innerText = 'Colors';
613
- tabContent.appendChild(colorTitle);
614
 
615
  const grid = document.createElement('div');
616
- grid.className = 'swatch-grid';
617
-
618
- // Custom Picker Button
619
- const pickerContainer = document.createElement('div');
620
- pickerContainer.className = 'swatch upload-btn';
621
- pickerContainer.title = "Custom Color";
622
- pickerContainer.innerHTML = '<span class="material-symbols-rounded" style="color:var(--text-main)">colorize</span>';
623
-
624
- const colorInput = document.createElement('input');
625
- colorInput.type = 'color';
626
- colorInput.style.opacity = 0;
627
- colorInput.style.position = 'absolute';
628
- colorInput.style.width = '100%';
629
- colorInput.style.height = '100%';
630
- colorInput.style.cursor = 'pointer';
631
- colorInput.oninput = (e) => {
632
- setBackground('color', e.target.value);
633
- document.querySelectorAll('.swatch').forEach(s => s.classList.remove('selected'));
634
- pickerContainer.classList.add('selected');
635
- pickerContainer.style.background = e.target.value;
636
- pickerContainer.innerHTML = ''; // Hide icon
637
- };
638
- pickerContainer.appendChild(colorInput);
639
- grid.appendChild(pickerContainer);
640
 
641
- // Presets
642
- colors.forEach(c => {
 
 
643
  const el = document.createElement('div');
644
- el.className = 'swatch';
645
- if (c === 'transparent') el.classList.add('none');
646
- else el.style.backgroundColor = c;
647
-
648
- el.onclick = () => {
649
- document.querySelectorAll('.swatch').forEach(s => s.classList.remove('selected'));
650
- el.classList.add('selected');
651
- setBackground('color', c);
652
- };
653
- if (currentBg.value === c) el.classList.add('selected');
654
  grid.appendChild(el);
655
  });
656
- tabContent.appendChild(grid);
657
-
658
- // --- GRADIENTS ---
659
- const gradTitle = document.createElement('div');
660
- gradTitle.className = 'sidebar-subtitle';
661
- gradTitle.innerText = 'Gradients';
662
- gradTitle.style.marginTop = '1.5rem';
663
- tabContent.appendChild(gradTitle);
664
-
665
- // Builder
666
- const gradBuilder = document.createElement('div');
667
- gradBuilder.className = 'grad-builder';
668
-
669
- const g1 = document.createElement('input'); g1.type = 'color'; g1.value = '#a1c4fd';
670
- const g2 = document.createElement('input'); g2.type = 'color'; g2.value = '#c2e9fb';
671
- const deg = document.createElement('input'); deg.type = 'number'; deg.value = 90; deg.style.width = '50px';
672
-
673
- const applyGradient = () => {
674
- const val = `linear-gradient(${deg.value}deg, ${g1.value}, ${g2.value})`;
675
- setBackground('color', val);
676
- document.querySelectorAll('.swatch').forEach(s => s.classList.remove('selected')); // Deselect presets
677
  };
 
678
 
679
- g1.oninput = applyGradient;
680
- g2.oninput = applyGradient;
681
- deg.oninput = applyGradient;
682
 
683
- gradBuilder.appendChild(g1);
684
- gradBuilder.appendChild(g2);
685
- gradBuilder.appendChild(deg);
686
- gradBuilder.appendChild(document.createTextNode('deg'));
687
-
688
- tabContent.appendChild(gradBuilder);
689
 
690
  // Presets
691
- const gradGrid = document.createElement('div');
692
- gradGrid.className = 'swatch-grid';
693
- gradients.forEach(g => {
694
- const el = document.createElement('div');
695
- el.className = 'swatch';
696
- el.style.backgroundImage = g;
697
- el.onclick = () => {
698
- document.querySelectorAll('.swatch').forEach(s => s.classList.remove('selected'));
699
- el.classList.add('selected');
700
- setBackground('color', g);
701
- }
702
- if (currentBg.value === g) el.classList.add('selected');
703
- gradGrid.appendChild(el);
704
- });
705
- tabContent.appendChild(gradGrid);
706
 
707
- }
708
- else if (mode === 'image') {
709
  const grid = document.createElement('div');
710
- grid.className = 'swatch-grid';
711
-
712
- // Upload Btn
713
- const upBtn = document.createElement('div');
714
- upBtn.className = 'swatch upload-btn';
715
- upBtn.innerHTML = '<span class="material-symbols-rounded">add_photo_alternate</span>';
716
- upBtn.onclick = () => document.getElementById('bgInput').click();
717
- grid.appendChild(upBtn);
718
-
719
- // Hidden Input
720
- const input = document.createElement('input');
721
- input.type = 'file';
722
- input.id = 'bgInput';
723
- input.accept = 'image/*';
724
- input.hidden = true;
725
- input.onchange = (e) => {
726
- const file = e.target.files[0];
727
- if (file) {
728
- const url = URL.createObjectURL(file);
729
- setBackground('image', url);
730
- }
731
- };
732
- grid.appendChild(input);
733
- tabContent.appendChild(grid);
734
  }
735
  }
736
 
737
- function setBackground(type, value) {
738
- currentBg = { type, value };
739
- if (type === 'color') {
740
- if (value === 'transparent') {
741
- bgLayer.style.background = 'none';
742
- } else {
743
- bgLayer.style.background = value;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744
  }
745
- } else {
746
- bgLayer.style.backgroundImage = `url(${value})`;
 
747
  }
748
  }
749
 
750
- function resetUI() {
751
- uploadView.style.display = 'block';
752
- hero.style.display = 'block';
753
- resultView.style.display = 'none';
754
- fileInput.value = '';
755
- subjectImg.src = '';
756
- setBackground('color', 'transparent');
757
  }
758
 
759
- // Compositing Download
760
- function downloadImage() {
 
 
 
 
 
 
 
 
 
 
761
  const canvas = document.createElement('canvas');
762
  const ctx = canvas.getContext('2d');
763
-
764
  canvas.width = subjectImg.naturalWidth;
765
  canvas.height = subjectImg.naturalHeight;
766
 
767
- // 1. Draw Background
768
- if (currentBg.type === 'color' && currentBg.value !== 'transparent') {
769
- if (currentBg.value.startsWith('linear-gradient')) {
770
- // Manual Parser for: linear-gradient(deg, c1, c2)
771
- // Handles standard hex codes e.g. linear-gradient(90deg, #ffffff, #000000)
772
- try {
773
- let deg = 90;
774
- let colors = [];
775
-
776
- // Regex to extract degree
777
- const degMatch = currentBg.value.match(/(\d+)deg/);
778
- if (degMatch) deg = parseInt(degMatch[1]);
779
-
780
- // Regex to extract colors (hex or rgb)
781
- const colorMatch = currentBg.value.match(/#[a-fA-F0-9]{6}|#[a-fA-F0-9]{3}|rgb\([^)]+\)|rgba\([^)]+\)/g);
782
- if (colorMatch && colorMatch.length >= 2) {
783
- colors = [colorMatch[0], colorMatch[1]];
784
-
785
- // Canvas Gradient Logic
786
- const w = canvas.width;
787
- const h = canvas.height;
788
-
789
- // Simple Angle Conversion:
790
- // 0deg = Bottom -> Top (x, h) -> (x, 0)
791
- // 90deg = Left -> Right (0, y) -> (w, y)
792
- // 180deg = Top -> Bottom (x, 0) -> (x, h)
793
- // 270deg = Right -> Left (w, y) -> (0, y)
794
- // Approximate for arbitary angles using center rotation if needed,
795
- // BUT for this feature, just supporting 4 directions or basic diagonal is enough for 99% usage.
796
-
797
- // Let's implement full angle logic
798
- const angleRad = (deg - 90) * (Math.PI / 180);
799
-
800
- const x1 = w / 2 + Math.cos(angleRad + Math.PI) * w / 2; // Start point approx
801
- const y1 = h / 2 + Math.sin(angleRad + Math.PI) * h / 2;
802
- const x2 = w / 2 + Math.cos(angleRad) * w / 2;
803
- const y2 = h / 2 + Math.sin(angleRad) * h / 2;
804
-
805
- const grd = ctx.createLinearGradient(x1, y1, x2, y2);
806
- grd.addColorStop(0, colors[0]);
807
- grd.addColorStop(1, colors[1]);
808
-
809
- ctx.fillStyle = grd;
810
- ctx.fillRect(0, 0, w, h);
811
- } else {
812
- // Fallback if parsing fails (e.g. "to right" syntax)
813
- ctx.fillStyle = '#ffffff';
814
- ctx.fillRect(0, 0, canvas.width, canvas.height);
815
- }
816
- } catch (e) {
817
- ctx.fillStyle = '#ffffff';
818
- ctx.fillRect(0, 0, canvas.width, canvas.height);
819
- }
820
- }
821
- else {
822
- ctx.fillStyle = currentBg.value;
823
- ctx.fillRect(0, 0, canvas.width, canvas.height);
824
- }
825
- }
826
- else if (currentBg.type === 'image') {
827
  const img = new Image();
828
- img.crossOrigin = 'Anonymous';
 
829
  img.onload = () => {
 
830
  const sRatio = img.width / img.height;
831
  const dRatio = canvas.width / canvas.height;
832
  let sw, sh, sx, sy;
833
  if (sRatio > dRatio) { sh = img.height; sw = sh * dRatio; sx = (img.width - sw) / 2; sy = 0; }
834
  else { sw = img.width; sh = sw / dRatio; sx = 0; sy = (img.height - sh) / 2; }
 
835
  ctx.drawImage(img, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
836
- drawSubjectNext(ctx, canvas);
837
  };
838
- img.src = currentBg.value;
839
- return;
 
 
 
 
840
  }
841
-
842
- drawSubjectNext(ctx, canvas);
843
  }
844
 
845
- function drawSubjectNext(ctx, canvas) {
846
  ctx.drawImage(subjectImg, 0, 0);
847
- const link = document.createElement('a');
848
- link.download = 'rembg-edit.png';
849
- link.href = canvas.toDataURL('image/png');
850
- link.click();
 
 
 
 
 
 
 
 
 
 
851
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
852
  </script>
853
  </body>
854
 
 
4
  <head>
5
  <meta charset="UTF-8">
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Remove Background - Photo Editor</title>
8
  <!-- Google Fonts: Inter -->
9
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
10
  <!-- Google Material Symbols -->
 
13
 
14
  <style>
15
  :root {
16
+ --primary: #2563eb;
17
+ --primary-hover: #1d4ed8;
18
+ --bg-page: #f8fafc;
19
  --surface: #ffffff;
20
+ --surface-hover: #f1f5f9;
21
+ --text-main: #0f172a;
22
+ --text-sec: #64748b;
23
+ --border: #e2e8f0;
24
+ --shadow-sm: 0 1px 2px -1px rgba(0, 0, 0, 0.08);
25
+ --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
26
+ --radius-xl: 24px;
27
+ --radius-md: 12px;
28
+ --radius-pill: 9999px;
29
+ --h-header: 70px;
30
+ }
31
+
32
+ /* --- Base & Reset --- */
33
+ * {
34
+ box-sizing: border-box;
35
+ outline: none;
36
  }
37
 
38
  body {
39
  font-family: 'Inter', sans-serif;
40
+ background-color: var(--bg-page);
41
  margin: 0;
 
42
  color: var(--text-main);
43
+ height: 100vh;
44
  display: flex;
45
  flex-direction: column;
46
+ overflow: hidden;
47
+ /* App-like feel */
48
  }
49
 
50
+ /* --- Navbar --- */
51
  nav {
52
+ height: var(--h-header);
53
  background: var(--surface);
54
+ padding: 0 2rem;
 
55
  display: flex;
56
  align-items: center;
57
  justify-content: space-between;
58
+ border-bottom: 1px solid var(--border);
59
+ z-index: 50;
 
60
  }
61
 
62
  .brand {
63
  display: flex;
64
  align-items: center;
65
+ gap: 10px;
66
  font-weight: 700;
67
+ font-size: 1.35rem;
68
+ color: #334155;
69
  text-decoration: none;
70
+ letter-spacing: -0.5px;
71
  }
72
 
73
+ .brand span {
74
+ color: var(--primary);
75
+ font-size: 32px;
76
  }
77
 
78
+ /* --- Main Layout --- */
79
  main {
80
  flex: 1;
81
  display: flex;
82
+ position: relative;
83
+ overflow: hidden;
84
+ }
85
+
86
+ /* UPLOAD STATE (Centered) */
87
+ .upload-view {
88
+ position: absolute;
89
+ inset: 0;
90
+ display: flex;
91
  flex-direction: column;
92
  align-items: center;
93
  justify-content: center;
94
+ background: var(--bg-page);
95
+ z-index: 10;
96
+ transition: opacity 0.3s ease;
 
 
97
  }
98
 
99
+ .hero {
100
  text-align: center;
101
+ margin-bottom: 3rem;
102
+ animation: fadeIn 0.8s ease;
103
  }
104
 
105
+ .hero h1 {
106
+ font-size: 3.5rem;
107
+ margin: 0 0 1rem 0;
108
+ letter-spacing: -1px;
109
+ background: linear-gradient(135deg, #0f172a 0%, #334155 100%);
110
+ -webkit-background-clip: text;
111
+ -webkit-text-fill-color: transparent;
112
  }
113
 
114
+ .hero p {
115
+ font-size: 1.25rem;
116
+ color: var(--text-sec);
117
  margin: 0;
118
  }
119
 
120
+ .drop-zone {
 
 
 
 
121
  width: 100%;
 
 
 
 
 
 
 
 
122
  max-width: 600px;
123
+ background: var(--surface);
124
+ border: 2px dashed #cbd5e1;
125
+ border-radius: var(--radius-xl);
126
+ padding: 4rem 2rem;
127
+ text-align: center;
 
 
 
 
 
 
 
 
 
128
  cursor: pointer;
129
+ transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
130
+ box-shadow: var(--shadow-sm);
131
  }
132
 
133
+ .drop-zone:hover,
134
+ .drop-zone.dragover {
135
  border-color: var(--primary);
136
+ transform: translateY(-4px);
137
+ box-shadow: var(--shadow-lg);
 
 
 
138
  }
139
 
140
+ .btn-upload {
141
+ background: var(--primary);
142
  color: white;
143
+ font-size: 1.1rem;
 
144
  font-weight: 600;
145
+ padding: 16px 40px;
146
+ border-radius: var(--radius-pill);
147
  border: none;
148
  cursor: pointer;
 
149
  display: inline-flex;
150
  align-items: center;
151
+ gap: 10px;
152
+ box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
153
+ transition: transform 0.1s;
154
  }
155
 
156
+ .btn-upload:hover {
157
+ transform: scale(1.02);
158
+ background: #1e40af;
159
  }
160
 
161
  .upload-hint {
162
+ margin-top: 1.5rem;
163
+ color: var(--text-sec);
164
  font-size: 0.95rem;
 
 
 
165
  }
166
 
167
+ /* EDITOR STATE (Split View) */
168
+ .editor-view {
169
  display: none;
170
+ /* hidden by default */
171
+ width: 100%;
172
+ height: 100%;
173
  flex-direction: row;
 
174
  }
175
 
176
+ /* Editor: Stage (Left/Center) */
177
+ .stage-area {
178
+ flex: 1;
179
+ background: #e2e8f0;
180
  display: flex;
181
  align-items: center;
182
  justify-content: center;
183
  padding: 2rem;
184
  position: relative;
185
+ /* Checkerboard */
186
+ background-image:
187
+ linear-gradient(45deg, #cbd5e1 25%, transparent 25%),
188
+ linear-gradient(-45deg, #cbd5e1 25%, transparent 25%),
189
+ linear-gradient(45deg, transparent 75%, #cbd5e1 75%),
190
+ linear-gradient(-45deg, transparent 75%, #cbd5e1 75%);
191
+ background-size: 24px 24px;
192
+ background-position: 0 0, 0 12px, 12px -12px, -12px 0px;
193
+ background-color: #f1f5f9;
194
  }
195
 
196
+ .canvas-wrapper {
197
  position: relative;
198
+ max-width: 90%;
199
+ max-height: 90%;
200
+ border-radius: var(--radius-md);
201
  overflow: hidden;
202
+ box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.18);
203
+ background: transparent;
204
+ /* allow checkerboard to show through if bg none */
205
+ transition: all 0.3s ease;
 
 
 
 
 
 
 
206
  }
207
 
208
+ .layer-bg {
209
  position: absolute;
210
+ inset: 0;
 
 
 
 
211
  background-size: cover;
212
  background-position: center;
213
+ z-index: 1;
214
  }
215
 
216
+ .layer-subject {
217
  position: relative;
218
  z-index: 2;
 
219
  display: block;
220
+ max-height: 80vh;
221
+ max-width: 100%;
222
  }
223
 
224
+ /* Editor: Tools Panel (Right - Floating Style) */
225
+ .tools-panel {
226
+ width: 420px;
 
227
  background: var(--surface);
228
  border-left: 1px solid var(--border);
229
  display: flex;
230
  flex-direction: column;
231
+ z-index: 20;
232
+ box-shadow: -4px 0 24px rgba(0, 0, 0, 0.04);
233
  }
234
 
235
+ .tools-header {
236
  padding: 1.5rem;
237
  border-bottom: 1px solid var(--border);
238
+ display: flex;
239
+ justify-content: space-between;
240
+ align-items: center;
241
  }
242
 
243
+ .tools-title {
244
  font-weight: 700;
245
+ font-size: 1.1rem;
246
+ color: var(--text-main);
247
  margin: 0;
248
  }
249
 
250
+ /* Pills Tab */
251
+ .tabs-pills {
252
  display: flex;
253
+ background: var(--surface-hover);
254
+ padding: 4px;
255
+ border-radius: var(--radius-pill);
256
+ margin: 1rem 1.5rem;
257
  }
258
 
259
+ .pill {
260
+ flex: 1;
261
+ text-align: center;
262
+ padding: 8px;
263
+ font-size: 0.9rem;
264
+ font-weight: 600;
265
+ color: var(--text-sec);
266
+ border-radius: var(--radius-pill);
267
  cursor: pointer;
268
+ transition: all 0.2s;
 
 
 
 
 
269
  }
270
 
271
+ .pill.active {
272
+ background: white;
273
+ color: var(--text-main);
274
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
 
 
 
 
275
  }
276
 
277
+ /* Tools Content */
278
+ .tools-content {
279
  flex: 1;
280
  overflow-y: auto;
281
+ padding: 0 1.5rem 1.5rem 1.5rem;
282
  }
283
 
284
+ .section-label {
285
+ font-size: 0.8rem;
286
+ font-weight: 700;
287
+ color: var(--text-sec);
288
  text-transform: uppercase;
289
  letter-spacing: 0.5px;
290
+ margin: 1.5rem 0 0.75rem 0;
291
+ }
292
+
293
+ /* Search Bar */
294
+ .search-row {
295
+ display: flex;
296
+ gap: 8px;
297
+ margin-bottom: 1rem;
298
+ }
299
+
300
+ .search-input {
301
+ flex: 1;
302
+ background: var(--surface-hover);
303
+ border: 1px solid transparent;
304
+ padding: 10px 16px;
305
+ border-radius: var(--radius-md);
306
+ font-family: inherit;
307
+ }
308
+
309
+ .search-input:focus {
310
+ background: white;
311
+ border-color: var(--primary);
312
+ }
313
+
314
+ .btn-icon {
315
+ background: var(--surface-hover);
316
+ border: none;
317
+ width: 42px;
318
+ border-radius: var(--radius-md);
319
+ cursor: pointer;
320
+ color: var(--text-sec);
321
+ display: flex;
322
+ align-items: center;
323
+ justify-content: center;
324
+ }
325
+
326
+ .btn-icon:hover {
327
+ background: #e2e8f0;
328
+ color: var(--text-main);
329
  }
330
 
331
+ /* Grids */
332
+ .grid-cards {
333
  display: grid;
334
+ grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
335
  gap: 12px;
336
  }
337
 
338
+ .card-opt {
339
+ aspect-ratio: 1;
340
+ border-radius: var(--radius-md);
341
+ overflow: hidden;
342
  cursor: pointer;
343
  border: 2px solid transparent;
 
344
  position: relative;
 
345
  background-size: cover;
346
  background-position: center;
347
+ transition: transform 0.1s;
348
  }
349
 
350
+ .card-opt:hover {
351
+ transform: scale(1.05);
 
352
  }
353
 
354
+ .card-opt.selected {
355
+ border-color: var(--primary);
356
+ box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
 
 
 
 
357
  }
358
 
359
+ .card-opt.upload-pl {
360
+ background: #f1f5f9;
361
  display: flex;
362
+ flex-direction: column;
363
  align-items: center;
364
  justify-content: center;
365
+ color: var(--text-sec);
366
+ font-size: 0.8rem;
367
+ gap: 4px;
368
+ border: 2px dashed #cbd5e1;
369
+ }
370
+
371
+ /* Color Inputs */
372
+ .color-picker-row {
373
+ display: flex;
374
+ gap: 12px;
375
+ align-items: center;
376
+ background: var(--surface-hover);
377
+ padding: 12px;
378
+ border-radius: var(--radius-md);
379
+ margin-bottom: 12px;
380
+ }
381
+
382
+ .cp-thumb {
383
+ width: 32px;
384
+ height: 32px;
385
+ border-radius: 50%;
386
+ border: 2px solid white;
387
+ box-shadow: 0 0 0 1px #cbd5e1;
388
+ overflow: hidden;
389
+ position: relative;
390
+ }
391
+
392
+ input[type="color"] {
393
+ position: absolute;
394
+ inset: -5px;
395
+ width: 150%;
396
+ height: 150%;
397
+ cursor: pointer;
398
+ padding: 0;
399
  }
400
 
401
+ /* Footer Actions */
402
+ .tools-footer {
403
  padding: 1.5rem;
404
  border-top: 1px solid var(--border);
405
  display: flex;
406
  gap: 1rem;
407
+ background: var(--surface);
408
  }
409
 
410
+ .btn-dl {
411
  flex: 1;
412
  background: var(--primary);
413
  color: white;
414
+ padding: 14px;
415
+ border-radius: 50px;
 
416
  font-weight: 600;
417
  border: none;
418
  cursor: pointer;
 
420
  align-items: center;
421
  justify-content: center;
422
  gap: 8px;
423
+ box-shadow: 0 4px 12px rgba(37, 99, 235, 0.25);
424
+ transition: background 0.2s;
425
  }
426
 
427
+ .btn-dl:hover {
428
+ background: var(--primary-hover);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
429
  }
430
 
431
+ .btn-ghost {
432
+ padding: 14px;
433
+ color: var(--text-sec);
434
+ background: transparent;
435
+ font-weight: 600;
436
  border: none;
 
 
437
  cursor: pointer;
438
+ border-radius: 50px;
439
  }
440
 
441
+ .btn-ghost:hover {
442
+ background: var(--surface-hover);
443
+ color: var(--text-main);
 
 
 
444
  }
445
 
446
+ /* Loader */
447
  .loader-overlay {
448
+ position: fixed;
449
+ inset: 0;
450
+ background: rgba(255, 255, 255, 0.9);
451
+ z-index: 100;
452
  display: none;
453
  flex-direction: column;
454
  align-items: center;
455
  justify-content: center;
 
456
  }
457
 
458
+ .spinner {
459
+ width: 56px;
460
+ height: 56px;
461
+ border: 4px solid #e2e8f0;
462
  border-top-color: var(--primary);
463
  border-radius: 50%;
464
+ animation: spin 0.8s linear infinite;
465
  }
466
 
467
  @keyframes spin {
 
470
  }
471
  }
472
 
473
+ @keyframes fadeIn {
474
+ from {
475
+ opacity: 0;
476
+ transform: translateY(10px);
477
+ }
478
+
479
+ to {
480
+ opacity: 1;
481
+ transform: translateY(0);
482
+ }
483
+ }
484
+
485
  /* Mobile */
486
  @media (max-width: 900px) {
487
+ .hero h1 {
488
+ font-size: 2.25rem;
489
+ }
490
+
491
+ .editor-view {
492
  flex-direction: column;
 
493
  }
494
 
495
+ .stage-area {
496
+ flex: 1;
497
+ padding: 1rem;
498
  }
499
 
500
+ .tools-panel {
501
  width: 100%;
502
+ height: 50%;
503
+ border-radius: 20px 20px 0 0;
504
+ box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
505
  }
506
  }
507
  </style>
 
511
 
512
  <nav>
513
  <a href="#" class="brand">
514
+ <span class="material-symbols-rounded">filter_b_and_w</span>
515
  RemoveBG
516
  </a>
517
+ <div style="display: flex; gap: 10px;">
518
+ <button class="btn-icon" title="API Settings" onclick="promptApiKey()">
519
+ <span class="material-symbols-rounded">settings</span>
520
+ </button>
521
+ </div>
522
  </nav>
523
 
524
  <main>
525
 
526
+ <!-- Upload Screen -->
527
+ <div id="uploadView" class="upload-view">
528
+ <div class="hero">
529
+ <h1>Remove Backgrounds<br>100% Automatically</h1>
530
+ <p>Upload an image to get a transparent background in seconds.</p>
531
+ </div>
532
+
533
+ <div class="drop-zone" id="dropZone" onclick="document.getElementById('fileInput').click()">
534
+ <button class="btn-upload">
535
+ <span class="material-symbols-rounded">upload</span>
536
+ Upload Image
537
+ </button>
538
+ <div class="upload-hint">or drop a file here</div>
539
+ </div>
540
+ <input type="file" id="fileInput" accept="image/*" hidden>
541
  </div>
542
 
543
+ <!-- Editor Screen -->
544
+ <div id="editorView" class="editor-view">
545
 
546
+ <div class="stage-area">
547
+ <div class="canvas-wrapper" id="canvasWrapper">
548
+ <div id="bgLayer" class="layer-bg"></div>
549
+ <img id="subjectImg" class="layer-subject" src="" alt="Subject">
 
 
 
 
 
 
 
550
  </div>
 
551
  </div>
552
 
553
+ <div class="tools-panel">
554
+ <div class="tabs-pills">
555
+ <div class="pill active" onclick="switchTab('photo')">Photo</div>
556
+ <div class="pill" onclick="switchTab('color')">Color</div>
557
+ </div>
558
 
559
+ <div class="tools-content" id="toolsContent">
560
+ <!-- Dynamic Content -->
 
 
 
 
 
 
561
  </div>
562
 
563
+ <div class="tools-footer">
564
+ <button class="btn-ghost" onclick="resetApp()">New</button>
565
+ <button class="btn-dl" onclick="downloadResult()">
566
+ <span class="material-symbols-rounded">download</span>
567
+ Download
568
+ </button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
569
  </div>
570
  </div>
571
+ </div>
572
 
573
+ <!-- Loader -->
574
+ <div id="loader" class="loader-overlay">
575
+ <div class="spinner"></div>
576
+ <div style="margin-top: 1rem; font-weight: 500; color: #64748b;">Processing Image...</div>
577
  </div>
578
 
579
  </main>
580
 
581
  <script>
582
+ // State
583
+ const state = {
584
+ currentTab: 'photo',
585
+ bgType: 'none', // none, color, image
586
+ bgValue: '',
587
+ apiKey: 'wCwCTx5e6SExjsLckZoDEyCIK4sNE1rbH6JenhPciPQlbvUDg7FDhChl',
588
+ curatedPhotos: [
589
+ 'https://images.pexels.com/photos/206359/pexels-photo-206359.jpeg?auto=compress&cs=tinysrgb&h=350',
590
+ 'https://images.pexels.com/photos/1761279/pexels-photo-1761279.jpeg?auto=compress&cs=tinysrgb&h=350',
591
+ 'https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&h=350',
592
+ 'https://images.pexels.com/photos/1072179/pexels-photo-1072179.jpeg?auto=compress&cs=tinysrgb&h=350',
593
+ 'https://images.pexels.com/photos/1166209/pexels-photo-1166209.jpeg?auto=compress&cs=tinysrgb&h=350',
594
+ 'https://images.pexels.com/photos/1252869/pexels-photo-1252869.jpeg?auto=compress&cs=tinysrgb&h=350'
595
+ ], // Fallback if no API key
596
+ colors: ['#ffffff', '#f4f4f5', '#e4e4e7', '#fecaca', '#fde68a', '#bbf7d0', '#bfdbfe', '#ddd6fe', '#fbcfe8', '#000000']
597
+ };
598
+
599
  // Elements
 
600
  const uploadView = document.getElementById('uploadView');
601
+ const editorView = document.getElementById('editorView');
602
+ const loader = document.getElementById('loader');
603
  const subjectImg = document.getElementById('subjectImg');
604
  const bgLayer = document.getElementById('bgLayer');
605
+ const toolsContent = document.getElementById('toolsContent');
606
+
607
+ // --- Upload Logic ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
608
  const dropZone = document.getElementById('dropZone');
609
+ const fileInput = document.getElementById('fileInput');
610
+
611
+ dropZone.addEventListener('dragover', e => { e.preventDefault(); dropZone.classList.add('dragover'); });
612
  dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
613
+ dropZone.addEventListener('drop', e => {
614
  e.preventDefault();
615
  dropZone.classList.remove('dragover');
616
+ if (e.dataTransfer.files[0]) handleFile(e.dataTransfer.files[0]);
617
  });
618
+ fileInput.addEventListener('change', e => {
619
+ if (e.target.files[0]) handleFile(e.target.files[0]);
 
620
  });
621
 
622
  function handleFile(file) {
623
+ if (!file.type.startsWith('image/')) return alert('Please upload an image.');
624
 
625
+ showLoader(true);
626
+ const fd = new FormData();
627
+ fd.append('file', file);
628
 
629
+ fetch('/', { method: 'POST', body: fd })
630
+ .then(r => r.blob())
 
 
 
631
  .then(blob => {
632
  const url = URL.createObjectURL(blob);
633
  subjectImg.src = url;
634
+ // Reset State
635
+ state.bgType = 'none';
636
+ state.bgValue = '';
637
+ updatePreview();
638
+ showLoader(false);
639
+ uploadView.style.opacity = 0;
640
+ setTimeout(() => {
641
+ uploadView.style.display = 'none';
642
+ editorView.style.display = 'flex';
643
+ renderTools();
644
+ }, 300);
645
  })
646
+ .catch(err => {
647
+ console.error(err);
648
+ alert('Error removing background');
649
+ showLoader(false);
650
  });
651
  }
652
 
653
+ // --- Editor Logic ---
654
+ function switchTab(tab) {
655
+ state.currentTab = tab;
656
+ document.querySelectorAll('.pill').forEach(p => p.classList.remove('active'));
657
+ document.querySelectorAll('.pill')[tab === 'photo' ? 0 : 1].classList.add('active');
658
+ renderTools();
 
 
659
  }
660
 
661
+ function renderTools() {
662
+ toolsContent.innerHTML = '';
663
+
664
+ if (state.currentTab === 'photo') {
665
+ // Search Bar
666
+ const searchRow = document.createElement('div');
667
+ searchRow.className = 'search-row';
668
+ searchRow.innerHTML = `
669
+ <input type="text" id="pexelsSearch" class="search-input" placeholder="Search backgrounds (e.g. 'Beach')...">
670
+ <button class="btn-icon" onclick="searchPexels()">
671
+ <span class="material-symbols-rounded">search</span>
672
+ </button>
673
+ `;
674
+ toolsContent.appendChild(searchRow);
675
+
676
+ // Enter key
677
+ setTimeout(() => {
678
+ document.getElementById('pexelsSearch').addEventListener('keypress', (e) => {
679
+ if (e.key === 'Enter') searchPexels();
680
+ });
681
+ }, 0);
682
+
683
+ // Upload BG Card
684
+ const uploadCard = document.createElement('div');
685
+ uploadCard.className = 'card-opt upload-pl';
686
+ uploadCard.innerHTML = `<span class="material-symbols-rounded">add_photo_alternate</span> Upload`;
687
+ uploadCard.onclick = () => document.getElementById('bgUploadInput').click();
688
+
689
+ const hiddenInput = document.createElement('input');
690
+ hiddenInput.type = 'file'; hiddenInput.id = 'bgUploadInput'; hiddenInput.accept = 'image/*'; hiddenInput.hidden = true;
691
+ hiddenInput.onchange = (e) => {
692
+ const f = e.target.files[0];
693
+ if (f) {
694
+ const u = URL.createObjectURL(f);
695
+ setBg('image', u);
696
+ }
697
+ };
698
+ toolsContent.appendChild(hiddenInput);
699
 
700
+ const label = document.createElement('div');
701
+ label.className = 'section-label';
702
+ label.innerText = 'Suggestions';
703
+ toolsContent.appendChild(label);
 
 
704
 
705
  const grid = document.createElement('div');
706
+ grid.className = 'grid-cards';
707
+ grid.id = 'photoGrid';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
708
 
709
+ grid.appendChild(uploadCard);
710
+
711
+ // Initial Content (Curated)
712
+ state.curatedPhotos.forEach(src => {
713
  const el = document.createElement('div');
714
+ el.className = 'card-opt';
715
+ el.style.backgroundImage = `url(${src})`;
716
+ el.onclick = () => setBg('image', src.replace('h=350', 'w=1920')); // Try to get hi-res if URL logic permits
717
+ // For Pexels strictly:
718
+ // tiny -> original usually works by replacing query params if we knew them.
719
+ // For static list, we just use the src.
720
+ if (state.bgValue === src) el.classList.add('selected');
 
 
 
721
  grid.appendChild(el);
722
  });
723
+ toolsContent.appendChild(grid);
724
+ }
725
+ else if (state.currentTab === 'color') {
726
+ // Color Picker
727
+ const label = document.createElement('div');
728
+ label.className = 'section-label';
729
+ label.innerText = 'Custom Color';
730
+ toolsContent.appendChild(label);
731
+
732
+ const pickerRow = document.createElement('div');
733
+ pickerRow.className = 'color-picker-row';
734
+
735
+ const thumb = document.createElement('div');
736
+ thumb.className = 'cp-thumb';
737
+ const inp = document.createElement('input');
738
+ inp.type = 'color';
739
+ inp.oninput = (e) => {
740
+ setBg('color', e.target.value);
741
+ thumb.style.background = e.target.value;
 
 
742
  };
743
+ thumb.appendChild(inp);
744
 
745
+ const txt = document.createElement('span');
746
+ txt.innerText = 'Pick any color';
747
+ txt.style.fontWeight = '500';
748
 
749
+ pickerRow.appendChild(thumb);
750
+ pickerRow.appendChild(txt);
751
+ toolsContent.appendChild(pickerRow);
 
 
 
752
 
753
  // Presets
754
+ const label2 = document.createElement('div');
755
+ label2.className = 'section-label';
756
+ label2.innerText = 'Presets';
757
+ toolsContent.appendChild(label2);
 
 
 
 
 
 
 
 
 
 
 
758
 
 
 
759
  const grid = document.createElement('div');
760
+ grid.className = 'grid-cards';
761
+
762
+ // Transparent
763
+ const trans = document.createElement('div');
764
+ trans.className = 'card-opt';
765
+ trans.style.backgroundImage = `linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%), linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%)`;
766
+ trans.style.backgroundSize = '10px 10px';
767
+ trans.title = 'Transparent';
768
+ trans.onclick = () => setBg('none', '');
769
+ if (state.bgType === 'none') trans.classList.add('selected');
770
+ grid.appendChild(trans);
771
+
772
+ state.colors.forEach(c => {
773
+ const el = document.createElement('div');
774
+ el.className = 'card-opt';
775
+ el.style.backgroundColor = c;
776
+ el.onclick = () => setBg('color', c);
777
+ if (state.bgValue === c) el.classList.add('selected');
778
+ grid.appendChild(el);
779
+ });
780
+ toolsContent.appendChild(grid);
 
 
 
781
  }
782
  }
783
 
784
+ async function searchPexels() {
785
+ const q = document.getElementById('pexelsSearch').value;
786
+ if (!q) return;
787
+
788
+ if (!state.apiKey) {
789
+ alert('Pexels API Key missing. Please click the Settings icon to add it, or use the default suggestions.');
790
+ promptApiKey();
791
+ return;
792
+ }
793
+
794
+ const grid = document.getElementById('photoGrid');
795
+ // Keep upload button
796
+ const upBtn = grid.firstElementChild;
797
+ grid.innerHTML = '';
798
+ grid.appendChild(upBtn);
799
+
800
+ // Loader in grid?
801
+ const loading = document.createElement('div'); loading.innerText = 'Searching...';
802
+ grid.appendChild(loading);
803
+
804
+ try {
805
+ const res = await fetch(`https://api.pexels.com/v1/search?query=${encodeURIComponent(q)}&per_page=15`, {
806
+ headers: { 'Authorization': state.apiKey }
807
+ });
808
+ const data = await res.json();
809
+
810
+ grid.removeChild(loading);
811
+
812
+ if (data.photos) {
813
+ data.photos.forEach(p => {
814
+ const el = document.createElement('div');
815
+ el.className = 'card-opt';
816
+ el.style.backgroundImage = `url(${p.src.tiny})`;
817
+ // On click use large
818
+ el.onclick = () => setBg('image', p.src.large2x || p.src.large);
819
+ grid.appendChild(el);
820
+ });
821
  }
822
+ } catch (e) {
823
+ console.error(e);
824
+ alert('Pexels Search Failed. Check API Key.');
825
  }
826
  }
827
 
828
+ function setBg(type, val) {
829
+ state.bgType = type;
830
+ state.bgValue = val;
831
+ updatePreview();
832
+
833
+ // Re-render to update selected border
834
+ renderTools();
835
  }
836
 
837
+ function updatePreview() {
838
+ if (state.bgType === 'none') {
839
+ bgLayer.style.background = 'none';
840
+ } else if (state.bgType === 'color') {
841
+ bgLayer.style.background = state.bgValue;
842
+ } else if (state.bgType === 'image') {
843
+ bgLayer.style.background = 'none';
844
+ bgLayer.style.backgroundImage = `url(${state.bgValue})`;
845
+ }
846
+ }
847
+
848
+ function downloadResult() {
849
  const canvas = document.createElement('canvas');
850
  const ctx = canvas.getContext('2d');
 
851
  canvas.width = subjectImg.naturalWidth;
852
  canvas.height = subjectImg.naturalHeight;
853
 
854
+ // Draw Background
855
+ if (state.bgType === 'color') {
856
+ ctx.fillStyle = state.bgValue;
857
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
858
+ drawSubject(ctx, canvas);
859
+ } else if (state.bgType === 'image') {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
860
  const img = new Image();
861
+ img.crossOrigin = "Anonymous";
862
+ img.src = state.bgValue;
863
  img.onload = () => {
864
+ // Object fit cover
865
  const sRatio = img.width / img.height;
866
  const dRatio = canvas.width / canvas.height;
867
  let sw, sh, sx, sy;
868
  if (sRatio > dRatio) { sh = img.height; sw = sh * dRatio; sx = (img.width - sw) / 2; sy = 0; }
869
  else { sw = img.width; sh = sw / dRatio; sx = 0; sy = (img.height - sh) / 2; }
870
+
871
  ctx.drawImage(img, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
872
+ drawSubject(ctx, canvas);
873
  };
874
+ img.onerror = () => {
875
+ alert('Could not load background image for download (CORS issue?). Try uploading a local file.');
876
+ drawSubject(ctx, canvas); // Download without bg if fail
877
+ }
878
+ } else {
879
+ drawSubject(ctx, canvas);
880
  }
 
 
881
  }
882
 
883
+ function drawSubject(ctx, canvas) {
884
  ctx.drawImage(subjectImg, 0, 0);
885
+ const a = document.createElement('a');
886
+ a.download = 'rembg-edit.png';
887
+ a.href = canvas.toDataURL('image/png');
888
+ a.click();
889
+ }
890
+
891
+ // --- Helpers ---
892
+ function promptApiKey() {
893
+ const cur = localStorage.getItem('pexels_api_key') || '';
894
+ const key = prompt("Enter Pexels API Key for Search (Free at pexels.com/api):", cur);
895
+ if (key !== null) {
896
+ localStorage.setItem('pexels_api_key', key.trim());
897
+ state.apiKey = key.trim();
898
+ }
899
  }
900
+
901
+ function resetApp() {
902
+ uploadView.style.display = 'flex';
903
+ uploadView.style.opacity = 1;
904
+ editorView.style.display = 'none';
905
+ fileInput.value = '';
906
+ subjectImg.src = '';
907
+ state.bgType = 'none';
908
+ state.bgValue = '';
909
+ }
910
+
911
+ function showLoader(show) {
912
+ loader.style.display = show ? 'flex' : 'none';
913
+ }
914
+
915
  </script>
916
  </body>
917