Skip to content

Instantly share code, notes, and snippets.

@erraticgenerator
Last active December 9, 2020 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erraticgenerator/a504622c3b5f249bda36d374d6adeb13 to your computer and use it in GitHub Desktop.
Save erraticgenerator/a504622c3b5f249bda36d374d6adeb13 to your computer and use it in GitHub Desktop.
function setup() {
createCanvas(800, 400)
background(0)
const str = 'Hello, world! Hi, there! Nice to meet you!'
const wordsStr = str.split(' ')
textSize(48)
// track word position
let x = 20
let y = 60
fill(255)
// iterate over each word
for (let i = 0; i < wordsStr.length; i++) {
const wordStr = wordsStr[i] // get current word
const wordStrWidth = textWidth(wordStr) // get current word width
text(wordStr, x, y) // display word
x = x + wordStrWidth + textWidth(' ') // update x by word width + space character
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment