Skip to content

Commit 35898a9

Browse files
committed
Add size options
1 parent 8fd286d commit 35898a9

File tree

3 files changed

+174
-2
lines changed

3 files changed

+174
-2
lines changed

src/app/photo-collage/photo-collage.component.css

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,100 @@ canvas {
141141
.remove-btn:hover {
142142
background: #ff6666;
143143
}
144+
145+
.toggle-btn {
146+
background: rgba(128, 128, 128, 0.9) !important;
147+
}
148+
149+
.toggle-btn.active {
150+
background: rgba(0, 200, 0, 0.9) !important;
151+
}
152+
153+
.toggle-btn:hover {
154+
background: rgba(128, 128, 128, 1) !important;
155+
}
156+
157+
.toggle-btn.active:hover {
158+
background: rgba(0, 200, 0, 1) !important;
159+
}
160+
161+
.save-group {
162+
position: relative;
163+
display: inline-block;
164+
}
165+
166+
.save-group:hover .save-options,
167+
.save-options:hover {
168+
display: flex;
169+
}
170+
171+
.save-options {
172+
display: none;
173+
position: absolute;
174+
bottom: calc(100% + 2px); /* Changed from 5px to 2px for closer proximity */
175+
left: 0;
176+
right: 0;
177+
flex-direction: column;
178+
gap: 5px;
179+
background: rgba(0, 0, 0, 0.8);
180+
padding: 5px;
181+
border-radius: 10px;
182+
padding-bottom: 8px;
183+
margin-bottom: 0; /* Removed margin-bottom */
184+
}
185+
186+
/* Add a small connecting element to visually link the options with the button */
187+
.save-options:after {
188+
content: '';
189+
position: absolute;
190+
bottom: -4px;
191+
left: 50%;
192+
transform: translateX(-50%);
193+
border-left: 4px solid transparent;
194+
border-right: 4px solid transparent;
195+
border-top: 4px solid rgba(0, 0, 0, 0.8);
196+
}
197+
198+
.size-btn {
199+
padding: 5px 10px !important;
200+
font-size: 12px !important;
201+
background: rgba(128, 128, 128, 0.9) !important;
202+
}
203+
204+
.size-btn:hover {
205+
background: rgba(128, 128, 128, 1) !important;
206+
}
207+
208+
.size-btn.active {
209+
background: rgba(0, 123, 255, 0.9) !important;
210+
}
211+
212+
.size-btn.active:hover {
213+
background: rgba(0, 123, 255, 1) !important;
214+
}
215+
216+
.size-label {
217+
color: white;
218+
font-size: 12px;
219+
text-align: center;
220+
}
221+
222+
.size-buttons {
223+
display: flex;
224+
gap: 5px;
225+
justify-content: center;
226+
}
227+
228+
/* Media query for touch devices */
229+
@media (hover: none) {
230+
.save-options {
231+
position: static;
232+
margin: 0 0 10px 0;
233+
}
234+
235+
.save-group {
236+
display: flex;
237+
flex-direction: column;
238+
align-items: stretch;
239+
}
240+
}

src/app/photo-collage/photo-collage.component.html

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,32 @@
4343
style="display: none">
4444
<div class="controls">
4545
<button (click)="fileInput.click()">Add Photos</button>
46-
<button (click)="saveCollage()">Save Collage</button>
46+
<div class="save-group">
47+
<div class="save-options">
48+
<div class="size-label">Size:</div>
49+
<div class="size-buttons">
50+
<button
51+
(click)="setSaveSize('normal')"
52+
[class.active]="saveSize === 'normal'"
53+
class="size-btn">
54+
Normal
55+
</button>
56+
<button
57+
(click)="setSaveSize('medium')"
58+
[class.active]="saveSize === 'medium'"
59+
class="size-btn">
60+
Medium
61+
</button>
62+
<button
63+
(click)="setSaveSize('large')"
64+
[class.active]="saveSize === 'large'"
65+
class="size-btn">
66+
Large
67+
</button>
68+
</div>
69+
</div>
70+
<button (click)="saveCollage()">Save Collage ({{saveSize}})</button>
71+
</div>
4772
<button (click)="resetCollage()">Reset</button>
4873
</div>
4974
</div>

src/app/photo-collage/photo-collage.component.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class PhotoCollageComponent implements AfterViewInit, OnDestroy {
5959
controlsPosition = { x: 0, y: 0 };
6060
private isOnControls = false;
6161
private imageCache: Map<string, HTMLImageElement> = new Map();
62+
saveSize: 'normal' | 'medium' | 'large' = 'medium';
6263

6364
presets: CollagePreset[] = [
6465
{
@@ -211,13 +212,62 @@ export class PhotoCollageComponent implements AfterViewInit, OnDestroy {
211212
}
212213

213214
saveCollage() {
214-
const dataUrl = this.canvas.toDataURL('image/png');
215+
// Adjust scale based on selected size
216+
const scales = {
217+
normal: 2,
218+
medium: 3,
219+
large: 6
220+
};
221+
const scale = scales[this.saveSize];
222+
223+
// Rest of the saveCollage method remains the same
224+
const tempCanvas = document.createElement('canvas');
225+
tempCanvas.width = this.canvas.width * scale;
226+
tempCanvas.height = this.canvas.height * scale;
227+
const tempCtx = tempCanvas.getContext('2d');
228+
229+
if (!tempCtx) return;
230+
231+
// Clear and prepare the temporary canvas
232+
tempCtx.fillStyle = 'white';
233+
tempCtx.fillRect(0, 0, tempCanvas.width, tempCanvas.height);
234+
235+
// Sort photos by z-index
236+
const sortedPhotos = [...this.photos].sort((a, b) => a.zIndex - b.zIndex);
237+
238+
// Apply the same transforms as the main canvas, but scaled up
239+
tempCtx.setTransform(1, 0, 0, 1, 0, 0);
240+
tempCtx.translate(this.panX * scale, this.panY * scale);
241+
tempCtx.scale(this.scale * scale, this.scale * scale);
242+
243+
// Draw each photo at high resolution
244+
for (const photo of sortedPhotos) {
245+
const img = this.imageCache.get(photo.url);
246+
if (!img) continue;
247+
248+
tempCtx.save();
249+
tempCtx.translate(photo.x + photo.width/2, photo.y + photo.height/2);
250+
tempCtx.rotate(photo.rotation);
251+
tempCtx.scale(photo.scale, photo.scale);
252+
tempCtx.drawImage(img, -photo.width/2, -photo.height/2, photo.width, photo.height);
253+
tempCtx.restore();
254+
}
255+
256+
// Reset transform
257+
tempCtx.setTransform(1, 0, 0, 1, 0, 0);
258+
259+
// Create download link
260+
const dataUrl = tempCanvas.toDataURL('image/png');
215261
const link = document.createElement('a');
216262
link.download = 'collage.png';
217263
link.href = dataUrl;
218264
link.click();
219265
}
220266

267+
setSaveSize(size: 'normal' | 'medium' | 'large') {
268+
this.saveSize = size;
269+
}
270+
221271
private handleZoom(event: WheelEvent) {
222272
event.preventDefault();
223273

0 commit comments

Comments
 (0)