Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
468 lines (389 loc) · 12.5 KB
/
Copy pathscript.js
File metadata and controls
468 lines (389 loc) · 12.5 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
* Frame Maker Logic
*/
import { presets } from './config.js';
import { createReactiveState } from './state.js';
import {
parseSvgConfig,
applySvgConfig,
getDefaultsFromConfig,
} from './svg-config.js';
import { generateUI } from './ui-generator.js';
import {
photosList,
uploadedPhotos,
loadPhotosFromStorage,
addUploadedPhoto,
} from './photo-storage.js';
import './theme-switcher.js';
import { updateMask } from './scroll-mask-feature.js';
import { resetCanvasZoom } from './canvas-zoom.js';
import './authentication.js';
import { currentUser } from './authentication.js';
/**
* Default state configuration
*/
const defaultState = {
userImage: null, // Will hold the Image object
frameImage: null, // Will hold the SVG Image object
svgContent: null, // Raw SVG string for editing
selectedFrameId: 'opentowork2', // 'hiring', 'opentowork', 'custom'
};
let currentFrameConfig = null;
// Reactive state - automatically calls updateAndDraw on changes
// Properties that should trigger redraw
const renderTriggerProperties = [
'text',
'textColor',
'frameColor',
'textRotation',
'frameRotation',
'fontSize',
'fontFamily',
'textScale',
];
const imageErrorPng = './no-image.png';
const renderPropsSet = new Set(renderTriggerProperties);
const [state, addStatePropListener] = createReactiveState(defaultState);
addStatePropListener(
renderTriggerProperties,
(_propName, _value, _oldValue) => {
updateAndDraw();
},
);
addStatePropListener(['userImage'], drawPhoto);
addStatePropListener(['frameImage'], drawFrame);
addStatePropListener(['userImage'], resetCanvasZoom);
// Canvas setup - separate canvases for photo and frame
const photoCanvas = $photoCanvas;
const frameCanvas = $frameCanvas;
const photoCtx = photoCanvas.getContext('2d');
const frameCtx = frameCanvas.getContext('2d');
const CANVAS_SIZE = 800;
photoCanvas.width = CANVAS_SIZE;
photoCanvas.height = CANVAS_SIZE;
frameCanvas.width = CANVAS_SIZE;
frameCanvas.height = CANVAS_SIZE;
// Drawing functions
function drawImageContain(ctx, canvas, img) {
const canvasAspect = canvas.width / canvas.height;
const imgAspect = img.width / img.height;
let drawWidth;
let drawHeight;
let offsetX;
let offsetY;
if (imgAspect > canvasAspect) {
// Image is wider relative to canvas
drawWidth = canvas.width;
drawHeight = canvas.width / imgAspect;
offsetX = 0;
offsetY = (canvas.height - drawHeight) / 2;
} else {
// Image is taller relative to canvas
drawHeight = canvas.height;
drawWidth = canvas.height * imgAspect;
offsetX = (canvas.width - drawWidth) / 2;
offsetY = 0;
}
ctx.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
}
function drawPhoto() {
photoCtx.clearRect(0, 0, photoCanvas.width, photoCanvas.height);
if (state.userImage) {
drawImageContain(photoCtx, photoCanvas, state.userImage);
}
}
function drawFrame() {
frameCtx.clearRect(0, 0, frameCanvas.width, frameCanvas.height);
if (state.frameImage) {
frameCtx.drawImage(
state.frameImage,
0,
0,
frameCanvas.width,
frameCanvas.height,
);
}
}
function draw() {
drawPhoto();
drawFrame();
}
// Combine both canvases for download with zoom/pan transforms applied
function getDownloadCanvas() {
// Combined canvas for download (hidden)
const downloadCanvas = document.createElement('canvas');
const downloadCtx = downloadCanvas.getContext('2d');
downloadCanvas.width = CANVAS_SIZE;
downloadCanvas.height = CANVAS_SIZE;
downloadCtx.clearRect(0, 0, downloadCanvas.width, downloadCanvas.height);
// Get zoom state from canvas-zoom.js
const zoom = window.canvasZoomControls?.getZoom() || 1;
const offsetX = window.canvasZoomControls?.getOffsetX() || 0;
const offsetY = window.canvasZoomControls?.getOffsetY() || 0;
// Calculate scale factor (CSS transform is relative to display size, canvas is 800x800)
// The preview container displays at ~300px, so we need to scale the offset
const previewContainer = document.getElementById('$profilePreview');
const containerSize = previewContainer?.getBoundingClientRect().width || 300;
const scaleFactor = CANVAS_SIZE / containerSize;
// Apply transform to match the visual preview
downloadCtx.save();
downloadCtx.translate(downloadCanvas.width / 2, downloadCanvas.height / 2);
downloadCtx.translate(offsetX * scaleFactor, offsetY * scaleFactor);
downloadCtx.scale(zoom, zoom);
downloadCtx.translate(-downloadCanvas.width / 2, -downloadCanvas.height / 2);
downloadCtx.drawImage(photoCanvas, 0, 0);
downloadCtx.restore();
// Draw frame without transform (stays static)
downloadCtx.drawImage(frameCanvas, 0, 0);
return downloadCanvas;
}
function updateSVG() {
if (!state.svgContent) return;
const parser = new DOMParser();
const doc = parser.parseFromString(state.svgContent, 'image/svg+xml');
if (!currentFrameConfig) {
currentFrameConfig = parseSvgConfig(doc);
const configDefaults = getDefaultsFromConfig(currentFrameConfig);
Object.entries(configDefaults).forEach(([key, value]) => {
if (state[key] === undefined || state[key] === defaultState[key]) {
state[key] = value;
}
});
const newProps = Array.from(currentFrameConfig.keys()).filter(
(prop) => !renderPropsSet.has(prop),
);
if (newProps.length) {
addStatePropListener(newProps, () => {
updateAndDraw();
});
newProps.forEach((prop) => renderPropsSet.add(prop));
}
if (currentFrameConfig.size > 0) {
generateUI(currentFrameConfig, state, $dynamicControlsContainer);
}
}
applySvgConfig(doc, currentFrameConfig, state);
// Serialize back to string
const serializer = new XMLSerializer();
const newSvgText = serializer.serializeToString(doc.documentElement);
// Create Blob and Image
const blob = new Blob([newSvgText], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
const img = new Image();
img.onload = () => {
state.frameImage = img;
draw();
URL.revokeObjectURL(url);
};
img.src = url;
}
function updateAndDraw() {
if (presets[state.selectedFrameId]?.type === 'svg') {
updateSVG();
} else {
draw();
}
}
// Preset management
function applyPreset(id) {
const preset = presets[id];
if (!preset) return;
currentFrameConfig = null;
state.selectedFrameId = id;
if (preset.type === 'svg') {
// Load SVG
fetch(preset.src)
.then((response) => response.text())
.then((svgText) => {
state.svgContent = svgText;
const parser = new DOMParser();
const doc = parser.parseFromString(svgText, 'image/svg+xml');
currentFrameConfig = parseSvgConfig(doc);
const defaultsFromConfig = getDefaultsFromConfig(currentFrameConfig);
Object.entries(defaultsFromConfig).forEach(([key, value]) => {
state[key] = value;
});
if (preset.text) state.text = preset.text;
if (preset.textColor) state.textColor = preset.textColor;
if (preset.frameColor) state.frameColor = preset.frameColor;
if (currentFrameConfig.size > 0) {
generateUI(currentFrameConfig, state, $dynamicControlsContainer);
const textStyleProps = [];
for (const [name, prop] of currentFrameConfig) {
if (prop.type === 'text') {
textStyleProps.push(`${name}Styles`);
}
}
if (textStyleProps.length > 0) {
addStatePropListener(
textStyleProps,
(_propName, _value, _oldValue) => {
updateAndDraw();
},
);
}
/////////////////////
// mask refreshing
updateMask($dynamicControlsContainer, 'vertical');
/////////////////////
} else {
// todo(vmyshko): no settings? add stub
}
currentFrameConfig = null;
updateSVG();
});
} else {
// Load static image (PNG/JPG)
state.svgContent = null;
const img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = () => {
state.frameImage = img;
draw();
};
img.src = preset.src;
$dynamicControlsContainer.replaceChildren();
const $controlsStub =
$tmplControlsStub.content.firstElementChild.cloneNode(true);
$dynamicControlsContainer.appendChild($controlsStub);
}
}
// Gallery initialization
function initFrameGallery() {
$frameGallery.replaceChildren();
Object.keys(presets).forEach((key) => {
const preset = presets[key];
const $galleryItem =
$tmplGalleryItem.content.firstElementChild.cloneNode(true);
const $img = $galleryItem.querySelector('img');
$img.src = preset.src;
$img.alt = preset.text;
$img.addEventListener('error', (error) => {
console.warn(`Failed to load frame: ${key}`, error);
$img.src = imageErrorPng;
$img.alt = '404';
});
const $radio = $galleryItem.querySelector('input[type=radio]');
$radio.value = key;
$radio.checked = key === state.selectedFrameId;
$galleryItem.dataset.frame = key;
$frameGallery.appendChild($galleryItem);
});
}
function initPhotoGallery() {
$photoGallery.replaceChildren();
photosList.forEach((photoId, index) => {
const $galleryItem =
$tmplGalleryItem.content.firstElementChild.cloneNode(true);
const $img = $galleryItem.querySelector('img');
$img.src = uploadedPhotos[photoId];
$img.addEventListener('error', (error) => {
console.warn(`Failed to load photo: ${photoId}`, error);
$img.src = imageErrorPng;
$img.alt = '404';
});
const $radio = $galleryItem.querySelector('input[type=radio]');
$radio.value = photoId;
if (index === 0) {
$radio.checked = true;
}
$photoGallery.appendChild($galleryItem);
});
}
// Preview overlay toggle
function togglePreviewOverlay() {
// Don't toggle if user was panning the canvas
if (window.canvasZoomControls?.didPan?.()) return;
$profilePreview.classList.toggle('show-preview-overlay');
}
// Event handlers setup
function setupEventListeners() {
// Preview overlay
$profilePreview.addEventListener('click', togglePreviewOverlay);
// Frame Gallery (Delegation)
$frameGallery.addEventListener('change', ({ target }) => {
if (target.type === 'radio') {
applyPreset(target.value);
}
});
// Photo Gallery (Delegation)
$photoGallery.addEventListener('change', ({ target }) => {
if (target.type === 'radio') {
const img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = () => {
state.userImage = img;
};
const photoId = target.value;
img.src = uploadedPhotos[photoId];
}
});
// Image Upload
$uploadPhotoBtn.addEventListener('click', () => {
$fileInput.click();
});
$fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
const img = new Image();
img.onload = () => {
state.userImage = img;
// Generate short ID for uploaded photo
const photoId = `uploaded-${Date.now()}`;
const dataUrl = event.target.result;
// Add to storage
addUploadedPhoto(photoId, dataUrl);
// Rebuild photo gallery
initPhotoGallery();
// Select the newly uploaded photo
const firstRadio = $photoGallery.querySelector('input[type=radio]');
if (firstRadio) {
firstRadio.checked = true;
}
};
img.src = event.target.result;
};
reader.readAsDataURL(file);
}
});
// Download
$downloadBtn.addEventListener('click', () => {
if (!currentUser) {
alert('Please log in or create an account to download the photo.');
return;
}
const link = document.createElement('a');
link.download = 'frame-avatar.png';
link.href = getDownloadCanvas().toDataURL('image/png');
link.click();
});
}
// Load default placeholder image
function loadDefaultImage() {
const placeholderImg = new Image();
placeholderImg.crossOrigin = 'Anonymous';
const firstPhotoId = photosList.at(0);
if (!firstPhotoId) return; // No photos available
placeholderImg.src = uploadedPhotos[firstPhotoId];
placeholderImg.onload = () => {
if (!state.userImage) {
state.userImage = placeholderImg;
}
};
}
// Initialize application
async function init() {
// Load photos from localStorage
loadPhotosFromStorage();
initFrameGallery();
initPhotoGallery();
setupEventListeners();
loadDefaultImage();
// Apply default preset
applyPreset(state.selectedFrameId);
}
// Start the application
init();