tykel
tykel

Reputation: 95

Canvas fillStyle from number

I am trying to get my canvas to use a fill style from an array of ARGB integers.

Here is a minimally working example which can be lifted into JSFiddle:

var ctx = $('#cv')[0].getContext("2d");
var pal = [0x00000000, 0xff000000, 0xffff0000];

var col = '"#' + (pal[2]&0xffffff).toString(16) + '"';
alert(col);

ctx.fillStyle = col;
ctx.fillRect(0, 0, 200, 200);

This gives me a black rectangle, and the fillStyle remains "#000000", yet the correct color is alerted.

What have I missed?

Upvotes: 1

Views: 2244

Answers (2)

Wyatt Anderson
Wyatt Anderson

Reputation: 9893

You can also use numerical color values to specify a color for fillStyle. The rgb and rgba operators might be helpful with the added benefit of making your intentions more readable and explicit.

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382132

Don't add the quotes.

This is fine :

var col = '#' + (pal[2]&0xffffff).toString(16);

As you did it, your were building a string containing quotes.

Upvotes: 3

Related Questions