Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color for fillText() on HTML5 <canvas>

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);   } 
like image 233
user2193106 Avatar asked Apr 01 '13 19:04

user2193106


People also ask

How do I change the color of text in canvas HTML?

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" );

How do I change the color of my element in canvas?

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.

How do you fill a canvas with HTML color?

The fill() method fills the current drawing (path). The default color is black. Tip: Use the fillStyle property to fill with another color/gradient.

How do I fill text in canvas?

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.


2 Answers

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

like image 52
Gwennael Buchet Avatar answered Oct 14 '22 17:10

Gwennael Buchet


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.

like image 29
Mikko Ohtamaa Avatar answered Oct 14 '22 19:10

Mikko Ohtamaa