-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
159 lines (139 loc) · 5.53 KB
/
script.js
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
const outputImage = document.getElementById("outputImage");
const downloadBtn = document.getElementById("downloadBtn");
const bgButton = document.getElementById("bgButton");
const imageInput = document.getElementById("imageInput");
const uploadBtn = document.getElementById("uploadBtn");
const imageDetails = document.getElementById("imageDetails");
const loadingSpinner = document.getElementById("loadingSpinner");
const previewImage = document.getElementById("previewImage");
let removedBgImageUrl = ""; // To store the image with removed background
let newBackgroundUrl = ""; // To store the new background image URL
// Function to simulate background removal using remove.bg API
function simulateBackgroundRemoval(file) {
loadingSpinner.style.display = "inline-block"; // Show loading spinner
const formData = new FormData();
formData.append("image_file", file);
fetch("https://api.remove.bg/v1.0/removebg", {
method: "POST",
headers: {
"X-Api-Key": "vKjpZwEgetFqYDPJXzp9q6Qt",
},
body: formData,
})
.then((response) => {
if (!response.ok) {
throw new Error("Failed to remove background: " + response.statusText);
}
return response.blob();
})
.then((blob) => {
const url = URL.createObjectURL(blob);
removedBgImageUrl = url; // Store the URL of the processed image
outputImage.src = url;
outputImage.style.display = "block";
downloadBtn.style.display = "inline-block";
bgButton.style.display = "inline-block";
loadingSpinner.style.display = "none"; // Hide loading spinner
})
.catch((error) => {
console.error("Error:", error);
loadingSpinner.style.display = "none"; // Hide loading spinner in case of error
});
}
// Handle the file upload for background removal
imageInput.addEventListener("change", function (event) {
const file = event.target.files[0];
if (file) {
// Display image preview
const reader = new FileReader();
reader.onload = function (e) {
previewImage.src = e.target.result;
previewImage.style.display = "inline-block";
};
reader.readAsDataURL(file);
// Display image details
imageDetails.innerHTML = `
<strong>File Name:</strong> ${file.name}<br>
<strong>File Size:</strong> ${(file.size / 1024).toFixed(2)} KB<br>
`;
simulateBackgroundRemoval(file);
}
});
// Handle the upload button click
uploadBtn.addEventListener("click", function () {
imageInput.click();
});
// Handle the new background upload
bgButton.addEventListener("click", function () {
const newBackgroundInput = document.createElement("input");
newBackgroundInput.type = "file";
newBackgroundInput.accept = "image/*";
newBackgroundInput.click();
newBackgroundInput.addEventListener("change", function (event) {
const newBackgroundFile = event.target.files[0];
if (newBackgroundFile) {
const reader = new FileReader();
reader.onload = function (e) {
newBackgroundUrl = e.target.result; // Store the URL of the new background
outputImage.style.backgroundImage = `url(${newBackgroundUrl})`;
outputImage.style.backgroundSize = "cover"; // Ensure the background image covers the canvas
outputImage.style.backgroundPosition = "center"; // Center the background image
outputImage.style.backgroundRepeat = "no-repeat"; // Don't repeat the background image
outputImage.style.overflow = "hidden"; // Crop the overflow
};
reader.readAsDataURL(newBackgroundFile);
}
});
});
downloadBtn.addEventListener("click", function () {
if (removedBgImageUrl && newBackgroundUrl) {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const bgImage = new Image();
const fgImage = new Image();
// Load the background and foreground images
bgImage.src = newBackgroundUrl;
fgImage.src = removedBgImageUrl;
bgImage.onload = function () {
fgImage.onload = function () {
// Set the canvas dimensions to match the foreground image (uploaded image)
const canvasWidth = fgImage.width;
const canvasHeight = fgImage.height;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
// Draw the background image to cover the canvas (like background-size: cover;)
// Cropping extra parts if overflow occurs
const bgWidth = bgImage.width;
const bgHeight = bgImage.height;
const xOffset = (bgWidth - canvasWidth) / 2; // center horizontally
const yOffset = (bgHeight - canvasHeight) / 2; // center vertically
// Draw the background image
ctx.drawImage(
bgImage,
xOffset,
yOffset,
canvasWidth,
canvasHeight,
0,
0,
canvasWidth,
canvasHeight
);
// Draw the foreground image on top of the background
ctx.drawImage(fgImage, 0, 0, canvasWidth, canvasHeight);
// Generate the final image and download
const mergedImage = canvas.toDataURL("image/png");
const link = document.createElement("a");
link.href = mergedImage;
link.download = "image-with-new-bg.png";
link.click();
};
};
} else {
const a = document.createElement("a");
a.href = removedBgImageUrl;
a.download = "image-with-new-bg.png";
a.click();
// alert("Please upload both the image and background!");
}
});