Chrome OffscreenCanvas custom fonts

前端 未结 1 689
有刺的猬
有刺的猬 2021-01-15 18:48

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 t

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-15 19:05

    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 = 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);
    }
    
    

    0 讨论(0)
提交回复
热议问题