Re: Clarification on CanvasGradient Behaviour

Jatinder Mann wrote:
> I have another related issue. This same section of the spec is not
> clear about the expected behaviour when the addColorStops are added
> in the wrong order by offset. In the following example, lines 2 and 3
> are placed in the wrong order:
> 
> 0 var gradient = ctx.createLinearGradient(0,0,0,150);
> 1 gradient.addColorStop(0, 'darkblue');
> 2 gradient.addColorStop(.5, '#00ABEB');
> 3 gradient.addColorStop(.25, 'blue');
> 4 gradient.addColorStop(1, 'white');
>  
> 5  // assign gradient to fill style
> 6 ctx.fillStyle = gradient;
>  
> 7 // draw shape
> 8 ctx.fillRect(10,10,130,130);
>  
> I have noticed that Firefox, Safari, Chrome and Opera all draw the
> image as if the colour stops were added in the correct order. Should
> the spec clarify this behaviour?

I believe the spec is already precise about this. The stop simply exists 
at the given offset ("the gradient must have a new stop placed, at 
offset 'offset' relative to the whole gradient"), and then it says:

   "The color of the gradient at each stop is the color specified for 
that stop. Between each such stop, the colors and the alpha component 
must be linearly interpolated over the RGBA space without premultiplying 
the alpha value to find the color to use at that offset."

which defines the rendering in terms of the offsets of stops. So 
everything is determined by offsets, and there is no opportunity for the 
order of addition to have any effect.

(The exception is when you add multiple stops at the same offset, in 
which case the order becomes important, and the spec is explicit about 
that situation.)

-- 
Philip Taylor
pjt47@cam.ac.uk

Received on Thursday, 4 March 2010 19:46:29 UTC