[whatwg] Canvas performance issue: setting colors

Greg Houston gregory.houston at gmail.com
Fri Oct 3 17:55:36 PDT 2008

On Fri, Oct 3, 2008 at 6:37 PM, Oliver Hunt <oliver at apple.com> wrote:
> <thinking out loud>
> Just had a thought (no idea how original) -- how about if fillStyle were
> able to accept a 3 or 4 number array? eg. fillStyle = [0, 0.3, 0.6, 1.0] ?
> That might work well if people are using arrays as vectors/colours
> </thinking out loud>

I have an application that allows the developer to set color options
for most of the canvas shapes in the application. The options are
stored as arrays:

	myColor:  [255, 0, 0]

When the colors are used then it takes a little JavaScript finessing
to apply them:

	ctx.fillStyle = 'rgb(' + myColor.join(',') + ')';

Being able to simply write the following, as would be the case with
your idea, would be nice.

	ctx.fillStyle = myColor;

Another example of using arrays for colors, both RGB and HSB can be
found in the Mootools JavaScript framework:

Note that also like your example the Mootools color plugin does not
have separate rgb and rgba methods. It is smart enough to recognize
that if there is a fourth value in the array it refers to the alpha. I
never quite grasped why there is both an rgb() and rgba() in canvas.
It seems like rgb is all that would have been necessary, and if a
fourth value is passed it obviously refers to the alpha.

- Greg

More information about the whatwg mailing list