phohale commited on
Commit
4e886f8
·
verified ·
1 Parent(s): 5523155

After uploading the image I see the correct preview, but clicking the "2. customize overlays" doesn't change it and when I click any of the download buttons I get the message "Please upload an image first"

Browse files
Files changed (2) hide show
  1. script.js +74 -13
  2. style.css +9 -2
script.js CHANGED
@@ -82,10 +82,10 @@ document.addEventListener('DOMContentLoaded', function() {
82
  previewImage.src = e.target.result;
83
  previewImage.classList.remove('hidden');
84
  placeholderText.classList.add('hidden');
 
85
  };
86
  reader.readAsDataURL(file);
87
  }
88
-
89
  function applyOverlay(type, color) {
90
  // Clear previous overlay
91
  overlayContainer.innerHTML = '';
@@ -101,11 +101,40 @@ document.addEventListener('DOMContentLoaded', function() {
101
  overlay.classList.add(position);
102
  overlay.classList.add(size);
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  overlayContainer.appendChild(overlay);
105
  currentOverlay = overlay;
106
  }
107
-
108
- function updateOverlay() {
109
  if (!currentOverlay) return;
110
 
111
  // Remove all position and size classes
@@ -119,20 +148,52 @@ document.addEventListener('DOMContentLoaded', function() {
119
  currentOverlay.classList.add(positionSelect.value);
120
  currentOverlay.classList.add(sizeSelect.value);
121
  }
122
-
123
  function downloadImage(format) {
124
- if (!previewImage.src || previewImage.src.startsWith('data:')) {
125
  alert('Please upload an image first');
126
  return;
127
  }
128
 
129
- // In a real implementation, we would use canvas to combine the image and overlay
130
- // and then trigger a download. This is a simplified version.
131
- const link = document.createElement('a');
132
- link.href = previewImage.src;
133
- link.download = `icon-with-overlay.${format}`;
134
- document.body.appendChild(link);
135
- link.click();
136
- document.body.removeChild(link);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  }
138
  });
 
82
  previewImage.src = e.target.result;
83
  previewImage.classList.remove('hidden');
84
  placeholderText.classList.add('hidden');
85
+ currentFile = e.target.result; // Store the data URL
86
  };
87
  reader.readAsDataURL(file);
88
  }
 
89
  function applyOverlay(type, color) {
90
  // Clear previous overlay
91
  overlayContainer.innerHTML = '';
 
101
  overlay.classList.add(position);
102
  overlay.classList.add(size);
103
 
104
+ // Add visual feedback
105
+ const preview = document.createElement('div');
106
+ preview.style.position = 'absolute';
107
+ preview.style.borderRadius = type === 'badge' ? '50%' : '0';
108
+
109
+ // Set color
110
+ preview.style.backgroundColor = color === 'red' ? '#ef4444' :
111
+ color === 'blue' ? '#3b82f6' :
112
+ color === 'green' ? '#10b981' : '#eab308';
113
+
114
+ // Set size and position based on selections
115
+ if (type === 'banner') {
116
+ preview.style.width = '100%';
117
+ preview.style.height = size === 'small' ? '10%' :
118
+ size === 'medium' ? '15%' : '20%';
119
+ preview.style.top = position.includes('bottom') ? 'auto' : '0';
120
+ preview.style.bottom = position.includes('bottom') ? '0' : 'auto';
121
+ } else { // badge
122
+ const badgeSize = size === 'small' ? '20%' :
123
+ size === 'medium' ? '25%' : '30%';
124
+ preview.style.width = badgeSize;
125
+ preview.style.height = badgeSize;
126
+
127
+ if (position.includes('top')) preview.style.top = '0';
128
+ if (position.includes('bottom')) preview.style.bottom = '0';
129
+ if (position.includes('left')) preview.style.left = '0';
130
+ if (position.includes('right')) preview.style.right = '0';
131
+ }
132
+
133
+ overlay.appendChild(preview);
134
  overlayContainer.appendChild(overlay);
135
  currentOverlay = overlay;
136
  }
137
+ function updateOverlay() {
 
138
  if (!currentOverlay) return;
139
 
140
  // Remove all position and size classes
 
148
  currentOverlay.classList.add(positionSelect.value);
149
  currentOverlay.classList.add(sizeSelect.value);
150
  }
 
151
  function downloadImage(format) {
152
+ if (!currentFile) {
153
  alert('Please upload an image first');
154
  return;
155
  }
156
 
157
+ // Create canvas to combine image and overlay
158
+ const canvas = document.createElement('canvas');
159
+ const ctx = canvas.getContext('2d');
160
+ const img = new Image();
161
+
162
+ img.onload = function() {
163
+ canvas.width = img.width;
164
+ canvas.height = img.height;
165
+ ctx.drawImage(img, 0, 0);
166
+
167
+ // Draw overlay if exists
168
+ if (currentOverlay) {
169
+ const overlayColor = currentOverlay.classList.contains('red') ? 'rgba(239, 68, 68, 0.7)' :
170
+ currentOverlay.classList.contains('blue') ? 'rgba(59, 130, 246, 0.7)' :
171
+ currentOverlay.classList.contains('green') ? 'rgba(16, 185, 129, 0.7)' :
172
+ 'rgba(234, 179, 8, 0.7)';
173
+
174
+ ctx.fillStyle = overlayColor;
175
+
176
+ // Draw based on overlay type
177
+ if (currentOverlay.classList.contains('banner')) {
178
+ const height = canvas.height * 0.1;
179
+ ctx.fillRect(0, 0, canvas.width, height);
180
+ } else if (currentOverlay.classList.contains('badge')) {
181
+ const size = canvas.width * 0.2;
182
+ ctx.beginPath();
183
+ ctx.arc(canvas.width - size/2, size/2, size/2, 0, Math.PI * 2);
184
+ ctx.fill();
185
+ }
186
+ }
187
+
188
+ // Trigger download
189
+ const link = document.createElement('a');
190
+ link.download = `icon-with-overlay.${format}`;
191
+ link.href = canvas.toDataURL(`image/${format}`);
192
+ document.body.appendChild(link);
193
+ link.click();
194
+ document.body.removeChild(link);
195
+ };
196
+
197
+ img.src = currentFile;
198
  }
199
  });
style.css CHANGED
@@ -37,10 +37,17 @@ body {
37
  border-color: #3b82f6;
38
  background-color: #eff6ff;
39
  }
40
-
41
  /* Responsive adjustments */
42
  @media (max-width: 640px) {
43
  .grid-cols-2 {
44
  grid-template-columns: 1fr;
45
  }
46
- }
 
 
 
 
 
 
 
 
 
37
  border-color: #3b82f6;
38
  background-color: #eff6ff;
39
  }
 
40
  /* Responsive adjustments */
41
  @media (max-width: 640px) {
42
  .grid-cols-2 {
43
  grid-template-columns: 1fr;
44
  }
45
+ }
46
+
47
+ /* Overlay styles */
48
+ .overlay {
49
+ position: absolute;
50
+ pointer-events: none;
51
+ width: 100%;
52
+ height: 100%;
53
+ }