I am trying to create a texture using a canvas texture, but the texture appears black in Safari while it works fine on Windows. Below is the code I am currently using:
export function svgToTexture(svgString, targetHeight) {
return new Promise((resolve, reject) => {
const img = new Image();
const svgBlob = new Blob([svgString], { type: 'image/svg+xml' });
const url = URL.createObjectURL(svgBlob);
img.onload = () => {
let aspectRatio = img.width / img.height;
let targetWidth = targetHeight * aspectRatio;
const MAX_TEXTURE_SIZE = 1024;
if (
targetWidth > MAX_TEXTURE_SIZE ||
targetHeight > MAX_TEXTURE_SIZE
) {
const scale =
MAX_TEXTURE_SIZE / Math.max(targetWidth, targetHeight);
targetWidth *= scale;
targetHeight *= scale;
}
const canvas = document.createElement('canvas');
canvas.width = targetWidth;
canvas.height = targetHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, targetWidth, targetHeight);
const texture = new THREE.CanvasTexture(canvas);
resolve(texture);
URL.revokeObjectURL(url);
};
img.onerror = (err) => {
console.error('Failed to load SVG string as image', err);
URL.revokeObjectURL(url);
reject(err);
};
img.src = url;
});
}