this resolves the crash but not the weird flicker
This commit is contained in:
parent
d9fae875f4
commit
953db2f968
1 changed files with 77 additions and 41 deletions
|
@ -13,16 +13,47 @@ const colorToGrayscale = (color) => {
|
|||
};
|
||||
|
||||
/**
|
||||
* Extract color using canvas2d
|
||||
* @param {HTMLImageElement} image Image source
|
||||
* @returns Colors represeting the image
|
||||
* Extract colors from an image or SVG element using canvas
|
||||
* @param {HTMLImageElement | SVGSVGElement} element Image or SVG source element
|
||||
* @returns {Promise<Array>} Colors representing the element
|
||||
*/
|
||||
const extractColors = (image) => {
|
||||
const extractColors = (element) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Create a canvas with the same dimensions as the element
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = image.naturalWidth || image.width;
|
||||
canvas.height = image.naturalHeight || image.height;
|
||||
let imageSource;
|
||||
|
||||
if (element instanceof SVGSVGElement) {
|
||||
// SVG element handling
|
||||
canvas.width = element.viewBox.baseVal.width;
|
||||
canvas.height = element.viewBox.baseVal.height;
|
||||
|
||||
const svgString = new XMLSerializer().serializeToString(element);
|
||||
const svgBlob = new Blob([svgString], { type: "image/svg+xml" });
|
||||
const svgUrl = URL.createObjectURL(svgBlob);
|
||||
|
||||
imageSource = new Image();
|
||||
imageSource.src = svgUrl;
|
||||
|
||||
imageSource.onload = () => {
|
||||
processImage(imageSource);
|
||||
URL.revokeObjectURL(svgUrl);
|
||||
};
|
||||
|
||||
imageSource.onerror = (error) => {
|
||||
URL.revokeObjectURL(svgUrl);
|
||||
reject(error);
|
||||
};
|
||||
} else {
|
||||
// Regular image handling
|
||||
canvas.width = element.naturalWidth || element.width;
|
||||
canvas.height = element.naturalHeight || element.height;
|
||||
processImage(element);
|
||||
}
|
||||
|
||||
const processImage = (img) => {
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx.drawImage(image, 0, 0);
|
||||
ctx.drawImage(img, 0, 0);
|
||||
|
||||
const imageData = ctx.getImageData(
|
||||
0,
|
||||
|
@ -43,7 +74,9 @@ const extractColors = (image) => {
|
|||
}
|
||||
}
|
||||
|
||||
return colors;
|
||||
resolve(colors);
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -51,28 +84,31 @@ const extractColors = (image) => {
|
|||
* @param {Mode} mode
|
||||
*/
|
||||
const changeTheme = (mode) => {
|
||||
for (const item of document.getElementsByTagName("svg")) {
|
||||
const image = document.createElement("img");
|
||||
image.src = `data:image/svg+xml;base64,${btoa(
|
||||
new XMLSerializer().serializeToString(item)
|
||||
)}`;
|
||||
|
||||
image.width = item.viewBox.baseVal.width;
|
||||
image.height = item.viewBox.baseVal.height;
|
||||
|
||||
image.onload = () => {
|
||||
applyFilter(mode, item, extractColors(image));
|
||||
};
|
||||
// Process SVG elements
|
||||
for (const svgElement of document.getElementsByTagName("svg")) {
|
||||
extractColors(svgElement)
|
||||
.then((colors) => {
|
||||
applyFilter(mode, svgElement, colors);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error extracting colors:", error);
|
||||
});
|
||||
}
|
||||
|
||||
// SVG embedded in images as base64
|
||||
// Process SVG images embedded as base64
|
||||
for (const item of document.getElementsByTagName("img")) {
|
||||
if (!item.src.startsWith("data:image/svg+xml;base64,")) {
|
||||
// Exclude image who aren't SVG and base64 encoded
|
||||
// Exclude images that aren't SVG and base64 encoded
|
||||
continue;
|
||||
}
|
||||
|
||||
applyFilter(mode, item, extractColors(item));
|
||||
extractColors(item)
|
||||
.then((colors) => {
|
||||
applyFilter(mode, item, colors);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error extracting colors:", error);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue