Radial gradient support

This commit is contained in:
Niklas von Hertzen 2017-12-12 20:57:48 +08:00
parent 8ef3861a5c
commit cacb9f64e4
3 changed files with 49 additions and 28 deletions

View File

@ -1,6 +1,7 @@
### Changelog ### ### Changelog ###
#### v1.0.0-alpha4 - TBD #### #### v1.0.0-alpha4 - TBD ####
* Add support for radial-gradients
* Fix logging option (#1302) * Fix logging option (#1302)
* Add support for rendering webgl canvas content (#646) * Add support for rendering webgl canvas content (#646)
* Fix external SVG loading with proxies (#802) * Fix external SVG loading with proxies (#802)

View File

@ -11,6 +11,7 @@ Below is a list of all the supported CSS properties and values.
- background-image - background-image
- url() - url()
- linear-gradient() - linear-gradient()
- radial-gradient()
- background-origin - background-origin
- background-position - background-position
- background-size - background-size
@ -68,7 +69,6 @@ These CSS properties are **NOT** currently supported
- [filter](https://github.com/niklasvh/html2canvas/issues/493) - [filter](https://github.com/niklasvh/html2canvas/issues/493)
- [font-variant-ligatures](https://github.com/niklasvh/html2canvas/pull/1085) - [font-variant-ligatures](https://github.com/niklasvh/html2canvas/pull/1085)
- [list-style](https://github.com/niklasvh/html2canvas/issues/177) - [list-style](https://github.com/niklasvh/html2canvas/issues/177)
- radial-gradient()
- [repeating-linear-gradient()](https://github.com/niklasvh/html2canvas/issues/1162) - [repeating-linear-gradient()](https://github.com/niklasvh/html2canvas/issues/1162)
- word-break - word-break
- [writing-mode](https://github.com/niklasvh/html2canvas/issues/1258) - [writing-mode](https://github.com/niklasvh/html2canvas/issues/1258)

View File

@ -32,19 +32,10 @@ export type ColorStop = {
stop: number stop: number
}; };
export type LinearGradient = { export interface Gradient {
type: GradientType, type: GradientType,
direction: Direction,
colorStops: Array<ColorStop> colorStops: Array<ColorStop>
}; }
export type RadialGradient = {
type: GradientType,
shape: RadialGradientShapeType,
center: Point,
radius: Point,
colorStops: Array<ColorStop>
};
export const GRADIENT_TYPE = { export const GRADIENT_TYPE = {
LINEAR_GRADIENT: 0, LINEAR_GRADIENT: 0,
@ -68,11 +59,44 @@ const LENGTH_FOR_POSITION = {
bottom: new Length('100%') bottom: new Length('100%')
}; };
export class LinearGradient implements Gradient {
type: GradientType;
colorStops: Array<ColorStop>;
direction: Direction;
constructor(colorStops: Array<ColorStop>, direction: Direction) {
this.type = GRADIENT_TYPE.LINEAR_GRADIENT;
this.colorStops = colorStops;
this.direction = direction;
}
}
export class RadialGradient implements Gradient {
type: GradientType;
colorStops: Array<ColorStop>;
shape: RadialGradientShapeType;
center: Point;
radius: Point;
constructor(
colorStops: Array<ColorStop>,
shape: RadialGradientShapeType,
center: Point,
radius: Point
) {
this.type = GRADIENT_TYPE.RADIAL_GRADIENT;
this.colorStops = colorStops;
this.shape = shape;
this.center = center;
this.radius = radius;
}
}
export const parseGradient = ( export const parseGradient = (
container: NodeContainer, container: NodeContainer,
{args, method, prefix}: BackgroundSource, {args, method, prefix}: BackgroundSource,
bounds: Bounds bounds: Bounds
): ?LinearGradient | RadialGradient => { ): ?Gradient => {
if (method === 'linear-gradient') { if (method === 'linear-gradient') {
return parseLinearGradient(args, bounds, !!prefix); return parseLinearGradient(args, bounds, !!prefix);
} else if (method === 'gradient' && args[0] === 'linear') { } else if (method === 'gradient' && args[0] === 'linear') {
@ -83,10 +107,11 @@ export const parseGradient = (
!!prefix !!prefix
); );
} else if (method === 'radial-gradient') { } else if (method === 'radial-gradient') {
if (prefix === '-webkit-') { return parseRadialGradient(
args = transformWebkitRadialGradientArgs(args); container,
} prefix === '-webkit-' ? transformWebkitRadialGradientArgs(args) : args,
return parseRadialGradient(container, args, bounds); bounds
);
} else if (method === 'gradient' && args[0] === 'radial') { } else if (method === 'gradient' && args[0] === 'radial') {
return parseRadialGradient( return parseRadialGradient(
container, container,
@ -176,11 +201,7 @@ const parseLinearGradient = (
bounds.height * 2 bounds.height * 2
); );
return { return new LinearGradient(parseColorStops(args, firstColorStopIndex, lineLength), direction);
type: GRADIENT_TYPE.LINEAR_GRADIENT,
direction,
colorStops: parseColorStops(args, firstColorStopIndex, lineLength)
};
}; };
const parseRadialGradient = ( const parseRadialGradient = (
@ -238,13 +259,12 @@ const parseRadialGradient = (
bounds bounds
); );
return { return new RadialGradient(
type: GRADIENT_TYPE.RADIAL_GRADIENT, parseColorStops(args, m ? 1 : 0, Math.min(gradientRadius.x, gradientRadius.y)),
shape, shape,
center: gradientCenter, gradientCenter,
radius: gradientRadius, gradientRadius
colorStops: parseColorStops(args, m ? 1 : 0, Math.min(gradientRadius.x, gradientRadius.y)) );
};
}; };
const calculateGradientDirection = (radian: number, bounds: Bounds): Direction => { const calculateGradientDirection = (radian: number, bounds: Bounds): Direction => {