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

Again the maxWidth parameter in fillText - text overflows (This time with right textAlign) #636

Closed
Tancred423 opened this issue Feb 12, 2023 · 0 comments · Fixed by #639
Closed
Assignees
Labels
bug Something isn't working

Comments

@Tancred423
Copy link

Hey hey,

a while ago we had this issue that the text shifts when using maxWidth. See #577.
It got fixed in #603 and works well so far.

But when I'm trying to use right alignment of text, the issue appears again:

simple

Code used for above image
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
const maxWidth = canvas.width - padding * 2

// The background
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = 'blue'
ctx.fillRect(padding, 0, maxWidth, canvas.height)

ctx.font = '16px Arial'
ctx.textAlign = 'right'
ctx.fillStyle = 'white'
ctx.textBaseline = 'top'

/** Short text */
ctx.fillText('Short text', canvas.width - padding, 10, maxWidth)

/** Very long text (10 repetitions) */
ctx.fillText(
`Very ${'long '.repeat(10)} text`,
canvas.width - padding,
30,
maxWidth,
)

/** Very long text (20 repetitions) */
ctx.fillText(
`Very ${'long '.repeat(20)} text`,
canvas.width - padding,
50,
maxWidth,
)

async function main() {
const pngData = await canvas.encode('png')
await promises.writeFile(join(__dirname, 'simple.png'), pngData)
}

main()
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.

2 participants