How to use multiple colors in one text #110
-
Beta Was this translation helpful? Give feedback.
Answered by
samizdatco
Aug 7, 2022
Replies: 1 comment 1 reply
-
It's not possible in a single step. You need to use a combination of Something like this will work: let prefix = "correct horse ",
highlighted = "battery",
suffix = " staple"
let x = 100,
y = 100
ctx.font = '20px Helvetica'
ctx.fillText(prefix, x, y)
x += ctx.measureText(prefix).width
ctx.fillStyle = 'red'
ctx.fillText(highlighted, x, y)
x += ctx.measureText(highlighted).width
ctx.fillStyle = 'black'
ctx.fillText(suffix, x, y) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Farman52
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's not possible in a single step. You need to use a combination of
fillText
andmeasureText
to lay out the line yourself and change thefillStyle
in between.Something like this will work: