How to use Google fonts in Canvas when Drawing DOM objects in SVG?

后端 未结 2 1829
生来不讨喜
生来不讨喜 2020-12-02 00:43

As per Mozilla\'s documentation, you can draw complex HTML on Canvas like this.

What I can\'t figure out is a way to make Google fonts work with it.

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 01:24

    This has been already asked a few times, but never really as precise as it about Google Fonts.

    So the general ideas are that :

    • To draw an svg on a canvas, we need to load it in an element first.
    • For security reasons, inner documents can not make any external requests.
      This means that you'll have to embed all your external resources as dataURIs inside your svg markup itself, before loading it to the element.

    So for a font, you'll need to append a