-
-
Save BeiXiao/299e5b763cce7368faaf5c1f6302915a to your computer and use it in GitHub Desktop.
Satori で Google Fonts を使う
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import satori from 'satori' | |
| import fs from 'fs' | |
| // fetch font information | |
| const endpoint = new URL('https://www.googleapis.com/webfonts/v1/webfonts'); | |
| endpoint.searchParams.set('family', 'M PLUS Rounded 1c'); | |
| endpoint.searchParams.set('key', process.env.GOOGLE_FONTS_API_KEY); | |
| const fontInfo = await fetch(endpoint).then(res => res.json()); | |
| // fetch font | |
| const fontResponse = await fetch(fontInfo.items[0].files['regular']); | |
| const fontBuffer = await fontResponse.arrayBuffer(); | |
| const svg = await satori( | |
| <div style={{ | |
| alignItems: 'center', | |
| justifyContent: 'center', | |
| background: 'lightgray', | |
| width: '100%', | |
| height: '100%', | |
| fontSize: 32, | |
| }}> | |
| Hello, Google Fonts! | |
| </div>, | |
| { | |
| width: 600, | |
| height: 400, | |
| fonts: [ | |
| { | |
| name: 'M PLUS Rounded 1c', | |
| data: fontBuffer, | |
| }, | |
| ], | |
| }, | |
| ) | |
| fs.writeFileSync('output.svg', svg); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment