Canvas-Txt
Canvas-Txt copied to clipboard
Multiple Colors
Do you think there will be support for multiple colored words at any stage?
I was actually able to get it to work
//print all lines of text textarray.forEach(txtline => { txtline = txtline.trim() let wordArray = txtline.split(' ') let xPos=0 wordArray.forEach(element => { ctx.fillStyle=randomColor() let width = ctx.measureText(element).width ctx.fillText(element, textanchor+xPos, txtY) xPos += width+(this.fontSize/4) }); txtY += charHeight })
Actually the above breaks positioning of words when centered.
It now seems to be working: ` textarray.forEach(txtline => { txtline = txtline.trim() let wordArray = txtline.split(' ') let xPos=0 const lineWidth = ctx.measureText(txtline).width // iterate over each word and set a color (or other values) wordArray.forEach(element => { ctx.fillStyle=randomColor() ctx.textAlign="left" let wordWidth = ctx.measureText(element).width if(this.align === "left"){ ctx.fillText(element, textanchor+xPos, txtY) }else if(this.align === "center"){ ctx.fillText(element, (textanchor+xPos)-lineWidth/2, txtY) }else{ ctx.fillText(element, (textanchor-lineWidth)+xPos, txtY) } xPos += wordWidth+(this.fontSize/4) });
txtY += charHeight
})`
You can do this now using splitText
and rendering text on your own using v4. Check readme for docs