We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
maxWidth: The maximum number of pixels wide the text may be once rendered.
maxWidth
const pad = 10; // padding ctx.textBaseline = 'top'; ctx.font = '16px Arial'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'blue'; ctx.fillRect(pad, pad, canvas.width - pad * 2, canvas.height - pad * 2); const maxWidth = canvas.width - pad * 2; ctx.fillStyle = 'white'; ctx.fillText('Short text', pad, 10, maxWidth); ctx.fillText(`Very ${'long '.repeat(8)} text`, pad, 30, maxWidth);
This is how it is supposed to be rendered (chrome latest version)
Here is how this package rendered it. (text overflows out of the blue rectangle)
Both rendered with the code above on a 200x60 canvas.
The text was updated successfully, but these errors were encountered:
Also, the longer the text, the worse it gets:
const { promises } = require('fs') const { join } = require('path') const { createCanvas } = require('@napi-rs/canvas') const canvas = createCanvas(500, 100) const ctx = canvas.getContext('2d') const padding = 50 ctx.textBaseline = 'top' ctx.font = '16px Arial' ctx.fillRect(0, 0, canvas.width, canvas.height) ctx.fillStyle = 'blue' ctx.fillRect(padding, 0, canvas.width - padding * 2, canvas.height) const maxWidth = canvas.width - padding * 2 ctx.fillStyle = 'white' ctx.fillText('Short text', padding, 10, maxWidth) ctx.fillText(`Very ${'long '.repeat(75)} text`, padding, 30, maxWidth) async function main() { const pngData = await canvas.encode('png') await promises.writeFile(join(__dirname, 'simple.png'), pngData) } main()
Sorry, something went wrong.
Brooooooklyn
Successfully merging a pull request may close this issue.
maxWidth
: The maximum number of pixels wide the text may be once rendered.This is how it is supposed to be rendered (chrome latest version)

Here is how this package rendered it. (text overflows out of the blue rectangle)

Both rendered with the code above on a 200x60 canvas.
The text was updated successfully, but these errors were encountered: