I am trying to change the color for each line of my messages on canvas, but no success so far. I have tried creating variable for each one of them, but still no luck. any help would be appreciated.
function initiate(){ var elem = document.getElementById("canvas"); canvas = elem.getContext("2d"); addEventListener("mousemove", animation); canvas.shadowColor = "rgba(0, 0, 0, 0.5)"; canvas.shadowOffsetX = 4; canvas.shadowOffsetY = 4; canvas.shadowBlur = 5; canvas.font = "bold 24px verdana, sans-serif "; var welcomeMessage ="Welcome to the store!"; canvas.textAlign = "start"; canvas.textBaseline = "bottom"; canvas.fillText(welcomeMessage, 400, 50); canvas.font = "bold 14px verdana, sans-serif"; var message2 = "Your favorite store for videos games and latest DVDs!"; canvas.textAlign = "start"; canvas.textBaseline = "bottom"; canvas.fillText(message2, 400, 100); canvas.font = "bold 12px verdana, sans-serif"; canvas.textAlign = "start"; canvas.textBaseline = "bottom"; // Create gradient //var gradient=canvas.createLinearGradient(0,0,c.width,0); //gradient.addColorStop("0","magenta"); //gradient.addColorStop("0.5","blue"); //gradient.addColorStop("1.0","red"); //canvas.fillStyle = gradient; canvas.fillText(" <-- Move your mouse aroud to interact with Macroplay smily!", 400, 250); }
To change the color of text created by fillText, use the fillStyle property. Similarly, to change the color of text created by strokeText, use the strokeStyle property: var canvas = document. querySelector( "#myCanvas" );
In a sense, you can't really "change" the color of an element on the canvas because it has no scene graph, or, in other words, it has no history of what has been drawn on the canvas. To change the color of a line, you would have to redraw the line. ctx. moveTo(0, 0); ctx.
The fill() method fills the current drawing (path). The default color is black. Tip: Use the fillStyle property to fill with another color/gradient.
The fillText() method draws filled text on the canvas. The default color of the text is black. Tip: Use the font property to specify font and font size, and use the fillStyle property to render the text in another color/gradient.
you have to set the color for the text.
Something like this:
const canvasObj = document.getElementById('canvas') const context = canvasObj.getContext('2d') context.font = "bold 24px verdana, sans-serif "; var welcomeMessage = "Welcome to the store!"; context.textAlign = "start"; context.textBaseline = "bottom"; context.fillStyle = "#ff0000"; //<======= here context.fillText(welcomeMessage, 10, 50); context.font = "bold 14px verdana, sans-serif"; var message2 = "Your favorite store for videos games and latest DVDs!"; context.textAlign = "start"; context.textBaseline = "bottom"; context.fillStyle = "#00ff00"; //<======= and here context.fillText(message2, 10, 100);
<canvas id="canvas" width="400" height="100"></canvas>
Also, it could be a good idea to name your context variable "context" or "cxt" instead of "canvas". It would be less confusing :)
Have you tried simple fillStyles like:
canvas.fillStyle = "#ff00ff";
The text rendering on <canvas>
might not support advanced fill styles.
Just set new fillStyle before rendering each message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With