Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting canvas width or height resets font #835

Open
ibash opened this issue May 27, 2024 · 1 comment
Open

Setting canvas width or height resets font #835

ibash opened this issue May 27, 2024 · 1 comment

Comments

@ibash
Copy link

ibash commented May 27, 2024

Steps:

  1. Register a custom font
  2. Set the font on the canvas / context
  3. Set the canvas width or height
  4. fillText

Expected:
Font is the same as whatever you set it to

Actual:
Font is reset to the standard system font

I ran into this when using ctx.measureText and setting the canvas to the text size.

Feel free to close if this is working as expected.

const fontPath = path.join(__dirname, './fonts/MyFont.ttf')
const isRegistered = GlobalFonts.registerFromPath(fontPath, 'MyFont')

if (!isRegistered) {
  throw new Error('Failed to register font')
}

const canvas = createCanvas(300, 150)
const ctx = canvas.getContext('2d')


ctx.font = '25px Caveat'
canvas.width = 250

ctx.fillStyle = 'black'
ctx.fillText('some text', 0, 50)

const url = canvas.toDataURL('image/png')
console.log(url)
@zoli456
Copy link

zoli456 commented Jun 13, 2024

I can confirm it too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants