Spaces:
Paused
Paused
Download templates/index.html from veltrixcode/helper-bpt: direct link, hf CLI and curl.
- Browser
- Download file 36 kB
-
https://huggingface.co/spaces/veltrixcode/helper-bpt/resolve/main/templates/index.html
- Command line
-
hf download hf://spaces/veltrixcode/helper-bpt/templates/index.html
-
curl -L -o index.html https://huggingface.co/spaces/veltrixcode/helper-bpt/resolve/main/templates/index.html
36 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Remove Background - Photo Editor</title>
<!-- Google Fonts: Inter -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<!-- Google Material Symbols -->
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@24,400,0,0" />
<style>
:root {
--primary: #2563eb;
--primary-hover: #1d4ed8;
--bg-page: #f8fafc;
--surface: #ffffff;
--surface-hover: #f1f5f9;
--text-main: #0f172a;
--text-sec: #64748b;
--border: #e2e8f0;
--shadow-sm: 0 1px 2px -1px rgba(0, 0, 0, 0.08);
--shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
--radius-xl: 24px;
--radius-md: 12px;
--radius-pill: 9999px;
--h-header: 70px;
}
/* --- Base & Reset --- */
* {
box-sizing: border-box;
outline: none;
}
body {
font-family: 'Inter', sans-serif;
background-color: var(--bg-page);
margin: 0;
color: var(--text-main);
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
/* App-like feel */
}
/* --- Navbar --- */
nav {
height: var(--h-header);
background: var(--surface);
padding: 0 2rem;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid var(--border);
z-index: 50;
}
.brand {
display: flex;
align-items: center;
gap: 10px;
font-weight: 700;
font-size: 1.35rem;
color: #334155;
text-decoration: none;
letter-spacing: -0.5px;
}
.brand span {
color: var(--primary);
font-size: 32px;
}
.nav-actions {
display: flex;
gap: 12px;
align-items: center;
}
/* CPU Badge */
.cpu-badge {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 12px;
border-radius: var(--radius-pill);
background: var(--surface-hover);
font-size: 0.85rem;
font-weight: 600;
color: var(--text-sec);
border: 1px solid var(--border);
}
.cpu-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #22c55e;
transition: background 0.3s;
}
.cpu-badge.high .cpu-dot {
background: #ef4444;
}
.cpu-badge.med .cpu-dot {
background: #eab308;
}
/* --- Main Layout --- */
main {
flex: 1;
display: flex;
position: relative;
overflow: hidden;
}
/* UPLOAD STATE (Centered) */
.upload-view {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: var(--bg-page);
z-index: 10;
transition: opacity 0.3s ease;
}
.hero {
text-align: center;
margin-bottom: 3rem;
animation: fadeIn 0.8s ease;
}
.hero h1 {
font-size: 3.5rem;
margin: 0 0 1rem 0;
letter-spacing: -1px;
background: linear-gradient(135deg, #0f172a 0%, #334155 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
/* Fix lint */
}
.hero p {
font-size: 1.25rem;
color: var(--text-sec);
margin: 0;
}
.drop-zone {
width: 100%;
max-width: 600px;
background: var(--surface);
border: 2px dashed #cbd5e1;
border-radius: var(--radius-xl);
padding: 4rem 2rem;
text-align: center;
cursor: pointer;
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
box-shadow: var(--shadow-sm);
}
.drop-zone:hover,
.drop-zone.dragover {
border-color: var(--primary);
transform: translateY(-4px);
box-shadow: var(--shadow-lg);
}
.btn-upload {
background: var(--primary);
color: white;
font-size: 1.1rem;
font-weight: 600;
padding: 16px 40px;
border-radius: var(--radius-pill);
border: none;
cursor: pointer;
display: inline-flex;
align-items: center;
gap: 10px;
box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
transition: transform 0.1s;
}
.btn-upload:hover {
transform: scale(1.02);
background: #1e40af;
}
.upload-hint {
margin-top: 1.5rem;
color: var(--text-sec);
font-size: 0.95rem;
}
/* EDITOR STATE (Split View) */
.editor-view {
display: none;
/* hidden by default */
width: 100%;
height: 100%;
flex-direction: row;
}
/* Editor: Stage (Left/Center) */
.stage-area {
flex: 1;
background: #e2e8f0;
display: flex;
align-items: center;
justify-content: center;
padding: 2rem;
position: relative;
/* Checkerboard */
background-image:
linear-gradient(45deg, #cbd5e1 25%, transparent 25%),
linear-gradient(-45deg, #cbd5e1 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #cbd5e1 75%),
linear-gradient(-45deg, transparent 75%, #cbd5e1 75%);
background-size: 24px 24px;
background-position: 0 0, 0 12px, 12px -12px, -12px 0px;
background-color: #f1f5f9;
}
.canvas-wrapper {
position: relative;
max-width: 90%;
max-height: 90%;
border-radius: var(--radius-md);
overflow: hidden;
box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.18);
background: transparent;
transition: all 0.3s ease;
}
.layer-bg {
position: absolute;
inset: 0;
background-size: cover;
background-position: center;
z-index: 1;
}
.layer-subject {
position: relative;
z-index: 2;
display: block;
max-height: 80vh;
max-width: 100%;
}
/* Editor: Tools Panel (Right - Floating Style) */
.tools-panel {
width: 420px;
background: var(--surface);
border-left: 1px solid var(--border);
display: flex;
flex-direction: column;
z-index: 20;
box-shadow: -4px 0 24px rgba(0, 0, 0, 0.04);
}
/* Pills Tab */
.tabs-pills {
display: flex;
background: var(--surface-hover);
padding: 4px;
border-radius: var(--radius-pill);
margin: 1.5rem 1.5rem 1rem 1.5rem;
}
.pill {
flex: 1;
text-align: center;
padding: 8px;
font-size: 0.9rem;
font-weight: 600;
color: var(--text-sec);
border-radius: var(--radius-pill);
cursor: pointer;
transition: all 0.2s;
}
.pill.active {
background: white;
color: var(--text-main);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
/* Tools Content */
.tools-content {
flex: 1;
overflow-y: auto;
padding: 0 1.5rem 1.5rem 1.5rem;
}
.section-label {
font-size: 0.8rem;
font-weight: 700;
color: var(--text-sec);
text-transform: uppercase;
letter-spacing: 0.5px;
margin: 1.5rem 0 0.75rem 0;
}
/* Search Bar */
.search-row {
display: flex;
gap: 8px;
margin-bottom: 1rem;
}
.search-input {
flex: 1;
background: var(--surface-hover);
border: 1px solid transparent;
padding: 10px 16px;
border-radius: var(--radius-md);
font-family: inherit;
}
.search-input:focus {
background: white;
border-color: var(--primary);
}
.btn-icon {
background: var(--surface-hover);
border: none;
width: 42px;
border-radius: var(--radius-md);
cursor: pointer;
color: var(--text-sec);
display: flex;
align-items: center;
justify-content: center;
}
.btn-icon:hover {
background: #e2e8f0;
color: var(--text-main);
}
/* Grids */
.grid-cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
gap: 12px;
}
.card-opt {
aspect-ratio: 1;
border-radius: var(--radius-md);
overflow: hidden;
cursor: pointer;
border: 2px solid transparent;
position: relative;
background-size: cover;
background-position: center;
transition: transform 0.1s;
}
.card-opt:hover {
transform: scale(1.05);
}
.card-opt.selected {
border-color: var(--primary);
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
}
.card-opt.upload-pl {
background: #f1f5f9;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: var(--text-sec);
font-size: 0.8rem;
gap: 4px;
border: 2px dashed #cbd5e1;
}
/* Color Inputs */
.color-picker-row {
display: flex;
gap: 12px;
align-items: center;
background: var(--surface-hover);
padding: 12px;
border-radius: var(--radius-md);
margin-bottom: 12px;
}
.cp-thumb {
width: 32px;
height: 32px;
border-radius: 50%;
border: 2px solid white;
box-shadow: 0 0 0 1px #cbd5e1;
overflow: hidden;
position: relative;
}
input[type="color"] {
position: absolute;
inset: -5px;
width: 150%;
height: 150%;
cursor: pointer;
padding: 0;
}
/* Gradient Builder */
.grad-builder {
background: var(--surface-hover);
padding: 12px;
border-radius: var(--radius-md);
display: flex;
flex-direction: column;
gap: 10px;
}
.grad-controls {
display: flex;
gap: 8px;
align-items: center;
}
.grad-angle-input {
width: 50px;
padding: 6px;
border: 1px solid var(--border);
border-radius: 6px;
text-align: center;
}
/* Footer Actions */
.tools-footer {
padding: 1.5rem;
border-top: 1px solid var(--border);
display: flex;
gap: 1rem;
background: var(--surface);
border-radius: 0 0 0 24px;
/* Slight polish */
}
.btn-dl {
flex: 1;
background: var(--primary);
color: white;
padding: 14px;
border-radius: 50px;
font-weight: 600;
border: none;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
box-shadow: 0 4px 12px rgba(37, 99, 235, 0.25);
transition: background 0.2s;
}
.btn-dl:hover {
background: var(--primary-hover);
}
.btn-ghost {
padding: 14px;
color: var(--text-sec);
background: transparent;
font-weight: 600;
border: none;
cursor: pointer;
border-radius: 50px;
}
.btn-ghost:hover {
background: var(--surface-hover);
color: var(--text-main);
}
/* Loader */
.loader-overlay {
position: fixed;
inset: 0;
background: rgba(255, 255, 255, 0.9);
z-index: 100;
display: none;
flex-direction: column;
align-items: center;
justify-content: center;
}
.spinner {
width: 56px;
height: 56px;
border: 4px solid #e2e8f0;
border-top-color: var(--primary);
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Mobile */
@media (max-width: 900px) {
.hero h1 {
font-size: 2.25rem;
}
.editor-view {
flex-direction: column;
}
.stage-area {
flex: 1;
padding: 1rem;
}
.tools-panel {
width: 100%;
height: 50%;
border-radius: 20px 20px 0 0;
box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
}
}
</style>
</head>
<body>
<nav>
<a href="#" class="brand">
<span class="material-symbols-rounded">filter_b_and_w</span>
RemoveBG
</a>
<div class="nav-actions">
<div class="cpu-badge" title="Server CPU Usage">
<div class="cpu-dot" id="cpuDot"></div>
<span id="cpuText">0%</span>
</div>
<button class="btn-icon" title="API Settings" onclick="promptApiKey()">
<span class="material-symbols-rounded">settings</span>
</button>
</div>
</nav>
<main>
<!-- Upload Screen -->
<div id="uploadView" class="upload-view">
<div class="hero">
<h1>Remove Backgrounds<br>100% Automatically</h1>
<p>Upload an image to get a transparent background in seconds.</p>
</div>
<div class="drop-zone" id="dropZone" onclick="document.getElementById('fileInput').click()">
<button class="btn-upload">
<span class="material-symbols-rounded">upload</span>
Upload Image
</button>
<div class="upload-hint">or drop a file here</div>
</div>
<input type="file" id="fileInput" accept="image/*" hidden>
</div>
<!-- Editor Screen -->
<div id="editorView" class="editor-view">
<div class="stage-area">
<div class="canvas-wrapper" id="canvasWrapper">
<div id="bgLayer" class="layer-bg"></div>
<img id="subjectImg" class="layer-subject" src="" alt="Subject">
</div>
</div>
<div class="tools-panel">
<div class="tabs-pills">
<div class="pill active" onclick="switchTab('photo')">Photo</div>
<div class="pill" onclick="switchTab('color')">Color</div>
</div>
<div class="tools-content" id="toolsContent">
<!-- Dynamic Content -->
</div>
<div class="tools-footer">
<button class="btn-ghost" onclick="resetApp()">New</button>
<button class="btn-dl" onclick="downloadResult()">
<span class="material-symbols-rounded">download</span>
Download
</button>
</div>
</div>
</div>
<!-- Loader -->
<div id="loader" class="loader-overlay">
<div class="spinner"></div>
<div style="margin-top: 1rem; font-weight: 500; color: #64748b;">Processing Image...</div>
</div>
</main>
<script>
// State
const state = {
currentTab: 'photo',
bgType: 'none', // none, color, image
bgValue: '',
apiKey: 'wCwCTx5e6SExjsLckZoDEyCIK4sNE1rbH6JenhPciPQlbvUDg7FDhChl',
curatedPhotos: [
'https://images.pexels.com/photos/206359/pexels-photo-206359.jpeg?auto=compress&cs=tinysrgb&h=350',
'https://images.pexels.com/photos/1761279/pexels-photo-1761279.jpeg?auto=compress&cs=tinysrgb&h=350',
'https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&h=350',
'https://images.pexels.com/photos/1072179/pexels-photo-1072179.jpeg?auto=compress&cs=tinysrgb&h=350',
'https://images.pexels.com/photos/1166209/pexels-photo-1166209.jpeg?auto=compress&cs=tinysrgb&h=350'
],
colors: ['#ffffff', '#f4f4f5', '#e4e4e7', '#fecaca', '#fde68a', '#bbf7d0', '#bfdbfe', '#ddd6fe', '#fbcfe8', '#000000'],
gradients: [
'linear-gradient(to right, #ffecd2 0%, #fcb69f 100%)',
'linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%)',
'linear-gradient(120deg, #d4fc79 0%, #96e6a1 100%)',
'linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%)',
'linear-gradient(to top, #fbc2eb 0%, #a6c1ee 100%)'
]
};
// CPU Monitor
setInterval(() => {
fetch('/cpu')
.then(r => r.json())
.then(d => {
const el = document.getElementById('cpuText');
const dot = document.getElementById('cpuDot');
const usage = d.cpu_percent;
el.innerText = usage + '%';
document.querySelector('.cpu-badge').classList.remove('high', 'med');
if (usage > 80) document.querySelector('.cpu-badge').classList.add('high');
else if (usage > 50) document.querySelector('.cpu-badge').classList.add('med');
})
.catch(() => { });
}, 3000);
// Elements
const uploadView = document.getElementById('uploadView');
const editorView = document.getElementById('editorView');
const loader = document.getElementById('loader');
const subjectImg = document.getElementById('subjectImg');
const bgLayer = document.getElementById('bgLayer');
const toolsContent = document.getElementById('toolsContent');
// --- Upload Logic ---
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
dropZone.addEventListener('dragover', e => { e.preventDefault(); dropZone.classList.add('dragover'); });
dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
dropZone.addEventListener('drop', e => {
e.preventDefault();
dropZone.classList.remove('dragover');
if (e.dataTransfer.files[0]) handleFile(e.dataTransfer.files[0]);
});
fileInput.addEventListener('change', e => {
if (e.target.files[0]) handleFile(e.target.files[0]);
});
function handleFile(file) {
if (!file.type.startsWith('image/')) return alert('Please upload an image.');
showLoader(true);
const fd = new FormData();
fd.append('file', file);
fetch('/', { method: 'POST', body: fd })
.then(r => r.blob())
.then(blob => {
const url = URL.createObjectURL(blob);
subjectImg.src = url;
// Reset State
state.bgType = 'none';
state.bgValue = '';
updatePreview();
showLoader(false);
uploadView.style.opacity = 0;
setTimeout(() => {
uploadView.style.display = 'none';
editorView.style.display = 'flex';
renderTools();
}, 300);
})
.catch(err => {
console.error(err);
alert('Error removing background');
showLoader(false);
});
}
// --- Editor Logic ---
function switchTab(tab) {
state.currentTab = tab;
document.querySelectorAll('.pill').forEach(p => p.classList.remove('active'));
document.querySelectorAll('.pill')[tab === 'photo' ? 0 : 1].classList.add('active');
renderTools();
}
function renderTools() {
toolsContent.innerHTML = '';
if (state.currentTab === 'photo') {
// Search Bar
const searchRow = document.createElement('div');
searchRow.className = 'search-row';
searchRow.innerHTML = `
<input type="text" id="pexelsSearch" class="search-input" placeholder="Search backgrounds (e.g. 'Beach')...">
<button class="btn-icon" onclick="searchPexels()">
<span class="material-symbols-rounded">search</span>
</button>
`;
toolsContent.appendChild(searchRow);
setTimeout(() => {
document.getElementById('pexelsSearch').addEventListener('keypress', (e) => {
if (e.key === 'Enter') searchPexels();
});
}, 0);
// Upload BG Card
const uploadCard = document.createElement('div');
uploadCard.className = 'card-opt upload-pl';
uploadCard.innerHTML = `<span class="material-symbols-rounded">add_photo_alternate</span> Upload`;
uploadCard.onclick = () => document.getElementById('bgUploadInput').click();
const hiddenInput = document.createElement('input');
hiddenInput.type = 'file'; hiddenInput.id = 'bgUploadInput'; hiddenInput.accept = 'image/*'; hiddenInput.hidden = true;
hiddenInput.onchange = (e) => {
const f = e.target.files[0];
if (f) {
const u = URL.createObjectURL(f);
setBg('image', u);
}
};
toolsContent.appendChild(hiddenInput);
const label = document.createElement('div');
label.className = 'section-label';
label.innerText = 'Suggestions';
toolsContent.appendChild(label);
const grid = document.createElement('div');
grid.className = 'grid-cards';
grid.id = 'photoGrid';
grid.appendChild(uploadCard);
state.curatedPhotos.forEach(src => {
const el = document.createElement('div');
el.className = 'card-opt';
el.style.backgroundImage = `url(${src})`;
el.onclick = () => setBg('image', src.replace('h=350', 'w=1920'));
if (state.bgValue === src) el.classList.add('selected');
grid.appendChild(el);
});
toolsContent.appendChild(grid);
}
else if (state.currentTab === 'color') {
// 1. SOLID COLOR SECTION
const cLabel = document.createElement('div');
cLabel.className = 'section-label';
cLabel.innerText = 'Solid Color';
toolsContent.appendChild(cLabel);
const pickerRow = document.createElement('div');
pickerRow.className = 'color-picker-row';
const thumb = document.createElement('div');
thumb.className = 'cp-thumb';
const inp = document.createElement('input');
inp.type = 'color';
inp.oninput = (e) => {
setBg('color', e.target.value);
thumb.style.background = e.target.value;
};
thumb.appendChild(inp);
const txt = document.createElement('span');
txt.innerText = 'Custom Color';
txt.style.fontWeight = '500';
pickerRow.appendChild(thumb);
pickerRow.appendChild(txt);
toolsContent.appendChild(pickerRow);
const cGrid = document.createElement('div');
cGrid.className = 'grid-cards';
const trans = document.createElement('div');
trans.className = 'card-opt';
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%)`;
trans.style.backgroundSize = '10px 10px';
trans.title = 'Transparent';
trans.onclick = () => setBg('none', '');
if (state.bgType === 'none') trans.classList.add('selected');
cGrid.appendChild(trans);
state.colors.forEach(c => {
const el = document.createElement('div');
el.className = 'card-opt';
el.style.backgroundColor = c;
el.onclick = () => setBg('color', c);
if (state.bgValue === c) el.classList.add('selected');
cGrid.appendChild(el);
});
toolsContent.appendChild(cGrid);
// 2. GRADIENT SECTION
const gLabel = document.createElement('div');
gLabel.className = 'section-label';
gLabel.innerText = 'Gradients';
gLabel.style.marginTop = '2rem';
toolsContent.appendChild(gLabel);
// Custom Gradient Builder
const gBuilder = document.createElement('div');
gBuilder.className = 'grad-builder';
const gControls = document.createElement('div');
gControls.className = 'grad-controls';
const g1 = document.createElement('div'); g1.className = 'cp-thumb';
const g1Inp = document.createElement('input'); g1Inp.type = 'color'; g1Inp.value = '#a1c4fd';
g1.appendChild(g1Inp);
const g2 = document.createElement('div'); g2.className = 'cp-thumb';
const g2Inp = document.createElement('input'); g2Inp.type = 'color'; g2Inp.value = '#c2e9fb';
g2.appendChild(g2Inp);
const deg = document.createElement('input');
deg.className = 'grad-angle-input';
deg.type = 'number'; deg.value = 90;
const updateGrad = () => {
const val = `linear-gradient(${deg.value}deg, ${g1Inp.value}, ${g2Inp.value})`;
setBg('color', val);
};
g1Inp.oninput = updateGrad;
g2Inp.oninput = updateGrad;
deg.oninput = updateGrad;
gControls.appendChild(g1);
gControls.appendChild(g2);
gControls.appendChild(document.createTextNode('Angle:'));
gControls.appendChild(deg);
gBuilder.appendChild(gControls);
toolsContent.appendChild(gBuilder);
const gGrid = document.createElement('div');
gGrid.className = 'grid-cards';
gGrid.style.marginTop = '12px';
state.gradients.forEach(g => {
const el = document.createElement('div');
el.className = 'card-opt';
el.style.backgroundImage = g;
el.onclick = () => setBg('color', g);
if (state.bgValue === g) el.classList.add('selected');
gGrid.appendChild(el);
});
toolsContent.appendChild(gGrid);
}
}
async function searchPexels() {
const q = document.getElementById('pexelsSearch').value;
if (!q) return;
if (!state.apiKey) {
alert('Pexels API Key missing.');
promptApiKey();
return;
}
const grid = document.getElementById('photoGrid');
const upBtn = grid.firstElementChild;
grid.innerHTML = '';
grid.appendChild(upBtn);
const loading = document.createElement('div'); loading.innerText = 'Searching...';
grid.appendChild(loading);
try {
const res = await fetch(`https://api.pexels.com/v1/search?query=${encodeURIComponent(q)}&per_page=15`, {
headers: { 'Authorization': state.apiKey }
});
const data = await res.json();
grid.removeChild(loading);
if (data.photos) {
data.photos.forEach(p => {
const el = document.createElement('div');
el.className = 'card-opt';
el.style.backgroundImage = `url(${p.src.tiny})`;
el.onclick = () => setBg('image', p.src.large2x || p.src.large);
grid.appendChild(el);
});
}
} catch (e) {
console.error(e);
alert('Pexels Search Failed.');
}
}
function setBg(type, val) {
state.bgType = type;
state.bgValue = val;
updatePreview();
renderTools();
}
function updatePreview() {
if (state.bgType === 'none') {
bgLayer.style.background = 'none';
} else if (state.bgType === 'color') {
bgLayer.style.background = state.bgValue;
} else if (state.bgType === 'image') {
bgLayer.style.background = 'none';
bgLayer.style.backgroundImage = `url(${state.bgValue})`;
}
}
function downloadResult() {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = subjectImg.naturalWidth;
canvas.height = subjectImg.naturalHeight;
// Draw Background
if (state.bgType === 'color') {
if (state.bgValue.startsWith('linear-gradient')) {
try {
let deg = 90;
const degMatch = state.bgValue.match(/(\d+)deg/);
if (degMatch) deg = parseInt(degMatch[1]);
const colorMatch = state.bgValue.match(/#[a-fA-F0-9]{6}|#[a-fA-F0-9]{3}|rgb\([^)]+\)/g);
if (colorMatch && colorMatch.length >= 2) {
const w = canvas.width, h = canvas.height;
const angleRad = (deg - 90) * (Math.PI / 180);
const x1 = w / 2 + Math.cos(angleRad + Math.PI) * w / 2;
const y1 = h / 2 + Math.sin(angleRad + Math.PI) * h / 2;
const x2 = w / 2 + Math.cos(angleRad) * w / 2;
const y2 = h / 2 + Math.sin(angleRad) * h / 2;
const grd = ctx.createLinearGradient(x1, y1, x2, y2);
grd.addColorStop(0, colorMatch[0]);
grd.addColorStop(1, colorMatch[1]);
ctx.fillStyle = grd;
ctx.fillRect(0, 0, w, h);
} else {
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, w, h);
}
} catch (e) {
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
} else {
ctx.fillStyle = state.bgValue;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
drawSubject(ctx, canvas);
} else if (state.bgType === 'image') {
const img = new Image();
img.crossOrigin = "Anonymous";
img.src = state.bgValue;
img.onload = () => {
const sRatio = img.width / img.height;
const dRatio = canvas.width / canvas.height;
let sw, sh, sx, sy;
if (sRatio > dRatio) { sh = img.height; sw = sh * dRatio; sx = (img.width - sw) / 2; sy = 0; }
else { sw = img.width; sh = sw / dRatio; sx = 0; sy = (img.height - sh) / 2; }
ctx.drawImage(img, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
drawSubject(ctx, canvas);
};
img.onerror = () => drawSubject(ctx, canvas);
} else {
drawSubject(ctx, canvas);
}
}
function drawSubject(ctx, canvas) {
ctx.drawImage(subjectImg, 0, 0);
const a = document.createElement('a');
a.download = 'rembg-edit.png';
a.href = canvas.toDataURL('image/png');
a.click();
}
function promptApiKey() {
const cur = state.apiKey;
const key = prompt("Enter Pexels API Key:", cur);
if (key !== null) {
localStorage.setItem('pexels_api_key', key.trim());
state.apiKey = key.trim();
}
}
function resetApp() {
uploadView.style.display = 'flex';
uploadView.style.opacity = 1;
editorView.style.display = 'none';
fileInput.value = '';
subjectImg.src = '';
state.bgType = 'none';
state.bgValue = '';
}
function showLoader(show) {
loader.style.display = show ? 'flex' : 'none';
}
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Remove Background - Photo Editor</title>
<!-- Google Fonts: Inter -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<!-- Google Material Symbols -->
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@24,400,0,0" />
<style>
:root {
--primary: #2563eb;
--primary-hover: #1d4ed8;
--bg-page: #f8fafc;
--surface: #ffffff;
--surface-hover: #f1f5f9;
--text-main: #0f172a;
--text-sec: #64748b;
--border: #e2e8f0;
--shadow-sm: 0 1px 2px -1px rgba(0, 0, 0, 0.08);
--shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
--radius-xl: 24px;
--radius-md: 12px;
--radius-pill: 9999px;
--h-header: 70px;
}
/* --- Base & Reset --- */
* {
box-sizing: border-box;
outline: none;
}
body {
font-family: 'Inter', sans-serif;
background-color: var(--bg-page);
margin: 0;
color: var(--text-main);
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
/* App-like feel */
}
/* --- Navbar --- */
nav {
height: var(--h-header);
background: var(--surface);
padding: 0 2rem;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid var(--border);
z-index: 50;
}
.brand {
display: flex;
align-items: center;
gap: 10px;
font-weight: 700;
font-size: 1.35rem;
color: #334155;
text-decoration: none;
letter-spacing: -0.5px;
}
.brand span {
color: var(--primary);
font-size: 32px;
}
.nav-actions {
display: flex;
gap: 12px;
align-items: center;
}
/* CPU Badge */
.cpu-badge {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 12px;
border-radius: var(--radius-pill);
background: var(--surface-hover);
font-size: 0.85rem;
font-weight: 600;
color: var(--text-sec);
border: 1px solid var(--border);
}
.cpu-dot {
width: 8px;
height: 8px;