[whatwg] Canvas performance issue: setting colors

Sjoerd Visscher sjoerd at w3future.com
Mon Sep 29 15:24:22 PDT 2008


With the increasing speed of javascript engines, some other  
performance issues are starting to become apparent. Take this demo for  


A large percentage of the time goes into building color strings for  
setting the strokeStyle, which the browser then has to take apart  
again. This is a very unnecessary performance hit. (Btw, on my Mac  
this runs faster under Google Chrome in VMware than natively in the  
latest Webkit nightly.)

I think the canvas api should get 2 new methods:

   CanvasColor createRGBAColor(in float r, in float g, in float b, in  
float a)
   CanvasColor createHSLAColor(in float h, in float s, in float l, in  
float a)

CanvasColor objects could be opaque, or maybe they have a toString  
method that returns the CSS serialization. The strokeStyle, fillStyle  
and shadowColor attributes should accept these objects, and probably  
the addColorStop method as well. Using CSS color strings should still  
work obviously. On getting these attributes the serialization must  
still be returned like it is now.

Sjoerd Visscher
sjoerd at w3future.com

More information about the whatwg mailing list