Created
December 25, 2023 14:15
-
-
Save blopa/7f160eb9e0638f97a5cdd6fa9b39c64d to your computer and use it in GitHub Desktop.
Code for post "How to automatically create thumbnails for your videos with Node.js"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const breakTextToFitCanvas = (ctx, text, maxWidth) => { | |
const words = text.split(' '); | |
const lines = []; | |
let currentLine = words[0]; | |
for (let i = 1; i < words.length; i++) { | |
const word = words[i]; | |
const { width } = ctx.measureText(`${currentLine} ${word}`); | |
if (width < maxWidth) { | |
currentLine += ` ${word}`; | |
} else { | |
lines.push(currentLine); | |
currentLine = word; | |
} | |
} | |
lines.push(currentLine); | |
return lines; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment