Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
939 lines (798 loc) · 35.5 KB
/
Copy pathscript.js
File metadata and controls
939 lines (798 loc) · 35.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
// State
let currentLayout = 'b';
// Palettes
const palettes = {
cool: ['#0f172a', '#38bdf8', '#e0f2fe', '#38bdf8', '#06b6d4', '#ffffff'],
indigo: ['#1e1b4b', '#6366f1', '#e0e7ff', '#6366f1', '#8b5cf6', '#ffffff'],
cyan: ['#164e63', '#67e8f9', '#cffafe', '#06b6d4', '#0891b2', '#ffffff'],
teal: ['#134e4a', '#5eead4', '#f0fdfa', '#14b8a6', '#0d9488', '#ffffff'],
rose: ['#4c1d24', '#fb7185', '#fff1f2', '#f43f5e', '#e11d48', '#ffffff'],
amber: ['#451a03', '#fbbf24', '#fffbeb', '#f59e0b', '#d97706', '#1f2937'],
purple: ['#2d1b69', '#a855f7', '#faf5ff', '#a855f7', '#9333ea', '#ffffff'],
sunset: ['#431407', '#fb923c', '#fff7ed', '#f97316', '#ea580c', '#ffffff'],
emerald: ['#064e3b', '#6ee7b7', '#ecfdf5', '#10b981', '#059669', '#ffffff'],
crimson: ['#450a0a', '#f87171', '#fef2f2', '#dc2626', '#b91c1c', '#ffffff'],
ocean: ['#0c4a6e', '#7dd3fc', '#f0f9ff', '#0284c7', '#0369a1', '#ffffff'],
forest: ['#14532d', '#86efac', '#f0fdf4', '#16a34a', '#15803d', '#ffffff'],
neon: ['#0a0a0a', '#00ff88', '#00ff88', '#ff0080', '#00ddff', '#000000'],
gold: ['#451a03', '#fcd34d', '#1f2937', '#eab308', '#ca8a04', '#1f2937'],
midnight: ['#0f0f23', '#818cf8', '#e2e8f0', '#4f46e5', '#3730a3', '#ffffff'],
volcano: ['#7c2d12', '#fb923c', '#fef2f2', '#f97316', '#dc2626', '#ffffff'],
// High-contrast and emotional trigger presets
urgent: ['#000000', '#ff4444', '#ffdddd', '#ff0000', '#ff4444', '#ffffff'],
shock: ['#ffffff', '#000000', '#000000', '#ff0080', '#ff0040', '#ffffff'],
warning: ['#1a1a00', '#ffff00', '#000000', '#ffff00', '#ffcc00', '#000000'],
success: ['#003300', '#00ff00', '#000000', '#00ff00', '#00cc00', '#000000'],
viral: ['#ff00ff', '#00ffff', '#ffffff', '#00ffff', '#0099ff', '#000000'],
mystery: ['#0a0a0a', '#8000ff', '#f3e8ff', '#8000ff', '#6600cc', '#ffffff']
};
// Elements
const titleInput = document.getElementById('titleInput');
const subtitleInput = document.getElementById('subtitleInput');
const tagInput = document.getElementById('tagInput');
const brandInput = document.getElementById('brandInput');
const layoutSelect = document.getElementById('layoutSelect');
const fontSizeSlider = document.getElementById('fontSizeSlider');
const fontSizeValue = document.getElementById('fontSizeValue');
const textRotationSlider = document.getElementById('textRotationSlider');
const textRotationValue = document.getElementById('textRotationValue');
// Visual elements
const visualElementCheckboxes = {
arrow: document.getElementById('showArrow'),
star: document.getElementById('showStar'),
checkmark: document.getElementById('showCheckmark'),
exclamation: document.getElementById('showExclamation'),
fire: document.getElementById('showFire'),
lightning: document.getElementById('showLightning')
};
const elementSizeSlider = document.getElementById('elementSizeSlider');
const elementSizeValue = document.getElementById('elementSizeValue');
// Drama elements
const showDrama = document.getElementById('showDrama');
const showTextShadow = document.getElementById('showTextShadow');
// Background image elements
const backgroundUpload = document.getElementById('backgroundUpload');
const removeBackground = document.getElementById('removeBackground');
const backgroundOpacity = document.getElementById('backgroundOpacity');
const backgroundOpacityValue = document.getElementById('backgroundOpacityValue');
const enableTextBands = document.getElementById('enableTextBands');
const showMobilePreview = document.getElementById('showMobilePreview');
// Color inputs
const colorInputs = {
bg0: document.getElementById('bg0Color'),
bg1: document.getElementById('bg1Color'),
primary: document.getElementById('primaryColor'),
accent: document.getElementById('accentColor'),
pill: document.getElementById('pillColor'),
tagText: document.getElementById('tagTextColor')
};
// Apply CSS variables
function applyCSSVariables() {
const root = document.documentElement;
Object.entries(colorInputs).forEach(([key, input]) => {
root.style.setProperty(`--${key}`, input.value);
});
}
// Get text width
function getTextWidth(text, fontSize, fontWeight = 'normal') {
const svg = document.querySelector('#thumb');
const textEl = document.createElementNS('http://www.w3.org/2000/svg', 'text');
textEl.setAttribute('font-size', fontSize);
textEl.setAttribute('font-weight', fontWeight);
textEl.setAttribute('font-family', 'system-ui');
textEl.setAttribute('visibility', 'hidden');
textEl.textContent = text;
svg.appendChild(textEl);
const width = textEl.getComputedTextLength();
svg.removeChild(textEl);
return width;
}
// Auto-fit title
function autofitTitle(layout) {
const titleEl = document.getElementById(`title-${layout}`);
const subtitleEl = document.getElementById(`subtitle-${layout}`);
// Exit early if elements don't exist
if (!titleEl) return;
const title = titleInput.value;
let fontSize;
if (layout === 'b') fontSize = 80 + currentFontSizeOffset;
else if (layout === 'd') fontSize = 76 + currentFontSizeOffset;
else fontSize = 72 + currentFontSizeOffset;
const minFontSize = 24;
const maxFontSize = 120;
const maxWidth = layout === 'b' || layout === 'd' ? 920 : 1120;
// Apply font size constraints
fontSize = Math.max(minFontSize, Math.min(maxFontSize, fontSize));
// Auto-fit to width
while (fontSize > minFontSize) {
const width = getTextWidth(title, fontSize, '700');
if (width <= maxWidth) break;
fontSize -= 2;
}
titleEl.setAttribute('font-size', fontSize);
// Calculate proportional subtitle size (only if subtitle element exists)
if (subtitleEl) {
const subtitleBaseFontSize = layout === 'b' ? 36 : 32;
const subtitleFontSize = Math.max(16, Math.min(60, subtitleBaseFontSize + (currentFontSizeOffset * 0.45)));
subtitleEl.setAttribute('font-size', subtitleFontSize);
}
// Update underline width for Layout B
if (layout === 'b') {
const underline = document.getElementById('titleUnderline-b');
if (underline) {
const titleWidth = getTextWidth(title, fontSize, '700');
const underlineWidth = Math.min(Math.max(titleWidth, 200), 760);
const underlineX = 580 - underlineWidth / 2;
underline.setAttribute('x', underlineX);
underline.setAttribute('width', underlineWidth);
}
}
}
// Update pill size and position
function updatePill(layout) {
const tag = tagInput.value || 'TAG';
const pillEl = document.getElementById(`tagPill-${layout}`);
const tagEl = document.getElementById(`tag-${layout}`);
// Exit early if elements don't exist
if (!pillEl || !tagEl) return;
// Measure text - for longer text, use smaller font size
const maxWidth = 300; // Maximum pill width to avoid overlapping content
let fontSize = 18;
let fontWeight = '600';
// Dynamically adjust font size for long text
let textWidth = getTextWidth(tag, fontSize, fontWeight);
if (textWidth > maxWidth - 32) { // Account for padding
fontSize = 16;
textWidth = getTextWidth(tag, fontSize, fontWeight);
if (textWidth > maxWidth - 32) {
fontSize = 14;
textWidth = getTextWidth(tag, fontSize, fontWeight);
}
}
const pillWidth = Math.min(textWidth + 32, maxWidth); // 16px padding on each side
const pillHeight = 40;
const pillX = 1160 - pillWidth; // Right-align to safe area
const pillY = 40;
// Update pill
pillEl.setAttribute('x', pillX);
pillEl.setAttribute('width', pillWidth);
// Update text font size
tagEl.setAttribute('font-size', fontSize);
// Center text in pill
const textX = pillX + pillWidth / 2;
const textY = pillY + pillHeight / 2;
tagEl.setAttribute('x', textX);
tagEl.setAttribute('y', textY);
tagEl.textContent = tag;
}
// Update layout visibility
function updateLayoutVisibility() {
['a', 'b', 'c', 'd'].forEach(layout => {
const el = document.getElementById(`layout-${layout}`);
el.style.display = layout === currentLayout ? 'block' : 'none';
});
}
// Update content
function updateContent() {
const title = titleInput.value;
const subtitle = subtitleInput.value;
const brand = brandInput.value;
const tag = tagInput.value;
['a', 'b', 'c', 'd'].forEach(layout => {
// Update title
const titleEl = document.getElementById(`title-${layout}`);
if (titleEl) titleEl.textContent = title;
// Update subtitle (except for layout D which has special handling)
if (layout !== 'd') {
const subtitleEl = document.getElementById(`subtitle-${layout}`);
if (subtitleEl) subtitleEl.textContent = subtitle;
}
// Update brand
const brandEl = document.getElementById(`brand-${layout}`);
if (brandEl) brandEl.textContent = brand;
// Update tag/pill text
const tagEl = document.getElementById(`tag-${layout}`);
if (tagEl) tagEl.textContent = tag || 'TUTORIAL';
});
// Special handling for Layout D subtitle (urgency message)
const subtitleDEl = document.getElementById('subtitle-d');
if (subtitleDEl) {
if (subtitle.trim()) {
subtitleDEl.textContent = subtitle.toUpperCase() + ' - DON\'T MISS OUT!';
} else {
subtitleDEl.textContent = 'WATCH BEFORE IT\'S TOO LATE!';
}
}
}
// Main update function
function update() {
applyCSSVariables();
updateContent();
updateLayoutVisibility();
// Update title sizing and pill for current layout
autofitTitle(currentLayout);
updatePill(currentLayout);
updateVisualElements();
updateCharacterCounters();
updateDramaEffects();
updateTextVisibility();
updateTextRotation();
updateMobilePreview();
}
// Text rotation function
function updateTextRotation() {
const rotation = textRotationSlider.value;
['a', 'b', 'c', 'd'].forEach(layout => {
const titleElement = document.getElementById(`title-${layout}`);
const subtitleElement = document.getElementById(`subtitle-${layout}`);
if (rotation == 0) {
// Remove rotation when angle is 0
titleElement.removeAttribute('transform');
if (subtitleElement) {
subtitleElement.removeAttribute('transform');
}
} else {
// Get current position for rotation center
const titleX = titleElement.getAttribute('x') || 0;
const titleY = titleElement.getAttribute('y') || 0;
// Apply rotation around text center
titleElement.setAttribute('transform', `rotate(${rotation} ${titleX} ${titleY})`);
if (subtitleElement) {
const subtitleX = subtitleElement.getAttribute('x') || 0;
const subtitleY = subtitleElement.getAttribute('y') || 0;
subtitleElement.setAttribute('transform', `rotate(${rotation} ${subtitleX} ${subtitleY})`);
}
}
});
// Update the rotation value display
textRotationValue.textContent = rotation;
}
// Mobile preview sync function
function updateMobilePreview() {
if (showMobilePreview.checked) {
const mainSvg = document.getElementById('thumb');
const mobileSvg = document.getElementById('thumbMobile');
// Clone the main SVG content to mobile SVG
mobileSvg.innerHTML = mainSvg.innerHTML;
}
}
// Toggle mobile preview visibility
function toggleMobilePreview() {
const mobileWrapper = document.querySelector('.mobile-preview');
const mobileLabel = document.querySelector('.mobile-label');
const desktopLabel = document.querySelector('.desktop-label');
const canvasContainer = document.querySelector('.canvas-container');
if (showMobilePreview.checked) {
mobileWrapper.style.display = 'block';
mobileLabel.style.display = 'inline';
desktopLabel.textContent = 'Desktop Preview';
canvasContainer.classList.add('mobile-preview-active');
updateMobilePreview();
} else {
mobileWrapper.style.display = 'none';
mobileLabel.style.display = 'none';
desktopLabel.textContent = 'Desktop Preview';
canvasContainer.classList.remove('mobile-preview-active');
}
}
// Export functions
function exportSVG() {
// Ensure everything is up to date before export
update();
const svg = document.getElementById('thumb');
const svgClone = svg.cloneNode(true);
// Apply current CSS variables
const root = document.documentElement;
const cssVars = {
'--bg0': root.style.getPropertyValue('--bg0') || colorInputs.bg0.value,
'--bg1': root.style.getPropertyValue('--bg1') || colorInputs.bg1.value,
'--primary': root.style.getPropertyValue('--primary') || colorInputs.primary.value,
'--accent': root.style.getPropertyValue('--accent') || colorInputs.accent.value,
'--pill': root.style.getPropertyValue('--pill') || colorInputs.pill.value,
'--tagText': root.style.getPropertyValue('--tagText') || colorInputs.tagText.value
};
// Handle background image properly in the cloned SVG
const originalBgImage = svg.querySelector('#backgroundImage');
const clonedBgImage = svgClone.querySelector('#backgroundImage');
if (originalBgImage && clonedBgImage && originalBgImage.getAttribute('href')) {
clonedBgImage.setAttribute('href', originalBgImage.getAttribute('href'));
clonedBgImage.style.display = originalBgImage.style.display;
clonedBgImage.style.opacity = originalBgImage.style.opacity;
}
// Create style element
const style = document.createElementNS('http://www.w3.org/2000/svg', 'style');
const cssVarString = Object.entries(cssVars)
.map(([key, value]) => `${key}: ${value}`)
.join('; ');
style.textContent = `:root { ${cssVarString}; }`;
// Insert style into defs
const defs = svgClone.querySelector('defs');
if (defs) {
defs.insertBefore(style, defs.firstChild);
}
// Serialize and replace CSS variables as fallback
const serializer = new XMLSerializer();
let svgString = serializer.serializeToString(svgClone);
Object.entries(cssVars).forEach(([variable, value]) => {
const regex = new RegExp(`var\\(${variable.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\)`, 'g');
svgString = svgString.replace(regex, value);
});
// Ensure proper namespace
if (!svgString.includes('xmlns="http://www.w3.org/2000/svg"')) {
svgString = svgString.replace('<svg', '<svg xmlns="http://www.w3.org/2000/svg"');
}
const blob = new Blob([svgString], { type: 'image/svg+xml;charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'thumbnail.svg';
a.click();
URL.revokeObjectURL(url);
}
function exportPNG() {
// Ensure everything is up to date before export
update();
const svg = document.getElementById('thumb');
const canvas = document.createElement('canvas');
canvas.width = 1280;
canvas.height = 720;
const ctx = canvas.getContext('2d');
// Clone the SVG to avoid modifying the original
const svgClone = svg.cloneNode(true);
// Apply current CSS variables directly to the cloned SVG
const root = document.documentElement;
const cssVars = {
'--bg0': root.style.getPropertyValue('--bg0') || colorInputs.bg0.value,
'--bg1': root.style.getPropertyValue('--bg1') || colorInputs.bg1.value,
'--primary': root.style.getPropertyValue('--primary') || colorInputs.primary.value,
'--accent': root.style.getPropertyValue('--accent') || colorInputs.accent.value,
'--pill': root.style.getPropertyValue('--pill') || colorInputs.pill.value,
'--tagText': root.style.getPropertyValue('--tagText') || colorInputs.tagText.value
};
// Handle background image properly in the cloned SVG
const originalBgImage = svg.querySelector('#backgroundImage');
const clonedBgImage = svgClone.querySelector('#backgroundImage');
if (originalBgImage && clonedBgImage && originalBgImage.getAttribute('href')) {
clonedBgImage.setAttribute('href', originalBgImage.getAttribute('href'));
clonedBgImage.style.display = originalBgImage.style.display;
clonedBgImage.style.opacity = originalBgImage.style.opacity;
}
// Create a style element with CSS variables
const style = document.createElementNS('http://www.w3.org/2000/svg', 'style');
style.textContent = `
:root {
--bg0: ${cssVars['--bg0']};
--bg1: ${cssVars['--bg1']};
--primary: ${cssVars['--primary']};
--accent: ${cssVars['--accent']};
--pill: ${cssVars['--pill']};
--tagText: ${cssVars['--tagText']};
}
`;
// Insert style at the beginning of defs
const defs = svgClone.querySelector('defs');
if (defs) {
defs.insertBefore(style, defs.firstChild);
}
// Serialize the modified SVG
const serializer = new XMLSerializer();
let svgString = serializer.serializeToString(svgClone);
// Replace CSS variables with actual values as fallback
Object.entries(cssVars).forEach(([variable, value]) => {
const regex = new RegExp(`var\\(${variable.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\)`, 'g');
svgString = svgString.replace(regex, value);
});
// Ensure proper SVG namespace and encoding
if (!svgString.includes('xmlns="http://www.w3.org/2000/svg"')) {
svgString = svgString.replace('<svg', '<svg xmlns="http://www.w3.org/2000/svg"');
}
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = function() {
// Clear canvas with white background
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, 1280, 720);
// Draw the SVG
ctx.drawImage(img, 0, 0, 1280, 720);
canvas.toBlob(function(blob) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'thumbnail.png';
a.click();
URL.revokeObjectURL(url);
}, 'image/png', 1.0);
};
img.onerror = function(error) {
console.error('Error loading SVG for PNG export:', error);
alert('Error exporting PNG. Please try again.');
};
// Create blob and load image
const blob = new Blob([svgString], { type: 'image/svg+xml;charset=utf-8' });
const url = URL.createObjectURL(blob);
img.src = url;
// Clean up URL after a delay
setTimeout(() => {
URL.revokeObjectURL(url);
}, 1000);
}
// Font size adjustment
let currentFontSizeOffset = 0;
const maxFontSizeOffset = 20;
const minFontSizeOffset = -20;
function adjustFontSize(delta) {
currentFontSizeOffset = Math.max(minFontSizeOffset, Math.min(maxFontSizeOffset, currentFontSizeOffset + delta));
fontSizeSlider.value = currentFontSizeOffset;
updateFontSizeDisplay();
update(); // Instant update
}
function adjustRotation(delta) {
const currentRotation = parseInt(textRotationSlider.value);
const newRotation = Math.max(-15, Math.min(15, currentRotation + delta));
textRotationSlider.value = newRotation;
update(); // Instant update
}
function setFontSizeOffset(value) {
currentFontSizeOffset = parseInt(value);
updateFontSizeDisplay();
}
function updateFontSizeDisplay() {
fontSizeValue.textContent = currentFontSizeOffset > 0 ? `+${currentFontSizeOffset}` : currentFontSizeOffset;
}
// Character counter functions
function updateCharacterCounters() {
updateCounter('titleInput', 'titleCounter', 100, 80);
updateCounter('subtitleInput', 'subtitleCounter', 100, 80);
updateCounter('tagInput', 'tagCounter', 64, 48);
}
function updateCounter(inputId, counterId, maxChars, warningThreshold) {
const input = document.getElementById(inputId);
const counter = document.getElementById(counterId);
const length = input.value.length;
counter.textContent = `(${length}/${maxChars})`;
counter.className = 'char-counter';
if (length >= maxChars) {
counter.classList.add('danger');
} else if (length >= warningThreshold) {
counter.classList.add('warning');
}
}
// Visual elements functions
function updateVisualElements() {
const elementSize = parseInt(elementSizeSlider.value);
elementSizeValue.textContent = elementSize;
['a', 'b', 'c', 'd'].forEach(layout => {
Object.keys(visualElementCheckboxes).forEach(elementType => {
const element = document.getElementById(`${elementType}-${layout}`);
const checkbox = visualElementCheckboxes[elementType];
if (element && checkbox) {
element.style.display = checkbox.checked ? 'block' : 'none';
// Apply size scaling
const baseFontSize = layout === 'b' ? 100 : layout === 'd' ? 120 : layout === 'a' ? 80 : 90;
const scaledSize = Math.round(baseFontSize * (elementSize / 100));
element.setAttribute('font-size', scaledSize);
}
});
});
}
// Drama effects functions
function updateDramaEffects() {
// Toggle dramatic overlay
const overlay = document.getElementById('dramaticOverlay');
if (overlay) {
overlay.style.display = showDrama.checked ? 'block' : 'none';
}
// Toggle text shadows
const titles = document.querySelectorAll('.main-title');
titles.forEach(title => {
if (showTextShadow.checked) {
title.setAttribute('filter', 'url(#textShadow)');
} else {
title.removeAttribute('filter');
}
});
}
// Background image functions
function handleBackgroundUpload(event) {
const file = event.target.files[0];
if (file && file.type.startsWith('image/')) {
const reader = new FileReader();
reader.onload = function(e) {
const backgroundImage = document.getElementById('backgroundImage');
const backgroundOverlay = document.getElementById('backgroundOverlay');
backgroundImage.setAttribute('href', e.target.result);
backgroundImage.style.display = 'block';
backgroundOverlay.style.display = 'block';
removeBackground.style.display = 'block';
updateTextVisibility();
update();
};
reader.readAsDataURL(file);
}
}
function removeBackgroundImage() {
const backgroundImage = document.getElementById('backgroundImage');
const backgroundOverlay = document.getElementById('backgroundOverlay');
backgroundImage.style.display = 'none';
backgroundImage.removeAttribute('href');
backgroundOverlay.style.display = 'none';
removeBackground.style.display = 'none';
backgroundUpload.value = '';
updateTextVisibility();
update();
}
function updateBackgroundOpacity() {
const opacity = backgroundOpacity.value / 100;
const backgroundImage = document.getElementById('backgroundImage');
const backgroundOverlay = document.getElementById('backgroundOverlay');
backgroundOpacityValue.textContent = backgroundOpacity.value;
if (backgroundImage) {
backgroundImage.style.opacity = opacity;
}
// Adjust overlay opacity inversely for better text visibility
const overlayOpacity = Math.max(0.2, (100 - backgroundOpacity.value) / 200);
if (backgroundOverlay) {
backgroundOverlay.setAttribute('fill', `rgba(0,0,0,${overlayOpacity})`);
}
}
function updateTextVisibility() {
const textElements = document.querySelectorAll('text');
const hasBackground = document.getElementById('backgroundImage').style.display !== 'none';
textElements.forEach(element => {
// Remove any existing filters and strokes
element.removeAttribute('filter');
element.removeAttribute('stroke');
element.removeAttribute('stroke-width');
});
updateTextBands();
}
function updateTextBands() {
const layouts = ['a', 'b', 'c', 'd'];
const hasBackground = document.getElementById('backgroundImage').style.display !== 'none';
const showBands = enableTextBands.checked && hasBackground;
layouts.forEach(layout => {
// Title bands
const titleBand = document.getElementById(`titleBand-${layout}`);
if (titleBand) {
titleBand.style.display = showBands ? 'block' : 'none';
}
// Subtitle bands
const subtitleBand = document.getElementById(`subtitleBand-${layout}`);
if (subtitleBand) {
subtitleBand.style.display = showBands ? 'block' : 'none';
}
// Brand bands
const brandBand = document.getElementById(`brandBand-${layout}`);
if (brandBand) {
brandBand.style.display = showBands ? 'block' : 'none';
}
});
// Update band sizes dynamically based on text content
updateBandSizes();
}
function updateBandSizes() {
const layouts = ['a', 'b', 'c', 'd'];
layouts.forEach(layout => {
// Update title band size
const titleText = document.getElementById(`title-${layout}`);
const titleBand = document.getElementById(`titleBand-${layout}`);
if (titleText && titleBand && titleBand.style.display !== 'none') {
const textWidth = getTextWidth(titleText.textContent, titleText.getAttribute('font-size'), '700');
const padding = 40;
if (layout === 'b' || layout === 'd') {
// Centered layouts
const bandWidth = Math.min(textWidth + padding, 1000);
const bandX = 580 - bandWidth / 2;
titleBand.setAttribute('x', bandX);
titleBand.setAttribute('width', bandWidth);
} else {
// Left-aligned layouts
const bandWidth = Math.min(textWidth + padding, 800);
titleBand.setAttribute('width', bandWidth);
}
}
// Update subtitle band size
const subtitleText = document.getElementById(`subtitle-${layout}`);
const subtitleBand = document.getElementById(`subtitleBand-${layout}`);
if (subtitleText && subtitleBand && subtitleBand.style.display !== 'none') {
const textWidth = getTextWidth(subtitleText.textContent, subtitleText.getAttribute('font-size'), 'normal');
const padding = 30;
if (layout === 'b' || layout === 'd') {
// Centered layouts
const bandWidth = Math.min(textWidth + padding, 800);
const bandX = 580 - bandWidth / 2;
subtitleBand.setAttribute('x', bandX);
subtitleBand.setAttribute('width', bandWidth);
} else {
// Left-aligned layouts
const bandWidth = Math.min(textWidth + padding, 600);
subtitleBand.setAttribute('width', bandWidth);
}
}
// Update brand band size
const brandText = document.getElementById(`brand-${layout}`);
const brandBand = document.getElementById(`brandBand-${layout}`);
if (brandText && brandBand && brandBand.style.display !== 'none') {
const textWidth = getTextWidth(brandText.textContent, brandText.getAttribute('font-size'), 'normal');
const padding = 20;
const bandWidth = Math.min(textWidth + padding, 300);
if (layout === 'b' || layout === 'd') {
// Centered layouts
const bandX = 580 - bandWidth / 2;
brandBand.setAttribute('x', bandX);
}
brandBand.setAttribute('width', bandWidth);
}
});
}// Toggle guides
function toggleGuides() {
const guides = document.getElementById('guides');
guides.classList.toggle('visible');
}
// Event listeners with instant updates
[titleInput, subtitleInput, tagInput, brandInput].forEach(input => {
input.addEventListener('input', () => {
update(); // Instant update
});
});
layoutSelect.addEventListener('change', (e) => {
currentLayout = e.target.value;
update(); // Instant update
});
fontSizeSlider.addEventListener('input', (e) => {
setFontSizeOffset(e.target.value);
update(); // Instant update
});
fontSizeSlider.addEventListener('change', (e) => {
setFontSizeOffset(e.target.value);
update(); // Instant update
});
textRotationSlider.addEventListener('input', (e) => {
update(); // Instant update
});
textRotationSlider.addEventListener('change', (e) => {
update(); // Instant update
});
Object.values(colorInputs).forEach(input => {
input.addEventListener('input', () => {
update(); // Instant update
});
});
// Visual elements event listeners
Object.values(visualElementCheckboxes).forEach(checkbox => {
checkbox.addEventListener('change', () => {
update(); // Instant update
});
});
elementSizeSlider.addEventListener('input', () => {
update(); // Instant update
});
// Drama effects event listeners
showDrama.addEventListener('change', () => {
update(); // Instant update
});
showTextShadow.addEventListener('change', () => {
update(); // Instant update
});
// Background image event listeners
backgroundUpload.addEventListener('change', handleBackgroundUpload);
removeBackground.addEventListener('click', removeBackgroundImage);
backgroundOpacity.addEventListener('input', () => {
updateBackgroundOpacity();
});
enableTextBands.addEventListener('change', () => {
update(); // Instant update
});
showMobilePreview.addEventListener('change', toggleMobilePreview);
// Preset buttons
document.querySelectorAll('.preset-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.preventDefault();
const preset = btn.dataset.preset;
let colors;
console.log('Preset clicked:', preset); // Debug log
if (preset === 'random') {
const presetNames = Object.keys(palettes);
const randomPreset = presetNames[Math.floor(Math.random() * presetNames.length)];
colors = palettes[randomPreset];
console.log('Random preset selected:', randomPreset, colors); // Debug log
} else {
colors = palettes[preset];
console.log('Preset colors:', colors); // Debug log
}
if (colors) {
const colorKeys = ['bg0', 'bg1', 'primary', 'accent', 'pill', 'tagText'];
colors.forEach((color, index) => {
if (colorInputs[colorKeys[index]]) {
colorInputs[colorKeys[index]].value = color;
console.log(`Set ${colorKeys[index]} to ${color}`); // Debug log
}
});
update(); // Instant update
} else {
console.warn('Preset not found:', preset);
}
});
});
document.getElementById('exportSvg').addEventListener('click', exportSVG);
document.getElementById('exportPng').addEventListener('click', exportPNG);
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
// Prevent shortcuts when typing in input fields
if (e.target.tagName === 'INPUT' || e.target.tagName === 'SELECT' || e.target.tagName === 'TEXTAREA') {
// Only allow Ctrl/Cmd+S when in input fields
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault();
exportPNG();
}
return;
}
switch(e.key.toLowerCase()) {
case 'e':
e.preventDefault();
exportSVG();
break;
case '[':
e.preventDefault();
adjustFontSize(-2);
break;
case ']':
e.preventDefault();
adjustFontSize(2);
break;
case ',':
case '<':
e.preventDefault();
adjustRotation(-1);
break;
case '.':
case '>':
e.preventDefault();
adjustRotation(1);
break;
case 'g':
e.preventDefault();
toggleGuides();
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
e.preventDefault();
const presetNames = ['cool', 'indigo', 'cyan', 'teal', 'rose', 'amber'];
const presetIndex = parseInt(e.key) - 1;
if (presetIndex < presetNames.length) {
const colors = palettes[presetNames[presetIndex]];
const colorKeys = ['bg0', 'bg1', 'primary', 'accent', 'pill', 'tagText'];
colors.forEach((color, index) => {
colorInputs[colorKeys[index]].value = color;
});
update(); // Instant update
}
break;
case 's':
if (e.ctrlKey || e.metaKey) {
e.preventDefault();
exportPNG();
}
break;
}
});
// Initialize when DOM is ready
function initialize() {
updateFontSizeDisplay();
// Debug: Check if key elements exist
console.log('Font size slider:', fontSizeSlider);
console.log('Font size value:', fontSizeValue);
console.log('Title input:', titleInput);
console.log('Layout select:', layoutSelect);
update();
}
// Ensure DOM is loaded before initializing
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initialize);
} else {
initialize();
}