veltrixcode commited on
Commit
9e7ba0c
·
verified ·
1 Parent(s): 7efa118

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +169 -57
templates/index.html CHANGED
@@ -284,6 +284,15 @@
284
  padding: 1.5rem;
285
  }
286
 
 
 
 
 
 
 
 
 
 
287
  .swatch-grid {
288
  display: grid;
289
  grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
@@ -358,6 +367,35 @@
358
  font-weight: 500;
359
  }
360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  /* Loading */
362
  .loader-overlay {
363
  display: none;
@@ -559,18 +597,48 @@
559
  document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
560
  const tabs = document.querySelectorAll('.tab');
561
  if (name === 'color') tabs[0].classList.add('active');
562
- else tabs[1].classList.add('active');
563
 
564
  renderSidebar(name);
565
  }
566
 
567
  function renderSidebar(mode) {
568
  tabContent.innerHTML = '';
569
- const grid = document.createElement('div');
570
- grid.className = 'swatch-grid';
571
 
572
  if (mode === 'color') {
573
- // Colors
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
574
  colors.forEach(c => {
575
  const el = document.createElement('div');
576
  el.className = 'swatch';
@@ -585,8 +653,43 @@
585
  if (currentBg.value === c) el.classList.add('selected');
586
  grid.appendChild(el);
587
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
588
 
589
- // Gradients
 
 
 
 
 
 
 
 
 
590
  gradients.forEach(g => {
591
  const el = document.createElement('div');
592
  el.className = 'swatch';
@@ -597,10 +700,15 @@
597
  setBackground('color', g);
598
  }
599
  if (currentBg.value === g) el.classList.add('selected');
600
- grid.appendChild(el);
601
  });
 
 
602
  }
603
  else if (mode === 'image') {
 
 
 
604
  // Upload Btn
605
  const upBtn = document.createElement('div');
606
  upBtn.className = 'swatch upload-btn';
@@ -622,9 +730,8 @@
622
  }
623
  };
624
  grid.appendChild(input);
 
625
  }
626
-
627
- tabContent.appendChild(grid);
628
  }
629
 
630
  function setBackground(type, value) {
@@ -654,41 +761,60 @@
654
  const canvas = document.createElement('canvas');
655
  const ctx = canvas.getContext('2d');
656
 
657
- // Set size to natural image size
658
  canvas.width = subjectImg.naturalWidth;
659
  canvas.height = subjectImg.naturalHeight;
660
 
661
  // 1. Draw Background
662
  if (currentBg.type === 'color' && currentBg.value !== 'transparent') {
663
- // Check if gradient
664
- if (currentBg.value.includes('gradient')) {
665
- // Very basic gradient support (horizontal only as fallback)
666
- // Parsing complex CSS gradients to canvas is hard.
667
- // Try to just fill with the first color or a simple gradient if we were doing this properly.
668
- // Since this is a tutorial app, we will fill with a fallback color if gradient is selected,
669
- // OR we can try to create a simple linear gradient.
670
-
671
- // Let's assume a simple fallback for now to avoid complexity:
672
- // Just use the first color of the gradient or a default.
673
- // OR: we can draw the gradient if we knew the colors.
674
- // Given the list: linear-gradient(to right, #ffecd2 0%, #fcb69f 100%)
675
-
676
- // BETTER APPROACH FOR MVP: just fill white if gradient selected for download,
677
- // or try to extract colors.
678
-
679
- // Let's try to match the first gradient manually for the demo:
680
- if (currentBg.value.includes('#ffecd2')) {
681
- const grd = ctx.createLinearGradient(0, 0, canvas.width, 0);
682
- grd.addColorStop(0, "#ffecd2");
683
- grd.addColorStop(1, "#fcb69f");
684
- ctx.fillStyle = grd;
685
- ctx.fillRect(0, 0, canvas.width, canvas.height);
686
- } else {
687
- // Fallback for other gradients (white)
688
- const grd = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
689
- grd.addColorStop(0, "#acc");
690
- grd.addColorStop(1, "#eee");
691
- ctx.fillStyle = grd;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
692
  ctx.fillRect(0, 0, canvas.width, canvas.height);
693
  }
694
  }
@@ -696,42 +822,28 @@
696
  ctx.fillStyle = currentBg.value;
697
  ctx.fillRect(0, 0, canvas.width, canvas.height);
698
  }
699
- drawSubjectNext(ctx, canvas);
700
  }
701
  else if (currentBg.type === 'image') {
702
  const img = new Image();
703
  img.crossOrigin = 'Anonymous';
704
  img.onload = () => {
705
- // Draw image covering the canvas (object-fit: cover equivalent)
706
  const sRatio = img.width / img.height;
707
  const dRatio = canvas.width / canvas.height;
708
  let sw, sh, sx, sy;
709
-
710
- if (sRatio > dRatio) {
711
- sh = img.height;
712
- sw = sh * dRatio;
713
- sx = (img.width - sw) / 2;
714
- sy = 0;
715
- } else {
716
- sw = img.width;
717
- sh = sw / dRatio;
718
- sx = 0;
719
- sy = (img.height - sh) / 2;
720
- }
721
-
722
  ctx.drawImage(img, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
723
  drawSubjectNext(ctx, canvas);
724
  };
725
  img.src = currentBg.value;
726
- } else {
727
- // Transparent
728
- drawSubjectNext(ctx, canvas);
729
  }
 
 
730
  }
731
 
732
  function drawSubjectNext(ctx, canvas) {
733
  ctx.drawImage(subjectImg, 0, 0);
734
-
735
  const link = document.createElement('a');
736
  link.download = 'rembg-edit.png';
737
  link.href = canvas.toDataURL('image/png');
 
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));
 
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;
 
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';
 
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';
 
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';
 
730
  }
731
  };
732
  grid.appendChild(input);
733
+ tabContent.appendChild(grid);
734
  }
 
 
735
  }
736
 
737
  function setBackground(type, value) {
 
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
  }
 
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');