mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
84 lines
2.6 KiB
JavaScript
84 lines
2.6 KiB
JavaScript
var GradientContainer = require('./gradientcontainer');
|
|
var Color = require('./color');
|
|
|
|
var COLOR_STOP_REGEXP = /^(.*)\s*(\d{1,3})?(%|px)?$/;
|
|
|
|
function LinearGradientContainer(imageData) {
|
|
GradientContainer.apply(this, arguments);
|
|
this.type = this.TYPES.LINEAR;
|
|
|
|
var hasDirection = imageData.args[0].match(this.stepRegExp) === null;
|
|
|
|
if (hasDirection) {
|
|
imageData.args[0].split(" ").reverse().forEach(function(position) {
|
|
switch(position) {
|
|
case "left":
|
|
this.x0 = 0;
|
|
this.x1 = 1;
|
|
break;
|
|
case "top":
|
|
this.y0 = 0;
|
|
this.y1 = 1;
|
|
break;
|
|
case "right":
|
|
this.x0 = 1;
|
|
this.x1 = 0;
|
|
break;
|
|
case "bottom":
|
|
this.y0 = 1;
|
|
this.y1 = 0;
|
|
break;
|
|
case "to":
|
|
var y0 = this.y0;
|
|
var x0 = this.x0;
|
|
this.y0 = this.y1;
|
|
this.x0 = this.x1;
|
|
this.x1 = x0;
|
|
this.y1 = y0;
|
|
break;
|
|
}
|
|
}, this);
|
|
} else {
|
|
this.y0 = 0;
|
|
this.y1 = 1;
|
|
}
|
|
|
|
|
|
this.colorStops = imageData.args.slice(hasDirection ? 1 : 0)
|
|
.map(function(colorStop) { return colorStop.match(COLOR_STOP_REGEXP);})
|
|
.filter(function(colorStopMatch) { return !!colorStopMatch;})
|
|
.map(function(colorStopMatch) {
|
|
return {
|
|
color: new Color(colorStopMatch[1]),
|
|
stop: colorStopMatch[3] === "%" ? colorStopMatch[2] / 100 : null
|
|
};
|
|
});
|
|
|
|
if (this.colorStops[0].stop === null) {
|
|
this.colorStops[0].stop = 0;
|
|
}
|
|
|
|
if (this.colorStops[this.colorStops.length - 1].stop === null) {
|
|
this.colorStops[this.colorStops.length - 1].stop = 1;
|
|
}
|
|
|
|
this.colorStops.forEach(function(colorStop, index) {
|
|
if (colorStop.stop === null) {
|
|
this.colorStops.slice(index).some(function(find, count) {
|
|
if (find.stop !== null) {
|
|
colorStop.stop = ((find.stop - this.colorStops[index - 1].stop) / (count + 1)) + this.colorStops[index - 1].stop;
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}, this);
|
|
}
|
|
}, this);
|
|
}
|
|
|
|
LinearGradientContainer.prototype = Object.create(GradientContainer.prototype);
|
|
|
|
LinearGradientContainer.prototype.stepRegExp = /((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\))\s*(\d{1,3})?(%|px)?/;
|
|
|
|
module.exports = LinearGradientContainer;
|