function removeBackground(image) { // Create a canvas const canvas = document.createElement('canvas'); canvas.width = image.width; canvas.height = image.height; // Create a context const ctx = canvas.getContext('2d'); // Draw the image to the canvas ctx.drawImage(image, 0, 0); // Get the image data const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); // Loop through each pixel for (let x = 0; x < imageData.data.length; x += 4) { // Get the red, green, and blue values const red = imageData.data[x]; const green = imageData.data[x + 1]; const blue = imageData.data[x + 2]; // If the pixel is not white, set it to transparent if (red != 255 || green != 255 || blue != 255) { imageData.data[x] = 0; imageData.data[x + 1] = 0; imageData.data[x + 2] = 0; } } // Update the canvas with the new image data ctx.putImageData(imageData, 0, 0); // Return the canvas return canvas; }