Skip to content Skip to sidebar Skip to footer

Chrome Offscreencanvas Custom Fonts

I am using the new OffscreenCanvas released in Chrome 69 and cannot seem to render custom loaded fonts that render to ordinary canvases just fine. Is there a trick to getting a cus

Solution 1:

You should be able to use the FontFace and FontFaceSet interfaces of the CSS Font Loading API from within a Worker.

The main difference is that instead of accessing through document.fonts, the FontFaceSet is stored as self.fonts directly.

const worker = newWorker(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);
};

functiongenerateURL(el) {
  const blob = newBlob([el.textContent]);
  returnURL.createObjectURL(blob);
}
<scripttype="worker-script"id="worker_script">if(self.FontFace) {
    // first declare our font-faceconst fontFace = newFontFace(
      'Shadows Into Light',
      "local('Shadows Into Light'), local('ShadowsIntoLight'), url(https://fonts.gstatic.com/s/shadowsintolight/v7/UqyNK9UOIntux_czAvDQx_ZcHqZXBNQzdcD55TecYQ.woff2) format('woff2')"
    );
    // add it to the list of fonts our worker supports
    self.fonts.add(fontFace);
    // load the font
    fontFace.load()
    .then(()=> {
      // font loadedif(!self.OffscreenCanvas) {
        postMessage("Your browser doesn't support OffscreeenCanvas yet");
        return;
      }
      const canvas = newOffscreenCanvas(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><canvasid="renderer"></canvas>

Post a Comment for "Chrome Offscreencanvas Custom Fonts"