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

The maxWidth parameter in fillText - text overflows #577

Closed
Spacey4K opened this issue Oct 31, 2022 · 1 comment · Fixed by #603
Closed

The maxWidth parameter in fillText - text overflows #577

Spacey4K opened this issue Oct 31, 2022 · 1 comment · Fixed by #603
Assignees
Labels
bug Something isn't working

Comments

@Spacey4K
Copy link

maxWidth: The maximum number of pixels wide the text may be once rendered.

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)
Browser:Expected

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

Both rendered with the code above on a 200x60 canvas.

@Brooooooklyn Brooooooklyn self-assigned this Nov 2, 2022
@Brooooooklyn Brooooooklyn added the bug Something isn't working label Nov 2, 2022
@Tancred423
Copy link

Also, the longer the text, the worse it gets:

simple

Code used (almost the same as Spacey's, but more repetitions)
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()

@Spacey4K Spacey4K changed the title The maxWidth parameter in fillText - unexpected behaviour The maxWidth parameter in fillText - text overflows Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants