Skip to content

Commit 7b00a41

Browse files
committed
fix: cut off text when vanvas is too narrow instead of squishing text
1 parent d8fcf7f commit 7b00a41

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

playgrounds/app/src/components/Editor.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,11 @@ async function createAnimationFrame(
11961196
canvas.width = width + xPadding * 2
11971197
canvas.height = height + yPadding * 2
11981198

1199+
const textCanvas = document.createElement('canvas')
1200+
const textCtx = textCanvas.getContext('2d', { alpha: true })!
1201+
textCanvas.width = width + xPadding - 4
1202+
textCanvas.height = height + yPadding
1203+
11991204
if (backgroundType === 'linearGradient') {
12001205
// Convert angle to match CSS gradient angle (0deg = to top, 90deg = to right)
12011206
const cssAngle = (backgroundGradientDirection + 90) % 360
@@ -1266,13 +1271,15 @@ async function createAnimationFrame(
12661271
[el.color.start || 'rgba(0,0,0,0)', el.color.end || 'rgba(0,0,0,0)'],
12671272
)
12681273

1269-
ctx.font = `${fontSize} ${fontFamily}`
1270-
ctx.fillStyle = color
1271-
ctx.globalAlpha = opacity
1272-
ctx.fillText(htmlDecode(el.el.innerHTML), x, y, width - x + xPadding / 2)
1274+
textCtx.font = `${fontSize} ${fontFamily}`
1275+
textCtx.fillStyle = color
1276+
textCtx.globalAlpha = opacity
1277+
textCtx.fillText(htmlDecode(el.el.innerHTML), x, y)
12731278
})
12741279
await Promise.all(elementPromises)
12751280

1281+
ctx.drawImage(textCanvas, 0, 0)
1282+
12761283
return ctx.getImageData(0, 0, canvas.width, canvas.height)
12771284
}
12781285

0 commit comments

Comments
 (0)