Spaces:
Paused
Paused
Upload 5 files
Browse files- app.py +11 -0
- requirements.txt +2 -1
- templates/index.html +202 -67
app.py
CHANGED
|
@@ -27,5 +27,16 @@ def upload_file():
|
|
| 27 |
return send_file(img_io, mimetype='image/png', as_attachment=True, download_name='_rmbg.png')
|
| 28 |
return render_template('index.html')
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if __name__ == '__main__':
|
|
|
|
|
|
|
| 31 |
app.run(host='0.0.0.0', debug=True, port=7860)
|
|
|
|
| 27 |
return send_file(img_io, mimetype='image/png', as_attachment=True, download_name='_rmbg.png')
|
| 28 |
return render_template('index.html')
|
| 29 |
|
| 30 |
+
import psutil
|
| 31 |
+
|
| 32 |
+
# ... (existing imports)
|
| 33 |
+
|
| 34 |
+
@app.route('/cpu', methods=['GET'])
|
| 35 |
+
def get_cpu():
|
| 36 |
+
# interval=None gets the cpu usage since the last call, non-blocking
|
| 37 |
+
return jsonify({'cpu_percent': psutil.cpu_percent(interval=None)})
|
| 38 |
+
|
| 39 |
if __name__ == '__main__':
|
| 40 |
+
# Initial call to set the blocking point for psutil
|
| 41 |
+
psutil.cpu_percent()
|
| 42 |
app.run(host='0.0.0.0', debug=True, port=7860)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
flask
|
| 2 |
rembg>=2.0.57
|
| 3 |
pillow
|
| 4 |
-
onnxruntime
|
|
|
|
|
|
| 1 |
flask
|
| 2 |
rembg>=2.0.57
|
| 3 |
pillow
|
| 4 |
+
onnxruntime
|
| 5 |
+
psutil
|
templates/index.html
CHANGED
|
@@ -75,6 +75,42 @@
|
|
| 75 |
font-size: 32px;
|
| 76 |
}
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
/* --- Main Layout --- */
|
| 79 |
main {
|
| 80 |
flex: 1;
|
|
@@ -109,6 +145,8 @@
|
|
| 109 |
background: linear-gradient(135deg, #0f172a 0%, #334155 100%);
|
| 110 |
-webkit-background-clip: text;
|
| 111 |
-webkit-text-fill-color: transparent;
|
|
|
|
|
|
|
| 112 |
}
|
| 113 |
|
| 114 |
.hero p {
|
|
@@ -201,7 +239,6 @@
|
|
| 201 |
overflow: hidden;
|
| 202 |
box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.18);
|
| 203 |
background: transparent;
|
| 204 |
-
/* allow checkerboard to show through if bg none */
|
| 205 |
transition: all 0.3s ease;
|
| 206 |
}
|
| 207 |
|
|
@@ -232,28 +269,13 @@
|
|
| 232 |
box-shadow: -4px 0 24px rgba(0, 0, 0, 0.04);
|
| 233 |
}
|
| 234 |
|
| 235 |
-
.tools-header {
|
| 236 |
-
padding: 1.5rem;
|
| 237 |
-
border-bottom: 1px solid var(--border);
|
| 238 |
-
display: flex;
|
| 239 |
-
justify-content: space-between;
|
| 240 |
-
align-items: center;
|
| 241 |
-
}
|
| 242 |
-
|
| 243 |
-
.tools-title {
|
| 244 |
-
font-weight: 700;
|
| 245 |
-
font-size: 1.1rem;
|
| 246 |
-
color: var(--text-main);
|
| 247 |
-
margin: 0;
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
/* Pills Tab */
|
| 251 |
.tabs-pills {
|
| 252 |
display: flex;
|
| 253 |
background: var(--surface-hover);
|
| 254 |
padding: 4px;
|
| 255 |
border-radius: var(--radius-pill);
|
| 256 |
-
margin: 1rem 1.5rem;
|
| 257 |
}
|
| 258 |
|
| 259 |
.pill {
|
|
@@ -398,6 +420,30 @@
|
|
| 398 |
padding: 0;
|
| 399 |
}
|
| 400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
/* Footer Actions */
|
| 402 |
.tools-footer {
|
| 403 |
padding: 1.5rem;
|
|
@@ -405,6 +451,8 @@
|
|
| 405 |
display: flex;
|
| 406 |
gap: 1rem;
|
| 407 |
background: var(--surface);
|
|
|
|
|
|
|
| 408 |
}
|
| 409 |
|
| 410 |
.btn-dl {
|
|
@@ -514,7 +562,11 @@
|
|
| 514 |
<span class="material-symbols-rounded">filter_b_and_w</span>
|
| 515 |
RemoveBG
|
| 516 |
</a>
|
| 517 |
-
<div
|
|
|
|
|
|
|
|
|
|
|
|
|
| 518 |
<button class="btn-icon" title="API Settings" onclick="promptApiKey()">
|
| 519 |
<span class="material-symbols-rounded">settings</span>
|
| 520 |
</button>
|
|
@@ -590,12 +642,35 @@
|
|
| 590 |
'https://images.pexels.com/photos/1761279/pexels-photo-1761279.jpeg?auto=compress&cs=tinysrgb&h=350',
|
| 591 |
'https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&h=350',
|
| 592 |
'https://images.pexels.com/photos/1072179/pexels-photo-1072179.jpeg?auto=compress&cs=tinysrgb&h=350',
|
| 593 |
-
'https://images.pexels.com/photos/1166209/pexels-photo-1166209.jpeg?auto=compress&cs=tinysrgb&h=350'
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 597 |
};
|
| 598 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
// Elements
|
| 600 |
const uploadView = document.getElementById('uploadView');
|
| 601 |
const editorView = document.getElementById('editorView');
|
|
@@ -673,7 +748,6 @@
|
|
| 673 |
`;
|
| 674 |
toolsContent.appendChild(searchRow);
|
| 675 |
|
| 676 |
-
// Enter key
|
| 677 |
setTimeout(() => {
|
| 678 |
document.getElementById('pexelsSearch').addEventListener('keypress', (e) => {
|
| 679 |
if (e.key === 'Enter') searchPexels();
|
|
@@ -708,26 +782,23 @@
|
|
| 708 |
|
| 709 |
grid.appendChild(uploadCard);
|
| 710 |
|
| 711 |
-
// Initial Content (Curated)
|
| 712 |
state.curatedPhotos.forEach(src => {
|
| 713 |
const el = document.createElement('div');
|
| 714 |
el.className = 'card-opt';
|
| 715 |
el.style.backgroundImage = `url(${src})`;
|
| 716 |
-
el.onclick = () => setBg('image', src.replace('h=350', 'w=1920'));
|
| 717 |
-
// For Pexels strictly:
|
| 718 |
-
// tiny -> original usually works by replacing query params if we knew them.
|
| 719 |
-
// For static list, we just use the src.
|
| 720 |
if (state.bgValue === src) el.classList.add('selected');
|
| 721 |
grid.appendChild(el);
|
| 722 |
});
|
| 723 |
toolsContent.appendChild(grid);
|
| 724 |
}
|
| 725 |
else if (state.currentTab === 'color') {
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
|
|
|
| 731 |
|
| 732 |
const pickerRow = document.createElement('div');
|
| 733 |
pickerRow.className = 'color-picker-row';
|
|
@@ -743,23 +814,16 @@
|
|
| 743 |
thumb.appendChild(inp);
|
| 744 |
|
| 745 |
const txt = document.createElement('span');
|
| 746 |
-
txt.innerText = '
|
| 747 |
txt.style.fontWeight = '500';
|
| 748 |
|
| 749 |
pickerRow.appendChild(thumb);
|
| 750 |
pickerRow.appendChild(txt);
|
| 751 |
toolsContent.appendChild(pickerRow);
|
| 752 |
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
label2.className = 'section-label';
|
| 756 |
-
label2.innerText = 'Presets';
|
| 757 |
-
toolsContent.appendChild(label2);
|
| 758 |
-
|
| 759 |
-
const grid = document.createElement('div');
|
| 760 |
-
grid.className = 'grid-cards';
|
| 761 |
|
| 762 |
-
// Transparent
|
| 763 |
const trans = document.createElement('div');
|
| 764 |
trans.className = 'card-opt';
|
| 765 |
trans.style.backgroundImage = `linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%), linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%)`;
|
|
@@ -767,7 +831,7 @@
|
|
| 767 |
trans.title = 'Transparent';
|
| 768 |
trans.onclick = () => setBg('none', '');
|
| 769 |
if (state.bgType === 'none') trans.classList.add('selected');
|
| 770 |
-
|
| 771 |
|
| 772 |
state.colors.forEach(c => {
|
| 773 |
const el = document.createElement('div');
|
|
@@ -775,9 +839,64 @@
|
|
| 775 |
el.style.backgroundColor = c;
|
| 776 |
el.onclick = () => setBg('color', c);
|
| 777 |
if (state.bgValue === c) el.classList.add('selected');
|
| 778 |
-
|
| 779 |
});
|
| 780 |
-
toolsContent.appendChild(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 781 |
}
|
| 782 |
}
|
| 783 |
|
|
@@ -786,18 +905,15 @@
|
|
| 786 |
if (!q) return;
|
| 787 |
|
| 788 |
if (!state.apiKey) {
|
| 789 |
-
alert('Pexels API Key missing.
|
| 790 |
promptApiKey();
|
| 791 |
return;
|
| 792 |
}
|
| 793 |
|
| 794 |
const grid = document.getElementById('photoGrid');
|
| 795 |
-
// Keep upload button
|
| 796 |
const upBtn = grid.firstElementChild;
|
| 797 |
grid.innerHTML = '';
|
| 798 |
grid.appendChild(upBtn);
|
| 799 |
-
|
| 800 |
-
// Loader in grid?
|
| 801 |
const loading = document.createElement('div'); loading.innerText = 'Searching...';
|
| 802 |
grid.appendChild(loading);
|
| 803 |
|
|
@@ -806,22 +922,19 @@
|
|
| 806 |
headers: { 'Authorization': state.apiKey }
|
| 807 |
});
|
| 808 |
const data = await res.json();
|
| 809 |
-
|
| 810 |
grid.removeChild(loading);
|
| 811 |
-
|
| 812 |
if (data.photos) {
|
| 813 |
data.photos.forEach(p => {
|
| 814 |
const el = document.createElement('div');
|
| 815 |
el.className = 'card-opt';
|
| 816 |
el.style.backgroundImage = `url(${p.src.tiny})`;
|
| 817 |
-
// On click use large
|
| 818 |
el.onclick = () => setBg('image', p.src.large2x || p.src.large);
|
| 819 |
grid.appendChild(el);
|
| 820 |
});
|
| 821 |
}
|
| 822 |
} catch (e) {
|
| 823 |
console.error(e);
|
| 824 |
-
alert('Pexels Search Failed.
|
| 825 |
}
|
| 826 |
}
|
| 827 |
|
|
@@ -829,8 +942,6 @@
|
|
| 829 |
state.bgType = type;
|
| 830 |
state.bgValue = val;
|
| 831 |
updatePreview();
|
| 832 |
-
|
| 833 |
-
// Re-render to update selected border
|
| 834 |
renderTools();
|
| 835 |
}
|
| 836 |
|
|
@@ -853,28 +964,53 @@
|
|
| 853 |
|
| 854 |
// Draw Background
|
| 855 |
if (state.bgType === 'color') {
|
| 856 |
-
|
| 857 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
drawSubject(ctx, canvas);
|
| 859 |
} else if (state.bgType === 'image') {
|
| 860 |
const img = new Image();
|
| 861 |
img.crossOrigin = "Anonymous";
|
| 862 |
img.src = state.bgValue;
|
| 863 |
img.onload = () => {
|
| 864 |
-
// Object fit cover
|
| 865 |
const sRatio = img.width / img.height;
|
| 866 |
const dRatio = canvas.width / canvas.height;
|
| 867 |
let sw, sh, sx, sy;
|
| 868 |
if (sRatio > dRatio) { sh = img.height; sw = sh * dRatio; sx = (img.width - sw) / 2; sy = 0; }
|
| 869 |
else { sw = img.width; sh = sw / dRatio; sx = 0; sy = (img.height - sh) / 2; }
|
| 870 |
-
|
| 871 |
ctx.drawImage(img, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
|
| 872 |
drawSubject(ctx, canvas);
|
| 873 |
};
|
| 874 |
-
img.onerror = () =>
|
| 875 |
-
alert('Could not load background image for download (CORS issue?). Try uploading a local file.');
|
| 876 |
-
drawSubject(ctx, canvas); // Download without bg if fail
|
| 877 |
-
}
|
| 878 |
} else {
|
| 879 |
drawSubject(ctx, canvas);
|
| 880 |
}
|
|
@@ -888,10 +1024,9 @@
|
|
| 888 |
a.click();
|
| 889 |
}
|
| 890 |
|
| 891 |
-
// --- Helpers ---
|
| 892 |
function promptApiKey() {
|
| 893 |
-
const cur =
|
| 894 |
-
const key = prompt("Enter Pexels API Key
|
| 895 |
if (key !== null) {
|
| 896 |
localStorage.setItem('pexels_api_key', key.trim());
|
| 897 |
state.apiKey = key.trim();
|
|
|
|
| 75 |
font-size: 32px;
|
| 76 |
}
|
| 77 |
|
| 78 |
+
.nav-actions {
|
| 79 |
+
display: flex;
|
| 80 |
+
gap: 12px;
|
| 81 |
+
align-items: center;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
/* CPU Badge */
|
| 85 |
+
.cpu-badge {
|
| 86 |
+
display: flex;
|
| 87 |
+
align-items: center;
|
| 88 |
+
gap: 6px;
|
| 89 |
+
padding: 6px 12px;
|
| 90 |
+
border-radius: var(--radius-pill);
|
| 91 |
+
background: var(--surface-hover);
|
| 92 |
+
font-size: 0.85rem;
|
| 93 |
+
font-weight: 600;
|
| 94 |
+
color: var(--text-sec);
|
| 95 |
+
border: 1px solid var(--border);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
.cpu-dot {
|
| 99 |
+
width: 8px;
|
| 100 |
+
height: 8px;
|
| 101 |
+
border-radius: 50%;
|
| 102 |
+
background: #22c55e;
|
| 103 |
+
transition: background 0.3s;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
.cpu-badge.high .cpu-dot {
|
| 107 |
+
background: #ef4444;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
.cpu-badge.med .cpu-dot {
|
| 111 |
+
background: #eab308;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
/* --- Main Layout --- */
|
| 115 |
main {
|
| 116 |
flex: 1;
|
|
|
|
| 145 |
background: linear-gradient(135deg, #0f172a 0%, #334155 100%);
|
| 146 |
-webkit-background-clip: text;
|
| 147 |
-webkit-text-fill-color: transparent;
|
| 148 |
+
background-clip: text;
|
| 149 |
+
/* Fix lint */
|
| 150 |
}
|
| 151 |
|
| 152 |
.hero p {
|
|
|
|
| 239 |
overflow: hidden;
|
| 240 |
box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.18);
|
| 241 |
background: transparent;
|
|
|
|
| 242 |
transition: all 0.3s ease;
|
| 243 |
}
|
| 244 |
|
|
|
|
| 269 |
box-shadow: -4px 0 24px rgba(0, 0, 0, 0.04);
|
| 270 |
}
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
/* Pills Tab */
|
| 273 |
.tabs-pills {
|
| 274 |
display: flex;
|
| 275 |
background: var(--surface-hover);
|
| 276 |
padding: 4px;
|
| 277 |
border-radius: var(--radius-pill);
|
| 278 |
+
margin: 1.5rem 1.5rem 1rem 1.5rem;
|
| 279 |
}
|
| 280 |
|
| 281 |
.pill {
|
|
|
|
| 420 |
padding: 0;
|
| 421 |
}
|
| 422 |
|
| 423 |
+
/* Gradient Builder */
|
| 424 |
+
.grad-builder {
|
| 425 |
+
background: var(--surface-hover);
|
| 426 |
+
padding: 12px;
|
| 427 |
+
border-radius: var(--radius-md);
|
| 428 |
+
display: flex;
|
| 429 |
+
flex-direction: column;
|
| 430 |
+
gap: 10px;
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
.grad-controls {
|
| 434 |
+
display: flex;
|
| 435 |
+
gap: 8px;
|
| 436 |
+
align-items: center;
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
.grad-angle-input {
|
| 440 |
+
width: 50px;
|
| 441 |
+
padding: 6px;
|
| 442 |
+
border: 1px solid var(--border);
|
| 443 |
+
border-radius: 6px;
|
| 444 |
+
text-align: center;
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
/* Footer Actions */
|
| 448 |
.tools-footer {
|
| 449 |
padding: 1.5rem;
|
|
|
|
| 451 |
display: flex;
|
| 452 |
gap: 1rem;
|
| 453 |
background: var(--surface);
|
| 454 |
+
border-radius: 0 0 0 24px;
|
| 455 |
+
/* Slight polish */
|
| 456 |
}
|
| 457 |
|
| 458 |
.btn-dl {
|
|
|
|
| 562 |
<span class="material-symbols-rounded">filter_b_and_w</span>
|
| 563 |
RemoveBG
|
| 564 |
</a>
|
| 565 |
+
<div class="nav-actions">
|
| 566 |
+
<div class="cpu-badge" title="Server CPU Usage">
|
| 567 |
+
<div class="cpu-dot" id="cpuDot"></div>
|
| 568 |
+
<span id="cpuText">0%</span>
|
| 569 |
+
</div>
|
| 570 |
<button class="btn-icon" title="API Settings" onclick="promptApiKey()">
|
| 571 |
<span class="material-symbols-rounded">settings</span>
|
| 572 |
</button>
|
|
|
|
| 642 |
'https://images.pexels.com/photos/1761279/pexels-photo-1761279.jpeg?auto=compress&cs=tinysrgb&h=350',
|
| 643 |
'https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&h=350',
|
| 644 |
'https://images.pexels.com/photos/1072179/pexels-photo-1072179.jpeg?auto=compress&cs=tinysrgb&h=350',
|
| 645 |
+
'https://images.pexels.com/photos/1166209/pexels-photo-1166209.jpeg?auto=compress&cs=tinysrgb&h=350'
|
| 646 |
+
],
|
| 647 |
+
colors: ['#ffffff', '#f4f4f5', '#e4e4e7', '#fecaca', '#fde68a', '#bbf7d0', '#bfdbfe', '#ddd6fe', '#fbcfe8', '#000000'],
|
| 648 |
+
gradients: [
|
| 649 |
+
'linear-gradient(to right, #ffecd2 0%, #fcb69f 100%)',
|
| 650 |
+
'linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%)',
|
| 651 |
+
'linear-gradient(120deg, #d4fc79 0%, #96e6a1 100%)',
|
| 652 |
+
'linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%)',
|
| 653 |
+
'linear-gradient(to top, #fbc2eb 0%, #a6c1ee 100%)'
|
| 654 |
+
]
|
| 655 |
};
|
| 656 |
|
| 657 |
+
// CPU Monitor
|
| 658 |
+
setInterval(() => {
|
| 659 |
+
fetch('/cpu')
|
| 660 |
+
.then(r => r.json())
|
| 661 |
+
.then(d => {
|
| 662 |
+
const el = document.getElementById('cpuText');
|
| 663 |
+
const dot = document.getElementById('cpuDot');
|
| 664 |
+
const usage = d.cpu_percent;
|
| 665 |
+
el.innerText = usage + '%';
|
| 666 |
+
|
| 667 |
+
document.querySelector('.cpu-badge').classList.remove('high', 'med');
|
| 668 |
+
if (usage > 80) document.querySelector('.cpu-badge').classList.add('high');
|
| 669 |
+
else if (usage > 50) document.querySelector('.cpu-badge').classList.add('med');
|
| 670 |
+
})
|
| 671 |
+
.catch(() => { });
|
| 672 |
+
}, 3000);
|
| 673 |
+
|
| 674 |
// Elements
|
| 675 |
const uploadView = document.getElementById('uploadView');
|
| 676 |
const editorView = document.getElementById('editorView');
|
|
|
|
| 748 |
`;
|
| 749 |
toolsContent.appendChild(searchRow);
|
| 750 |
|
|
|
|
| 751 |
setTimeout(() => {
|
| 752 |
document.getElementById('pexelsSearch').addEventListener('keypress', (e) => {
|
| 753 |
if (e.key === 'Enter') searchPexels();
|
|
|
|
| 782 |
|
| 783 |
grid.appendChild(uploadCard);
|
| 784 |
|
|
|
|
| 785 |
state.curatedPhotos.forEach(src => {
|
| 786 |
const el = document.createElement('div');
|
| 787 |
el.className = 'card-opt';
|
| 788 |
el.style.backgroundImage = `url(${src})`;
|
| 789 |
+
el.onclick = () => setBg('image', src.replace('h=350', 'w=1920'));
|
|
|
|
|
|
|
|
|
|
| 790 |
if (state.bgValue === src) el.classList.add('selected');
|
| 791 |
grid.appendChild(el);
|
| 792 |
});
|
| 793 |
toolsContent.appendChild(grid);
|
| 794 |
}
|
| 795 |
else if (state.currentTab === 'color') {
|
| 796 |
+
|
| 797 |
+
// 1. SOLID COLOR SECTION
|
| 798 |
+
const cLabel = document.createElement('div');
|
| 799 |
+
cLabel.className = 'section-label';
|
| 800 |
+
cLabel.innerText = 'Solid Color';
|
| 801 |
+
toolsContent.appendChild(cLabel);
|
| 802 |
|
| 803 |
const pickerRow = document.createElement('div');
|
| 804 |
pickerRow.className = 'color-picker-row';
|
|
|
|
| 814 |
thumb.appendChild(inp);
|
| 815 |
|
| 816 |
const txt = document.createElement('span');
|
| 817 |
+
txt.innerText = 'Custom Color';
|
| 818 |
txt.style.fontWeight = '500';
|
| 819 |
|
| 820 |
pickerRow.appendChild(thumb);
|
| 821 |
pickerRow.appendChild(txt);
|
| 822 |
toolsContent.appendChild(pickerRow);
|
| 823 |
|
| 824 |
+
const cGrid = document.createElement('div');
|
| 825 |
+
cGrid.className = 'grid-cards';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 826 |
|
|
|
|
| 827 |
const trans = document.createElement('div');
|
| 828 |
trans.className = 'card-opt';
|
| 829 |
trans.style.backgroundImage = `linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%), linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%)`;
|
|
|
|
| 831 |
trans.title = 'Transparent';
|
| 832 |
trans.onclick = () => setBg('none', '');
|
| 833 |
if (state.bgType === 'none') trans.classList.add('selected');
|
| 834 |
+
cGrid.appendChild(trans);
|
| 835 |
|
| 836 |
state.colors.forEach(c => {
|
| 837 |
const el = document.createElement('div');
|
|
|
|
| 839 |
el.style.backgroundColor = c;
|
| 840 |
el.onclick = () => setBg('color', c);
|
| 841 |
if (state.bgValue === c) el.classList.add('selected');
|
| 842 |
+
cGrid.appendChild(el);
|
| 843 |
});
|
| 844 |
+
toolsContent.appendChild(cGrid);
|
| 845 |
+
|
| 846 |
+
// 2. GRADIENT SECTION
|
| 847 |
+
const gLabel = document.createElement('div');
|
| 848 |
+
gLabel.className = 'section-label';
|
| 849 |
+
gLabel.innerText = 'Gradients';
|
| 850 |
+
gLabel.style.marginTop = '2rem';
|
| 851 |
+
toolsContent.appendChild(gLabel);
|
| 852 |
+
|
| 853 |
+
// Custom Gradient Builder
|
| 854 |
+
const gBuilder = document.createElement('div');
|
| 855 |
+
gBuilder.className = 'grad-builder';
|
| 856 |
+
const gControls = document.createElement('div');
|
| 857 |
+
gControls.className = 'grad-controls';
|
| 858 |
+
|
| 859 |
+
const g1 = document.createElement('div'); g1.className = 'cp-thumb';
|
| 860 |
+
const g1Inp = document.createElement('input'); g1Inp.type = 'color'; g1Inp.value = '#a1c4fd';
|
| 861 |
+
g1.appendChild(g1Inp);
|
| 862 |
+
|
| 863 |
+
const g2 = document.createElement('div'); g2.className = 'cp-thumb';
|
| 864 |
+
const g2Inp = document.createElement('input'); g2Inp.type = 'color'; g2Inp.value = '#c2e9fb';
|
| 865 |
+
g2.appendChild(g2Inp);
|
| 866 |
+
|
| 867 |
+
const deg = document.createElement('input');
|
| 868 |
+
deg.className = 'grad-angle-input';
|
| 869 |
+
deg.type = 'number'; deg.value = 90;
|
| 870 |
+
|
| 871 |
+
const updateGrad = () => {
|
| 872 |
+
const val = `linear-gradient(${deg.value}deg, ${g1Inp.value}, ${g2Inp.value})`;
|
| 873 |
+
setBg('color', val);
|
| 874 |
+
};
|
| 875 |
+
|
| 876 |
+
g1Inp.oninput = updateGrad;
|
| 877 |
+
g2Inp.oninput = updateGrad;
|
| 878 |
+
deg.oninput = updateGrad;
|
| 879 |
+
|
| 880 |
+
gControls.appendChild(g1);
|
| 881 |
+
gControls.appendChild(g2);
|
| 882 |
+
gControls.appendChild(document.createTextNode('Angle:'));
|
| 883 |
+
gControls.appendChild(deg);
|
| 884 |
+
gBuilder.appendChild(gControls);
|
| 885 |
+
toolsContent.appendChild(gBuilder);
|
| 886 |
+
|
| 887 |
+
const gGrid = document.createElement('div');
|
| 888 |
+
gGrid.className = 'grid-cards';
|
| 889 |
+
gGrid.style.marginTop = '12px';
|
| 890 |
+
|
| 891 |
+
state.gradients.forEach(g => {
|
| 892 |
+
const el = document.createElement('div');
|
| 893 |
+
el.className = 'card-opt';
|
| 894 |
+
el.style.backgroundImage = g;
|
| 895 |
+
el.onclick = () => setBg('color', g);
|
| 896 |
+
if (state.bgValue === g) el.classList.add('selected');
|
| 897 |
+
gGrid.appendChild(el);
|
| 898 |
+
});
|
| 899 |
+
toolsContent.appendChild(gGrid);
|
| 900 |
}
|
| 901 |
}
|
| 902 |
|
|
|
|
| 905 |
if (!q) return;
|
| 906 |
|
| 907 |
if (!state.apiKey) {
|
| 908 |
+
alert('Pexels API Key missing.');
|
| 909 |
promptApiKey();
|
| 910 |
return;
|
| 911 |
}
|
| 912 |
|
| 913 |
const grid = document.getElementById('photoGrid');
|
|
|
|
| 914 |
const upBtn = grid.firstElementChild;
|
| 915 |
grid.innerHTML = '';
|
| 916 |
grid.appendChild(upBtn);
|
|
|
|
|
|
|
| 917 |
const loading = document.createElement('div'); loading.innerText = 'Searching...';
|
| 918 |
grid.appendChild(loading);
|
| 919 |
|
|
|
|
| 922 |
headers: { 'Authorization': state.apiKey }
|
| 923 |
});
|
| 924 |
const data = await res.json();
|
|
|
|
| 925 |
grid.removeChild(loading);
|
|
|
|
| 926 |
if (data.photos) {
|
| 927 |
data.photos.forEach(p => {
|
| 928 |
const el = document.createElement('div');
|
| 929 |
el.className = 'card-opt';
|
| 930 |
el.style.backgroundImage = `url(${p.src.tiny})`;
|
|
|
|
| 931 |
el.onclick = () => setBg('image', p.src.large2x || p.src.large);
|
| 932 |
grid.appendChild(el);
|
| 933 |
});
|
| 934 |
}
|
| 935 |
} catch (e) {
|
| 936 |
console.error(e);
|
| 937 |
+
alert('Pexels Search Failed.');
|
| 938 |
}
|
| 939 |
}
|
| 940 |
|
|
|
|
| 942 |
state.bgType = type;
|
| 943 |
state.bgValue = val;
|
| 944 |
updatePreview();
|
|
|
|
|
|
|
| 945 |
renderTools();
|
| 946 |
}
|
| 947 |
|
|
|
|
| 964 |
|
| 965 |
// Draw Background
|
| 966 |
if (state.bgType === 'color') {
|
| 967 |
+
if (state.bgValue.startsWith('linear-gradient')) {
|
| 968 |
+
try {
|
| 969 |
+
let deg = 90;
|
| 970 |
+
const degMatch = state.bgValue.match(/(\d+)deg/);
|
| 971 |
+
if (degMatch) deg = parseInt(degMatch[1]);
|
| 972 |
+
const colorMatch = state.bgValue.match(/#[a-fA-F0-9]{6}|#[a-fA-F0-9]{3}|rgb\([^)]+\)/g);
|
| 973 |
+
|
| 974 |
+
if (colorMatch && colorMatch.length >= 2) {
|
| 975 |
+
const w = canvas.width, h = canvas.height;
|
| 976 |
+
const angleRad = (deg - 90) * (Math.PI / 180);
|
| 977 |
+
const x1 = w / 2 + Math.cos(angleRad + Math.PI) * w / 2;
|
| 978 |
+
const y1 = h / 2 + Math.sin(angleRad + Math.PI) * h / 2;
|
| 979 |
+
const x2 = w / 2 + Math.cos(angleRad) * w / 2;
|
| 980 |
+
const y2 = h / 2 + Math.sin(angleRad) * h / 2;
|
| 981 |
+
|
| 982 |
+
const grd = ctx.createLinearGradient(x1, y1, x2, y2);
|
| 983 |
+
grd.addColorStop(0, colorMatch[0]);
|
| 984 |
+
grd.addColorStop(1, colorMatch[1]);
|
| 985 |
+
ctx.fillStyle = grd;
|
| 986 |
+
ctx.fillRect(0, 0, w, h);
|
| 987 |
+
} else {
|
| 988 |
+
ctx.fillStyle = '#ffffff';
|
| 989 |
+
ctx.fillRect(0, 0, w, h);
|
| 990 |
+
}
|
| 991 |
+
} catch (e) {
|
| 992 |
+
ctx.fillStyle = '#ffffff';
|
| 993 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 994 |
+
}
|
| 995 |
+
} else {
|
| 996 |
+
ctx.fillStyle = state.bgValue;
|
| 997 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 998 |
+
}
|
| 999 |
drawSubject(ctx, canvas);
|
| 1000 |
} else if (state.bgType === 'image') {
|
| 1001 |
const img = new Image();
|
| 1002 |
img.crossOrigin = "Anonymous";
|
| 1003 |
img.src = state.bgValue;
|
| 1004 |
img.onload = () => {
|
|
|
|
| 1005 |
const sRatio = img.width / img.height;
|
| 1006 |
const dRatio = canvas.width / canvas.height;
|
| 1007 |
let sw, sh, sx, sy;
|
| 1008 |
if (sRatio > dRatio) { sh = img.height; sw = sh * dRatio; sx = (img.width - sw) / 2; sy = 0; }
|
| 1009 |
else { sw = img.width; sh = sw / dRatio; sx = 0; sy = (img.height - sh) / 2; }
|
|
|
|
| 1010 |
ctx.drawImage(img, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
|
| 1011 |
drawSubject(ctx, canvas);
|
| 1012 |
};
|
| 1013 |
+
img.onerror = () => drawSubject(ctx, canvas);
|
|
|
|
|
|
|
|
|
|
| 1014 |
} else {
|
| 1015 |
drawSubject(ctx, canvas);
|
| 1016 |
}
|
|
|
|
| 1024 |
a.click();
|
| 1025 |
}
|
| 1026 |
|
|
|
|
| 1027 |
function promptApiKey() {
|
| 1028 |
+
const cur = state.apiKey;
|
| 1029 |
+
const key = prompt("Enter Pexels API Key:", cur);
|
| 1030 |
if (key !== null) {
|
| 1031 |
localStorage.setItem('pexels_api_key', key.trim());
|
| 1032 |
state.apiKey = key.trim();
|