Canvas-Txt icon indicating copy to clipboard operation
Canvas-Txt copied to clipboard

Multiple Colors

Open Nineswiss opened this issue 3 years ago • 3 comments

Do you think there will be support for multiple colored words at any stage?

Nineswiss avatar Jan 28 '22 04:01 Nineswiss

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 })

Nineswiss avatar Jan 28 '22 05:01 Nineswiss

Actually the above breaks positioning of words when centered.

Nineswiss avatar Jan 28 '22 05:01 Nineswiss

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

})`

Nineswiss avatar Jan 28 '22 06:01 Nineswiss

You can do this now using splitText and rendering text on your own using v4. Check readme for docs

geongeorge avatar Jul 14 '23 14:07 geongeorge