@Kaiido I updated your example to work on safari
const worker = new Worker(generateURL(worker_script));
worker.onmessage = e => {
const img = e.data;
if(typeof img === 'string') {
console.error(img);
}
else
renderer.getContext('2d').drawImage(img, 0,0);
};
function generateURL(el) {
const blob = new Blob([el.textContent]);
return URL.createObjectURL(blob);
}
<script type="worker-script" id="worker_script">
if(self.FontFace) {
const url = 'https://fonts.gstatic.com/s/shadowsintolight/v7/UqyNK9UOIntux_czAvDQx_ZcHqZXBNQzdcD55TecYQ.woff2'
// first declare our font-face
// Fetch font to workaround safari bug not able to make cross-origin requests by the FontFace loader in a worker
fetch(url).then(res => res.arrayBuffer())
.then(raw => {
const fontFace = new FontFace(
'Shadows Into Light',
raw
);
// add it to the list of fonts our worker supports
self.fonts.add(fontFace);
// load the font
fontFace.load()
.then(()=> {
// font loaded
if(!self.OffscreenCanvas) {
postMessage("Your browser doesn't support OffscreeenCanvas yet");
return;
}
const canvas = new OffscreenCanvas(300, 150);
const ctx = canvas.getContext('2d');
if(!ctx) {
postMessage("Your browser doesn't support the 2d context yet...");
return;
}
ctx.font = '50px "Shadows Into Light"';
ctx.fillText('Hello world', 10, 50);
const img = canvas.transferToImageBitmap();
self.postMessage(img, [img]);
})
});
} else {
postMessage("Your browser doesn't support the FontFace API from WebWorkers yet");
}
</script>
<canvas id="renderer"></canvas>