-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (81 loc) · 3.08 KB
/
Copy pathscript.js
File metadata and controls
100 lines (81 loc) · 3.08 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
const uploadArea = document.getElementById("uploadArea");
const imageInput = document.getElementById("imageInput");
const previewSection = document.getElementById("previewSection");
const originalImage = document.getElementById("originalImage");
const outputCanvas = document.getElementById("outputCanvas");
const downloadBtn = document.getElementById("downloadBtn");
const uploadNewBtn = document.getElementById("uploadNewBtn");
const loadingSpinner = document.getElementById("loadingSpinner");
const thresholdSlider = document.getElementById("thresholdSlider");
const thresholdValue = document.getElementById("thresholdValue");
const reprocessBtn = document.getElementById("reprocessBtn");
let currentThreshold = 50;
uploadArea.addEventListener("click", () => imageInput.click());
imageInput.addEventListener("change", handleImageUpload);
function handleImageUpload() {
const file = imageInput.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = e => {
originalImage.src = e.target.result;
originalImage.onload = () => {
showLoader();
setTimeout(() => {
removeBackground(currentThreshold);
hideLoader();
previewSection.classList.remove("hidden");
}, 300);
};
};
reader.readAsDataURL(file);
}
function removeBackground(threshold = 50) {
const ctx = outputCanvas.getContext("2d");
outputCanvas.width = originalImage.naturalWidth;
outputCanvas.height = originalImage.naturalHeight;
ctx.drawImage(originalImage, 0, 0);
const imageData = ctx.getImageData(0, 0, outputCanvas.width, outputCanvas.height);
const data = imageData.data;
let bgColor = { r: data[0], g: data[1], b: data[2] };
for (let i = 0; i < data.length; i += 4) {
const distance = Math.sqrt(
(data[i] - bgColor.r) ** 2 +
(data[i + 1] - bgColor.g) ** 2 +
(data[i + 2] - bgColor.b) ** 2
);
const normalized = (threshold / 100) * 255;
if (distance < normalized) {
data[i + 3] = 0;
}
}
ctx.putImageData(imageData, 0, 0);
}
thresholdSlider.addEventListener("input", e => {
currentThreshold = e.target.value;
thresholdValue.textContent = currentThreshold;
});
reprocessBtn.addEventListener("click", () => {
showLoader();
setTimeout(() => {
removeBackground(currentThreshold);
hideLoader();
}, 200);
});
downloadBtn.addEventListener("click", () => {
const link = document.createElement("a");
link.href = outputCanvas.toDataURL("image/png");
link.download = "background-removed.png";
link.click();
});
uploadNewBtn.addEventListener("click", () => {
previewSection.classList.add("hidden");
imageInput.value = "";
});
function showLoader() {
uploadArea.classList.add("hidden");
loadingSpinner.classList.remove("hidden");
}
function hideLoader() {
loadingSpinner.classList.add("hidden");
uploadArea.classList.remove("hidden");
}