Adds background support for gradient.

Warns on case when we don't handle radial gradients correctly.
Fixes chrome extend gradients.
This commit is contained in:
Brendan Dahl 2012-09-10 13:23:07 -07:00
parent 5d8f463162
commit 164931d2fc

View File

@ -48,8 +48,9 @@ var Shadings = {};
// A small number to offset the first/last color stops so we can insert ones to
// support extend. Number.MIN_VALUE appears to be too small and breaks the
// extend.
Shadings.SMALL_NUMBER = 1e-7;
// extend. 1e-7 works in FF but chrome seems to use an even smaller sized number
// internally so we have to go bigger.
Shadings.SMALL_NUMBER = 1e-2;
// Radial and axial shading have very similar implementations
// If needed, the implementations can be broken into two classes
@ -78,6 +79,23 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
extendEnd = extendArr[1];
}
if (this.shadingType === PatternType.RADIAL &&
(!extendStart || !extendEnd)) {
// Radial gradient only currently works if either circle is fully within
// the other circle.
var x1 = this.coordsArr[0];
var y1 = this.coordsArr[1];
var r1 = this.coordsArr[2];
var x2 = this.coordsArr[3];
var y2 = this.coordsArr[4];
var r2 = this.coordsArr[5];
var distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (r1 <= r2 + distance &&
r2 <= r1 + distance) {
warn('Unsupported radial gradient.');
}
}
this.extendStart = extendStart;
this.extendEnd = extendEnd;
@ -110,17 +128,22 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
colorStops.push([(i - t0) / diff, cssColor]);
}
// XXX: Extend start and end does work in Chrome. Tested in v21.
var background = 'transparent';
if (dict.has('Background')) {
var rgbColor = cs.getRgb(dict.get('Background'));
background = Util.makeCssRgb(rgbColor[0], rgbColor[1], rgbColor[2]);
}
if (!extendStart) {
// Insert a color stop at the front and offset the first real color stop
// so it doesn't conflict with the one we insert.
colorStops.unshift([0, 'rgba(255,255,255,0)']);
colorStops.unshift([0, background]);
colorStops[1][0] += Shadings.SMALL_NUMBER;
}
if (!extendEnd) {
// Same idea as above in extendStart but for the end.
colorStops[colorStops.length - 1][0] -= Shadings.SMALL_NUMBER;
colorStops.push([1, 'rgba(255,255,255,0)']);
colorStops.push([1, background]);
}
this.colorStops = colorStops;