-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
958 lines (834 loc) · 50.9 KB
/
Copy pathscript.js
File metadata and controls
958 lines (834 loc) · 50.9 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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
var json_prompt = JSON.parse(atob("eyIyNSI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lIjoiRHVhbCBNb2RlbCBDRkcgR3VpZGVyIn19LCI5ODoxNTYiOnsiaW5wdXRzIjp7ImNob2ljZSI6IkRlZmF1bHQiLCJpbmRleCI6MSwib3B0aW9uMSI6IlF1YWxpdHkiLCJvcHRpb24yIjoiRGVmYXVsdCIsIm9wdGlvbjMiOiJUdXJibyIsIm9wdGlvbjQiOiIifSwiY2xhc3NfdHlwZSI6IkN1c3RvbUNvbWJvIiwiX21ldGEiOnsidGl0bGUiOiJDdXN0b20gQ29tYm8ifX0sIjk4OjE1NyI6eyJpbnB1dHMiOnsiY2ZnIjozLCJzdGFydF9wZXJjZW50IjowLjksImVuZF9wZXJjZW50IjoxLCJtb2RlbCI6WyI5ODoyMyIsMF19LCJjbGFzc190eXBlIjoiQ0ZHT3ZlcnJpZGUiLCJfbWV0YSI6eyJ0aXRsZSI6IkNGRyBPdmVycmlkZSJ9fX0="))
// State
let canvasW = 1024;
let canvasH = 1024;
let scale = 1;
let boxes = [];
let globalPalette = [];
let selectedBoxId = null;
let boxCounter = 0;
// DOM Elements
const canvas = document.getElementById('canvas-wrapper');
const canvasWSlider = document.getElementById('canvas-width');
const canvasHSlider = document.getElementById('canvas-height');
const aspectSelect = document.getElementById('aspect-ratio');
const seedSlider = document.getElementById('r-seed');
const boxPanel = document.getElementById('box-panel');
const jsonOutput = document.getElementById('json-output')
// UI Listeners for sliders
function syncSizeLabels() {
document.getElementById('w-val').innerText = canvasWSlider.value.toString().padStart(4, '0');
document.getElementById('h-val').innerText = canvasHSlider.value.toString().padStart(4, '0');
if (aspectSelect.value === 'custom') {
aspectSelect.options[aspectSelect.selectedIndex].text = `Custom — ${canvasWSlider.value}×${canvasHSlider.value}`;
}
}
canvasWSlider.addEventListener('input', () => { aspectSelect.value = 'custom'; syncSizeLabels(); });
canvasHSlider.addEventListener('input', () => { aspectSelect.value = 'custom'; syncSizeLabels(); });
seedSlider.addEventListener('input', e => document.getElementById('r-seed-value').innerText = e.target.value.toString().padStart(5, '0'));
document.addEventListener('dragover', (e) => {
e.preventDefault();
});
document.addEventListener('drop', (e) => {
e.preventDefault();
if (e.dataTransfer.files.length > 0) {
importImage(e.dataTransfer.files[0]); // is async
}
});
let defaultExampleImage = 'example.png';
let defaultExampleSize = { width: 1024, height: 1024 };
function setCanvasSize(width, height) {
canvasWSlider.value = Math.round(width);
canvasHSlider.value = Math.round(height);
syncSizeLabels();
}
function applyAspectPreset() {
const value = aspectSelect.value;
if (value === 'original') {
setCanvasSize(defaultExampleSize.width, defaultExampleSize.height);
} else if (value !== 'custom') {
const [w, h] = value.split('x').map(Number);
setCanvasSize(w, h);
}
initCanvas();
}
function initCanvas() {
canvasW = parseInt(canvasWSlider.value);
canvasH = parseInt(canvasHSlider.value);
canvas.style.width = canvasW + 'px';
canvas.style.height = canvasH + 'px';
fitCanvasToShell();
canvas.dataset.size = `${canvasW} × ${canvasH}`;
setDefaultCanvasBackground();
canvas.innerHTML = '';
jsonOutput.value = '';
boxes = [];
selectBox(null);
}
// Color Palette Logic
function addColor(type) {
const picker = document.getElementById(type + '-color-picker');
let hex = picker.value.toUpperCase(); // Ideogram4 requires uppercase string
if (type === 'global') {
if (globalPalette.length >= 16) return alert("Max 16 colors allowed.");
if (!globalPalette.includes(hex)) {
globalPalette.push(hex);
renderColors('global');
saveStateDebounced(); generateJSON();
}
} else if (type === 'box' && selectedBoxId !== null) {
const box = boxes.find(b => b.id === selectedBoxId);
if (box.colors.length >= 5) return alert("Max 5 colors allowed per box.");
if (!box.colors.includes(hex)) {
box.colors.push(hex);
renderColors('box');
saveStateDebounced(); generateJSON();
}
}
}
function removeColor(type, hex) {
if (type === 'global') {
globalPalette = globalPalette.filter(c => c !== hex);
renderColors('global');
saveStateDebounced(); generateJSON();
} else if (type === 'box' && selectedBoxId !== null) {
const box = boxes.find(b => b.id === selectedBoxId);
box.colors = box.colors.filter(c => c !== hex);
renderColors('box');
saveStateDebounced(); generateJSON();
}
}
function renderColors(type) {
const container = document.getElementById(type + '-colors');
container.innerHTML = '';
const list = type === 'global' ? globalPalette : (boxes.find(b => b.id === selectedBoxId)?.colors || []);
list.forEach(hex => {
const swatch = document.createElement('div');
swatch.className = 'swatch';
swatch.style.backgroundColor = hex;
swatch.innerHTML = '×';
swatch.onclick = () => removeColor(type, hex);
container.appendChild(swatch);
});
}
// Canvas Drawing Logic
let isDrawing = false;
let isDragging = false;
let isResizing = false;
let currentBoxDOM = null;
let startX, startY;
canvas.addEventListener('pointerdown', (e) => {
if (e.target === canvas) {
isDrawing = true;
const rect = canvas.getBoundingClientRect();
startX = (e.clientX - rect.left) / scale;
startY = (e.clientY - rect.top) / scale;
const box = {
id: 'box_' + boxCounter++,
x: startX, y: startY, w: 0, h: 0,
mode: 'obj', text: '', desc: '', colors: []
};
boxes.push(box);
currentBoxDOM = document.createElement('div');
currentBoxDOM.className = 'bounding-box selected';
currentBoxDOM.id = box.id;
currentBoxDOM.style.left = startX + 'px';
currentBoxDOM.style.top = startY + 'px';
appendBoxControls(currentBoxDOM, box);
canvas.appendChild(currentBoxDOM);
selectBox(box.id);
} else if (e.target.classList.contains('resize-handle')) {
isResizing = true;
currentBoxDOM = e.target.parentElement;
selectBox(currentBoxDOM.id);
const rect = canvas.getBoundingClientRect();
dragStartX = (e.clientX - rect.left) / scale;
dragStartY = (e.clientY - rect.top) / scale;
initialBoxX = parseFloat(currentBoxDOM.style.left) || 0;
initialBoxY = parseFloat(currentBoxDOM.style.top) || 0;
initialBoxW = parseFloat(currentBoxDOM.style.width) || 0;
initialBoxH = parseFloat(currentBoxDOM.style.height) || 0;
resizeDir = e.target.dataset.dir || 'se';
e.stopPropagation();
} else if (e.target.closest('.bounding-box')) {
isDragging = true;
currentBoxDOM = e.target.closest('.bounding-box');
selectBox(currentBoxDOM.id);
const rect = canvas.getBoundingClientRect();
dragStartX = (e.clientX - rect.left) / scale;
dragStartY = (e.clientY - rect.top) / scale;
initialBoxX = parseFloat(currentBoxDOM.style.left);
initialBoxY = parseFloat(currentBoxDOM.style.top);
e.stopPropagation();
}
});
canvas.addEventListener('pointermove', (e) => {
const rect = canvas.getBoundingClientRect();
const currentX = (e.clientX - rect.left) / scale;
const currentY = (e.clientY - rect.top) / scale;
if (isDrawing && currentBoxDOM) {
const w = currentX - startX;
const h = currentY - startY;
currentBoxDOM.style.width = Math.abs(w) + 'px';
currentBoxDOM.style.height = Math.abs(h) + 'px';
currentBoxDOM.style.left = (w < 0 ? currentX : startX) + 'px';
currentBoxDOM.style.top = (h < 0 ? currentY : startY) + 'px';
}
});
window.addEventListener('pointerup', () => {
if (isDrawing && currentBoxDOM) {
// Save final dimensions
const box = boxes.find(b => b.id === currentBoxDOM.id);
box.w = parseFloat(currentBoxDOM.style.width) || 0;
box.h = parseFloat(currentBoxDOM.style.height) || 0;
box.x = parseFloat(currentBoxDOM.style.left) || 0;
box.y = parseFloat(currentBoxDOM.style.top) || 0;
if (box.w < 10 || box.h < 10) {
// Ignore tiny accidental clicks
canvas.removeChild(currentBoxDOM);
boxes = boxes.filter(b => b.id !== box.id);
selectBox(null);
}
}
if (isDragging || isResizing) syncDomToBoxes();
if (isDrawing || isDragging || isResizing) { renderLayers(); saveState(); generateJSON(); }
clearSnapLines();
isDrawing = false;
isDragging = false;
isResizing = false;
currentBoxDOM = null;
});
// Box Interaction (Select, Drag, Resize)
let dragStartX, dragStartY, initialBoxX, initialBoxY, initialBoxW, initialBoxH, resizeDir = 'se';
window.addEventListener('pointermove', (e) => {
const rect = canvas.getBoundingClientRect();
const currentX = (e.clientX - rect.left) / scale;
const currentY = (e.clientY - rect.top) / scale;
if (isDragging && currentBoxDOM) {
const dx = currentX - dragStartX;
const dy = currentY - dragStartY;
const snapped = snapRect(initialBoxX + dx, initialBoxY + dy, parseFloat(currentBoxDOM.style.width), parseFloat(currentBoxDOM.style.height));
currentBoxDOM.style.left = snapped.x + 'px';
currentBoxDOM.style.top = snapped.y + 'px';
} else if (isResizing && currentBoxDOM) {
const dx = currentX - dragStartX;
const dy = currentY - dragStartY;
// Resize with the opposite edge locked.
// Example: dragging the right handle only changes the right edge,
// dragging the top handle only changes the top edge, etc.
// This prevents the box from expanding in the opposite direction.
const minSize = 10;
let left = initialBoxX;
let top = initialBoxY;
let right = initialBoxX + initialBoxW;
let bottom = initialBoxY + initialBoxH;
if (resizeDir.includes('e')) right = initialBoxX + initialBoxW + dx;
if (resizeDir.includes('w')) left = initialBoxX + dx;
if (resizeDir.includes('s')) bottom = initialBoxY + initialBoxH + dy;
if (resizeDir.includes('n')) top = initialBoxY + dy;
// Clamp to canvas bounds while keeping the opposite side fixed.
left = Math.max(0, Math.min(left, canvasW));
right = Math.max(0, Math.min(right, canvasW));
top = Math.max(0, Math.min(top, canvasH));
bottom = Math.max(0, Math.min(bottom, canvasH));
if (resizeDir.includes('w') && right - left < minSize) left = right - minSize;
if (resizeDir.includes('e') && right - left < minSize) right = left + minSize;
if (resizeDir.includes('n') && bottom - top < minSize) top = bottom - minSize;
if (resizeDir.includes('s') && bottom - top < minSize) bottom = top + minSize;
// Final safety clamp.
left = Math.max(0, left);
top = Math.max(0, top);
right = Math.min(canvasW, right);
bottom = Math.min(canvasH, bottom);
currentBoxDOM.style.left = left + 'px';
currentBoxDOM.style.top = top + 'px';
currentBoxDOM.style.width = Math.max(minSize, right - left) + 'px';
currentBoxDOM.style.height = Math.max(minSize, bottom - top) + 'px';
}
});
// Box Forms logic
function selectBox(id) {
selectedBoxId = id;
document.querySelectorAll('.bounding-box').forEach(el => el.classList.remove('selected'));
if (id) {
document.getElementById(id).classList.add('selected');
boxPanel.style.display = 'block';
const box = boxes.find(b => b.id === id);
document.getElementById('box-mode').value = box.mode;
document.getElementById('box-text').value = box.text;
document.getElementById('box-desc').value = box.desc;
document.getElementById('text-input-group').style.display = box.mode === 'text' ? 'block' : 'none';
renderColors('box');
} else {
boxPanel.style.display = 'none';
}
renderLayers();
}
function updateBoxData() {
if (!selectedBoxId) return;
const box = boxes.find(b => b.id === selectedBoxId);
box.mode = document.getElementById('box-mode').value;
box.text = document.getElementById('box-text').value;
box.desc = document.getElementById('box-desc').value;
document.getElementById('text-input-group').style.display = box.mode === 'text' ? 'block' : 'none';
updateBoxLabel(box);
renderLayers();
saveStateDebounced();
generateJSON();
}
function getBoxTitle(box) {
const text = (box.mode === 'text' ? box.text : box.desc) || 'Describe this box...';
return text.trim() || 'Describe this box...';
}
function appendBoxLabel(dom, box) {
const label = document.createElement('div');
label.className = 'box-label';
dom.appendChild(label);
updateBoxLabel(box);
}
function updateBoxLabel(box) {
const dom = document.getElementById(box.id);
if (!dom) return;
let label = dom.querySelector('.box-label');
if (!label) {
label = document.createElement('div');
label.className = 'box-label';
dom.insertBefore(label, dom.firstChild);
}
label.textContent = getBoxTitle(box);
label.title = getBoxTitle(box);
}
function setDefaultCanvasBackground() {
const applyBg = (fileName, img) => {
defaultExampleImage = fileName;
if (img && img.naturalWidth && img.naturalHeight) {
defaultExampleSize = { width: img.naturalWidth, height: img.naturalHeight };
}
canvas.style.backgroundImage = `linear-gradient(rgba(255,255,255,.055) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.055) 1px, transparent 1px), url('${fileName}')`;
canvas.style.backgroundSize = '64px 64px, 64px 64px, cover';
canvas.style.backgroundPosition = 'center, center, center';
canvas.style.backgroundRepeat = 'repeat, repeat, no-repeat';
};
const png = new Image();
png.onload = () => applyBg('example.png', png);
png.onerror = () => {
const jpg = new Image();
jpg.onload = () => applyBg('example.jpg', jpg);
jpg.onerror = () => {
canvas.style.backgroundImage = `linear-gradient(rgba(255,255,255,.055) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.055) 1px, transparent 1px)`;
canvas.style.backgroundSize = '64px 64px, 64px 64px';
};
jpg.src = 'example.jpg';
};
png.src = 'example.png';
}
function fitCanvasToShell() {
const shell = canvas.parentElement;
const shellWidth = Math.max(320, shell.clientWidth - 40);
const shellHeight = Math.max(320, shell.clientHeight - 40);
// Auto fit when the canvas is larger than the visible workspace.
// When it is smaller, keep it at 100% and center it with flex.
scale = Math.min(shellWidth / canvasW, shellHeight / canvasH, 1);
canvas.style.width = canvasW + 'px';
canvas.style.height = canvasH + 'px';
canvas.style.transform = 'none';
canvas.style.zoom = scale;
shell.style.width = '100%';
shell.style.justifyContent = (canvasW * scale <= shellWidth) ? 'center' : 'flex-start';
requestAnimationFrame(() => {
if (shell.scrollWidth > shell.clientWidth) {
shell.scrollLeft = (shell.scrollWidth - shell.clientWidth) / 2;
}
});
}
window.addEventListener('resize', () => {
fitCanvasToShell();
});
function deleteSelectedBox() {
if (!selectedBoxId) return;
const dom = document.getElementById(selectedBoxId);
if (dom) dom.remove();
boxes = boxes.filter(b => b.id !== selectedBoxId);
selectBox(null);
renderLayers();
saveState();
generateJSON();
}
// JSON Generation
function generateJSON() {
// Save current bounding box positions to state
boxes.forEach(box => {
const dom = document.getElementById(box.id);
if (dom) {
box.x = parseFloat(dom.style.left);
box.y = parseFloat(dom.style.top);
box.w = parseFloat(dom.style.width);
box.h = parseFloat(dom.style.height);
}
});
// Normalize Coordinates 0-1000
const norm = (val, max) => Math.min(1000, Math.max(0, Math.round((val / max) * 1000)));
const elements = boxes.map(box => {
const x1 = norm(box.x, canvasW);
const y1 = norm(box.y, canvasH);
const x2 = norm(box.x + box.w, canvasW);
const y2 = norm(box.y + box.h, canvasH);
let el = {
type: box.mode,
bbox: [y1, x1, y2, x2],
};
if (box.mode === 'text') el.text = box.text;
el.desc = box.desc;
if (box.colors && box.colors.length > 0) el.color_palette = box.colors;
return el;
});
var output = {
high_level_description: document.getElementById('high_level_description').value,
style_description: {
aesthetics: document.getElementById('aesthetics').value,
lighting: document.getElementById('lighting').value,
//medium: document.getElementById('medium').value,
//art_style: document.getElementById('art_style').value,
//color_palette: globalPalette
},
compositional_deconstruction: {
background: document.getElementById('background').value,
elements: elements
}
};
// in photo mode, order in prompt should be "photo" then "medium",
// while in art_style mode order should be "medium" then "art_style"
if(photoArtStyleMode == MODE_PHOTO){
output.style_description.photo = document.getElementById('art_style').value;
output.style_description.medium = document.getElementById('medium').value;
}
else{
output.style_description.medium = document.getElementById('medium').value;
output.style_description.art_style = document.getElementById('art_style').value
}
output.style_description.color_palette = globalPalette
const strict = document.getElementById('strict-json')?.checked;
if (strict) cleanEmpty(output);
jsonOutput.value = JSON.stringify(output, null, document.getElementById('minify-json')?.checked ? 0 : 2);
}
async function waitForComfyUIResult(
promptId,
serverUrl,
pollInterval = 1000,
timeout = 300000,
fetchImages = true,
) {
const startTime = Date.now();
while (true) {
if (Date.now() - startTime > timeout) {
throw new Error(`Timed out waiting for prompt ${promptId}`);
}
// Check prompt history
const response = await fetch(
`${serverUrl}/history/${encodeURIComponent(promptId)}`
);
if (!response.ok) {
throw new Error(`Failed to fetch history: ${response.status}`);
}
const history = await response.json();
// Prompt appears in history once execution completes
if (history[promptId]) {
const outputs = history[promptId].outputs || {};
const results = [];
for (const nodeId of Object.keys(outputs)) {
const nodeOutput = outputs[nodeId];
if (!nodeOutput.images) continue;
for (const image of nodeOutput.images) {
const imageUrl =
`${serverUrl}/view?` +
new URLSearchParams({
filename: image.filename,
subfolder: image.subfolder || "",
type: image.type || "output",
});
const result = {
nodeId,
imageUrl,
filename: image.filename,
subfolder: image.subfolder,
type: image.type,
};
if (fetchImages) {
const imgResp = await fetch(imageUrl);
result.blob = await imgResp.blob();
}
results.push(result);
}
}
return results;
}
await new Promise(resolve => setTimeout(resolve, pollInterval));
}
}
function setImage(imgURL){
document.getElementById("image-view").src = imgURL
canvas.style.backgroundImage = `linear-gradient(rgba(255,255,255,.055) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.055) 1px, transparent 1px), url("${imgURL}")`;
canvas.style.backgroundSize = '64px 64px, 64px 64px, cover';
canvas.style.backgroundPosition = 'center, center, center';
fitCanvasToShell();
}
async function generateImage() {
const api = document.getElementById("api-location").value
json_prompt["98:24"]["inputs"]["text"] = jsonOutput.value
json_prompt["98:27"]["inputs"]["value"] = canvasWSlider.value
json_prompt["98:28"]["inputs"]["value"] = canvasHSlider.value
json_prompt["98:18"]["inputs"]["noise_seed"] = seedSlider.value
var req = await fetch(api+"/api/prompt", {
"headers": {
"accept": "*/*",
"content-type": "application/json"
},
"body": JSON.stringify({ "prompt": json_prompt }),
"method": "POST"
});
req = await req.json()
const image = await waitForComfyUIResult(req.prompt_id, api,
3000,
300000,
true
)
setImage(image[0].imageUrl)
}
function extractComfyUIMetadata(arrayBuffer) {
const view = new DataView(arrayBuffer);
// Check valid PNG 8-byte signature
if (view.byteLength < 8 || view.getUint32(0) !== 0x89504E47 || view.getUint32(4) !== 0x0D0A1A0A) {
throw new Error("Not a valid PNG file.");
}
let offset = 8;
const result = {};
while (offset < view.byteLength) {
if (offset + 8 > view.byteLength) break;
// Read chunk length and type
const length = view.getUint32(offset);
const chunkType = String.fromCharCode(
view.getUint8(offset + 4),
view.getUint8(offset + 5),
view.getUint8(offset + 6),
view.getUint8(offset + 7)
);
offset += 8; // Move past length and type
if (chunkType === 'tEXt') {
if (offset + length > view.byteLength) break;
const chunkData = new Uint8Array(arrayBuffer, offset, length);
const nullByteIndex = chunkData.indexOf(0); // Keyword and text are separated by a 0x00 byte
if (nullByteIndex !== -1) {
const keyword = new TextDecoder('ascii').decode(chunkData.subarray(0, nullByteIndex));
if (keyword === 'prompt' || keyword === 'workflow') {
const rawText = new TextDecoder('utf-8').decode(chunkData.subarray(nullByteIndex + 1));
try {
result[keyword] = JSON.parse(rawText);
} catch (e) {
result[keyword] = rawText; // Fallback if JSON parsing fails
}
}
}
}
offset += length + 4; // Skip the data segment and the 4-byte CRC trailing checksum
}
for (const key of Object.keys(result.prompt)){
try{
const json = JSON.parse(result.prompt[key].inputs.text)
if(json.hasOwnProperty("high_level_description")){
return json
}
}
catch(e){}
}
}
function parseBoxesFromJSON(json){
try{
const n = json.compositional_deconstruction.elements.length
for (var i=0; i<n; i++){
const element = json.compositional_deconstruction.elements[i]
const bbox = element.bbox
bbox[0] = bbox[0]/1000*canvasH
bbox[2] = bbox[2]/1000*canvasH
bbox[1] = bbox[1]/1000*canvasW
bbox[3] = bbox[3]/1000*canvasW
var box = {
id: 'box_' + boxCounter++,
x: bbox[1], y: bbox[0], w: bbox[3]-bbox[1], h: bbox[2]-bbox[0],
mode: element.type, text: element.text ?? '', desc: element.desc, colors: element.color_palette
}
boxes.push(box);
currentBoxDOM = document.createElement('div');
currentBoxDOM.className = 'bounding-box';
currentBoxDOM.id = box.id;
currentBoxDOM.style.left = box.x + 'px';
currentBoxDOM.style.top = box.y + 'px';
currentBoxDOM.style.width = box.w + 'px';
currentBoxDOM.style.height = box.h + 'px';
appendBoxControls(currentBoxDOM, box);
canvas.appendChild(currentBoxDOM);
currentBoxDOM = null;
}
}
catch(e){
alert("invalid JSON")
}
}
function parseParametersFromJSON(json){
document.getElementById('high_level_description').value = json.high_level_description
document.getElementById('aesthetics').value = json.style_description.aesthetics
document.getElementById('lighting').value = json.style_description.lighting
document.getElementById('medium').value = json.style_description.medium
document.getElementById('background').value = json.compositional_deconstruction.background
const globalColorPicker = document.getElementById('global-color-picker')
globalPalette = [];
renderColors('global');
for(var i=0; i<json.style_description.color_palette.length; i++){
globalColorPicker.value = json.style_description.color_palette[i];
addColor('global');
}
if(json.style_description.hasOwnProperty("photo")){
document.getElementById('art_style').value = json.style_description.photo
document.getElementById('mode_photo').checked = true
setPhotoArtstyle(MODE_PHOTO);
}
else{
document.getElementById('art_style').value = json.style_description.art_style
document.getElementById('mode_artstyle').checked = true
setPhotoArtstyle(MODE_ARTSTYLE);
}
}
async function importImage(image){
console.log(image.size)
if(image.type.startsWith("image")){
// is image
const img = new Image();
img.onload = function() {
canvasWSlider.value = this.width.toString();
canvasWSlider.dispatchEvent(new Event('input'));
canvasHSlider.value = this.height.toString();
canvasHSlider.dispatchEvent(new Event('input'));
defaultExampleSize = { width: this.width, height: this.height };
aspectSelect.value = 'original';
initCanvas();
setImage(img.src);
if(image.type=="image/png"){
// is png. try and find workflow metadata
image.arrayBuffer()
.then(buff => {
const json = extractComfyUIMetadata(buff);
jsonOutput.value = JSON.stringify(json, null, 2);
parseBoxesFromJSON(json)
parseParametersFromJSON(json)
})
}
};
img.src = URL.createObjectURL(image);
}
else{
alert("images only")
}
}
var photoArtStyleMode = 1;
const MODE_PHOTO = 0;
const MODE_ARTSTYLE = 1;
function setPhotoArtstyle(mode){
console.log(mode)
photoArtStyleMode = mode
if(mode==MODE_PHOTO){
// photo mode
document.getElementById("medium").value = "photograph";
document.getElementById("medium").disabled = true
document.getElementById("mode_label").innerText = "Photo"
}
else if (mode==MODE_ARTSTYLE){
// art_style mode
document.getElementById("medium").disabled = false
document.getElementById("mode_label").innerText = "Art Style"
}
else{
console.error("invalid mode")
}
}
// ---------- UX upgrades imported from Ideogram4-PromptEditor style ----------
let snapEnabled = true;
let historyStack = [];
let historyIndex = -1;
let saveTimer = null;
function syncDomToBoxes() {
boxes.forEach(box => {
const dom = document.getElementById(box.id);
if (!dom) return;
box.x = parseFloat(dom.style.left) || 0;
box.y = parseFloat(dom.style.top) || 0;
box.w = parseFloat(dom.style.width) || 0;
box.h = parseFloat(dom.style.height) || 0;
});
}
function cleanEmpty(obj) {
if (Array.isArray(obj)) { obj.forEach(cleanEmpty); return obj; }
if (!obj || typeof obj !== 'object') return obj;
Object.keys(obj).forEach(k => {
const v = obj[k];
if (v && typeof v === 'object') cleanEmpty(v);
const emptyArray = Array.isArray(v) && v.length === 0;
const emptyObj = v && typeof v === 'object' && !Array.isArray(v) && Object.keys(v).length === 0;
if (v === '' || v == null || emptyArray || emptyObj) delete obj[k];
});
return obj;
}
function appendBoxControls(dom, box) {
appendBoxLabel(dom, box);
['n','s','e','w','nw','ne','sw','se'].forEach(dir => {
const handle = document.createElement('div');
handle.className = 'resize-handle resize-handle-' + dir;
handle.dataset.dir = dir;
dom.appendChild(handle);
});
}
function snapRect(x, y, w, h) {
if (!snapEnabled) return {x,y,w,h};
const grid = 32;
const threshold = 8;
const snapVal = (v) => {
const n = Math.round(v / grid) * grid;
return Math.abs(v - n) <= threshold ? n : v;
};
const oldX = x, oldY = y;
x = snapVal(x); y = snapVal(y);
x = Math.max(0, Math.min(canvasW - w, x));
y = Math.max(0, Math.min(canvasH - h, y));
showSnapLines(x !== oldX ? x : null, y !== oldY ? y : null);
return {x,y,w,h};
}
function showSnapLines(x, y) {
clearSnapLines();
if (x !== null) { const l = document.createElement('div'); l.className='snap-line v'; l.style.left=x+'px'; canvas.appendChild(l); }
if (y !== null) { const l = document.createElement('div'); l.className='snap-line h'; l.style.top=y+'px'; canvas.appendChild(l); }
}
function clearSnapLines() { canvas.querySelectorAll('.snap-line').forEach(el => el.remove()); }
function toggleSnap() {
snapEnabled = !snapEnabled;
document.getElementById('snap-toggle')?.classList.toggle('active', snapEnabled);
clearSnapLines();
}
function saveState() {
syncDomToBoxes();
const snap = JSON.stringify({ boxes, globalPalette, selectedBoxId, canvasW, canvasH,
hld: document.getElementById('high_level_description').value,
aesthetics: document.getElementById('aesthetics').value,
lighting: document.getElementById('lighting').value,
medium: document.getElementById('medium').value,
art: document.getElementById('art_style').value,
background: document.getElementById('background').value,
photoArtStyleMode });
if (historyStack[historyIndex] === snap) return;
historyStack = historyStack.slice(0, historyIndex + 1);
historyStack.push(snap);
historyIndex = historyStack.length - 1;
updateHistoryButtons();
}
function saveStateDebounced() { clearTimeout(saveTimer); saveTimer = setTimeout(saveState, 300); }
function restoreState(snap) {
const data = JSON.parse(snap);
boxes = data.boxes || []; globalPalette = data.globalPalette || []; selectedBoxId = data.selectedBoxId || null;
canvasWSlider.value = data.canvasW || canvasWSlider.value; canvasHSlider.value = data.canvasH || canvasHSlider.value; syncSizeLabels();
document.getElementById('high_level_description').value = data.hld || '';
document.getElementById('aesthetics').value = data.aesthetics || '';
document.getElementById('lighting').value = data.lighting || '';
document.getElementById('medium').value = data.medium || '';
document.getElementById('art_style').value = data.art || '';
document.getElementById('background').value = data.background || '';
photoArtStyleMode = data.photoArtStyleMode ?? photoArtStyleMode;
canvasW = parseInt(canvasWSlider.value); canvasH = parseInt(canvasHSlider.value);
canvas.style.width = canvasW + 'px'; canvas.style.height = canvasH + 'px'; canvas.dataset.size = `${canvasW} × ${canvasH}`;
canvas.querySelectorAll('.bounding-box,.snap-line').forEach(el => el.remove());
boxes.forEach(box => createBoxDom(box));
fitCanvasToShell(); renderColors('global'); renderLayers(); selectBox(selectedBoxId); generateJSON(); updateHistoryButtons();
}
function undoState() { if (historyIndex > 0) { historyIndex--; restoreState(historyStack[historyIndex]); } }
function redoState() { if (historyIndex < historyStack.length - 1) { historyIndex++; restoreState(historyStack[historyIndex]); } }
function updateHistoryButtons() {
const u=document.getElementById('undo-btn'), r=document.getElementById('redo-btn');
if (u) u.disabled = historyIndex <= 0; if (r) r.disabled = historyIndex >= historyStack.length - 1;
}
function createBoxDom(box) {
const dom = document.createElement('div');
dom.className = 'bounding-box'; dom.id = box.id;
dom.style.left = box.x + 'px'; dom.style.top = box.y + 'px';
dom.style.width = box.w + 'px'; dom.style.height = box.h + 'px';
appendBoxControls(dom, box); canvas.appendChild(dom); return dom;
}
function renderLayers() {
const list = document.getElementById('layers-list');
if (!list) return;
syncDomToBoxes();
list.innerHTML = '';
boxes.forEach((box, i) => {
const item = document.createElement('div');
item.className = 'layer-item' + (box.id === selectedBoxId ? ' selected' : '');
item.onclick = () => selectBox(box.id);
item.innerHTML = `<div>${box.mode === 'text' ? 'T' : '□'}</div><div><div class="layer-title">${escapeHTML(getBoxTitle(box))}</div><div class="layer-meta">${box.mode} · [${Math.round(box.y)}, ${Math.round(box.x)}, ${Math.round(box.y+box.h)}, ${Math.round(box.x+box.w)}]</div></div><div class="layer-actions"><button title="Up">↑</button><button title="Down">↓</button></div>`;
const btns = item.querySelectorAll('button');
btns[0].onclick = (e) => { e.stopPropagation(); moveLayer(i, -1); };
btns[1].onclick = (e) => { e.stopPropagation(); moveLayer(i, 1); };
list.appendChild(item);
});
}
function escapeHTML(str) { return String(str).replace(/[&<>'"]/g, c => ({'&':'&','<':'<','>':'>',"'":''','"':'"'}[c])); }
function moveLayer(index, delta) {
const ni = index + delta; if (ni < 0 || ni >= boxes.length) return;
[boxes[index], boxes[ni]] = [boxes[ni], boxes[index]];
boxes.forEach((b, idx) => { const d=document.getElementById(b.id); if (d) d.style.zIndex = idx + 1; });
renderLayers(); saveState(); generateJSON();
}
function addPresetBox(desc='New customized layout element description', x=300, y=300, w=300, h=300, mode='obj', text='') {
const box = { id:'box_' + boxCounter++, x, y, w, h, mode, text, desc, colors: [] };
boxes.push(box); createBoxDom(box); selectBox(box.id); renderLayers(); saveState(); generateJSON();
}
function alignSelected(dir) {
const box = boxes.find(b => b.id === selectedBoxId); if (!box) return;
syncDomToBoxes();
if (dir === 'left') box.x = 0;
if (dir === 'right') box.x = canvasW - box.w;
if (dir === 'center-h') box.x = (canvasW - box.w) / 2;
if (dir === 'top') box.y = 0;
if (dir === 'bottom') box.y = canvasH - box.h;
if (dir === 'center-v') box.y = (canvasH - box.h) / 2;
const dom = document.getElementById(box.id);
if (dom) { dom.style.left=box.x+'px'; dom.style.top=box.y+'px'; }
renderLayers(); saveState(); generateJSON();
}
function copyJSON() {
generateJSON();
navigator.clipboard?.writeText(jsonOutput.value).then(() => alert('JSON copied!')).catch(() => { jsonOutput.select(); document.execCommand('copy'); });
}
const layoutPresets = {
cinematic: { ratio:'1568x672', hld:'A dramatic cinematic landscape with sweeping mountain silhouettes, moody clouds, warm sunlight, and a lone figure overlooking a vast valley.', bg:'Deep atmospheric sky gradient with volumetric haze and distant mountain layers.', style:['Epic cinematic realism with filmic color grading.','Golden hour backlight with volumetric rays.','Cinematic photography','medium format film look'], boxes:[['Expansive sky and cloud formation spanning the upper frame.',0,0,1000,420],['Distant mountain range silhouette across the horizon.',0,380,1000,180],['A lone figure standing on a rocky ridge in the foreground.',440,420,110,220]]},
product: { ratio:'864x1152', hld:'A clean minimalist product showcase featuring a single premium object centered in abundant negative space.', bg:'Seamless warm off-white studio background with a soft cast shadow.', style:['Pure minimalism with precise geometric harmony.','Soft diffused side light.','Product photography','commercial studio photo'], boxes:[['Centered premium product object on display.',300,330,300,420],['Soft cast shadow beneath the product.',270,730,460,60],['ESSENTIAL',210,120,580,90,'text','ESSENTIAL']]},
portrait: { ratio:'864x1152', hld:'A professional studio portrait with classic Rembrandt lighting and refined modern styling.', bg:'Textured charcoal gray muslin backdrop with a subtle gradient.', style:['Classic studio portraiture with timeless elegance.','Rembrandt triangle lighting with subtle rim light.','Studio portrait photography','professional headshot'], boxes:[['Subject head and shoulders positioned in the center.',280,150,420,520],['Neutral earth-tone clothing on lower body.',300,650,400,260]]},
editorial: { ratio:'720x1088', hld:'A bold fashion editorial composition with avant-garde silhouette and dramatic geometric shadows.', bg:'Bleached-white studio backdrop with diagonal architectural shadow lines.', style:['High-fashion editorial with sculptural composition.','Single hard side light creating deep shadows.','Editorial fashion photography','magazine cover style'], boxes:[['Avant-garde figure silhouette in a bold fashion pose.',220,140,320,700],['Bold editorial headline in heavy sans-serif.',90,850,540,120,'text','AVANT-GARDE']]},
tech: { ratio:'1280x720', hld:'A futuristic cyberpunk cityscape at night with holographic billboards, neon reflections, and orbiting drones.', bg:'Dark steel-blue city atmosphere with faint circuit grid distortion.', style:['Cyberpunk futuristic neon tech aesthetic.','Neon signage lighting with deep blue shadows.','Digital concept art','crisp digital render'], boxes:[['Gleaming skyscraper skyline spanning the background.',60,80,1100,380],['Large holographic billboard emitting magenta and cyan light.',760,190,330,210],['NEON CITY',330,30,620,90,'text','NEON CITY']]},
nature: { ratio:'1152x864', hld:'A serene forest stream through ancient moss-covered trees with dappled sunlight and wildflowers.', bg:'Softly blurred dense forest foliage receding into the distance.', style:['Lush botanical documentary realism.','Soft dappled forest light.','Nature photography','National Geographic style'], boxes:[['Lush green forest canopy framing the top.',0,0,1152,260],['Meandering stream winding into the foreground.',300,330,550,330],['Clusters of wildflowers along the stream bank.',60,650,260,170]]}
};
function loadLayoutPreset(name) {
if (!name || !layoutPresets[name]) return;
const p = layoutPresets[name];
aspectSelect.value = p.ratio; applyAspectPreset();
document.getElementById('high_level_description').value = p.hld;
document.getElementById('background').value = p.bg;
document.getElementById('aesthetics').value = p.style[0] || '';
document.getElementById('lighting').value = p.style[1] || '';
document.getElementById('medium').value = p.style[2] || '';
document.getElementById('art_style').value = p.style[3] || '';
boxes = []; canvas.querySelectorAll('.bounding-box').forEach(el => el.remove());
p.boxes.forEach(b => addPresetBox(b[0], b[1], b[2], b[3], b[4], b[5] || 'obj', b[6] || ''));
selectBox(null); renderLayers(); saveState(); generateJSON();
document.getElementById('preset-selector').value = '';
}
syncSizeLabels();
initCanvas();
setPhotoArtstyle(1);
renderLayers();
generateJSON();
saveState();
requestAnimationFrame(fitCanvasToShell);