mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Add support for single text-shadow
This commit is contained in:
parent
e27c41efd3
commit
17731169e9
@ -216,6 +216,28 @@ NodeContainer.prototype.parseBackgroundRepeat = function(index) {
|
|||||||
return this.cssList("backgroundRepeat", index)[0];
|
return this.cssList("backgroundRepeat", index)[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
NodeContainer.prototype.parseTextShadows = function() {
|
||||||
|
var textShadow = this.css("textShadow");
|
||||||
|
var results = [];
|
||||||
|
|
||||||
|
if (textShadow && textShadow !== 'none') {
|
||||||
|
var shadows = textShadow.match(this.TEXT_SHADOW_PROPERTY);
|
||||||
|
for (var i = 0; shadows && (i < shadows.length); i++) {
|
||||||
|
var s = shadows[i].match(this.TEXT_SHADOW_VALUES);
|
||||||
|
results.push({
|
||||||
|
color: s[0],
|
||||||
|
offsetX: s[1] ? s[1].replace('px', '') : 0,
|
||||||
|
offsetY: s[2] ? s[2].replace('px', '') : 0,
|
||||||
|
blur: s[3] ? s[3].replace('px', '') : 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
};
|
||||||
|
|
||||||
|
NodeContainer.prototype.TEXT_SHADOW_PROPERTY = /((rgba|rgb)\([^\)]+\)(\s-?\d+px){0,})/g;
|
||||||
|
NodeContainer.prototype.TEXT_SHADOW_VALUES = /(-?\d+px)|(#.+)|(rgb\(.+\))|(rgba\(.+\))/g;
|
||||||
|
|
||||||
function isPercentage(value) {
|
function isPercentage(value) {
|
||||||
return value.toString().indexOf("%") !== -1;
|
return value.toString().indexOf("%") !== -1;
|
||||||
}
|
}
|
||||||
|
@ -192,7 +192,15 @@ NodeParser.prototype.paintText = function(container) {
|
|||||||
var weight = container.parent.fontWeight();
|
var weight = container.parent.fontWeight();
|
||||||
var size = container.parent.css('fontSize');
|
var size = container.parent.css('fontSize');
|
||||||
var family = container.parent.css('fontFamily');
|
var family = container.parent.css('fontFamily');
|
||||||
|
var shadows = container.parent.parseTextShadows();
|
||||||
|
|
||||||
this.renderer.font(container.parent.css('color'), container.parent.css('fontStyle'), container.parent.css('fontVariant'), weight, size, family);
|
this.renderer.font(container.parent.css('color'), container.parent.css('fontStyle'), container.parent.css('fontVariant'), weight, size, family);
|
||||||
|
if (shadows.length) {
|
||||||
|
// TODO: support multiple text shadows
|
||||||
|
this.renderer.fontShadow(shadows[0].color, shadows[0].offsetX, shadows[0].offsetY, shadows[0].blur);
|
||||||
|
} else {
|
||||||
|
this.renderer.clearShadow();
|
||||||
|
}
|
||||||
|
|
||||||
textList.map(this.parseTextBounds(container), this).forEach(function(bounds, index) {
|
textList.map(this.parseTextBounds(container), this).forEach(function(bounds, index) {
|
||||||
if (bounds) {
|
if (bounds) {
|
||||||
|
@ -5,6 +5,7 @@ function CanvasRenderer(width, height) {
|
|||||||
this.canvas.height = height;
|
this.canvas.height = height;
|
||||||
this.ctx = this.canvas.getContext("2d");
|
this.ctx = this.canvas.getContext("2d");
|
||||||
this.ctx.textBaseline = "bottom";
|
this.ctx.textBaseline = "bottom";
|
||||||
|
this.variables = {};
|
||||||
log("Initialized CanvasRenderer");
|
log("Initialized CanvasRenderer");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,10 +49,29 @@ CanvasRenderer.prototype.font = function(color, style, variant, weight, size, fa
|
|||||||
this.setFillStyle(color).font = [style, variant, weight, size, family].join(" ");
|
this.setFillStyle(color).font = [style, variant, weight, size, family].join(" ");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
CanvasRenderer.prototype.fontShadow = function(color, offsetX, offsetY, blur) {
|
||||||
|
this.setVariable("shadowColor", color)
|
||||||
|
.setVariable("shadowOffsetY", offsetX)
|
||||||
|
.setVariable("shadowOffsetX", offsetY)
|
||||||
|
.setVariable("shadowBlur", blur);
|
||||||
|
};
|
||||||
|
|
||||||
|
CanvasRenderer.prototype.clearShadow = function() {
|
||||||
|
this.setVariable("shadowColor", "rgba(0,0,0,0)");
|
||||||
|
};
|
||||||
|
|
||||||
CanvasRenderer.prototype.setOpacity = function(opacity) {
|
CanvasRenderer.prototype.setOpacity = function(opacity) {
|
||||||
this.ctx.globalAlpha = opacity;
|
this.ctx.globalAlpha = opacity;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
CanvasRenderer.prototype.setVariable = function(property, value) {
|
||||||
|
if (this.variables[property] !== value) {
|
||||||
|
this.variables[property] = this.ctx[property] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
CanvasRenderer.prototype.text = function(text, left, bottom) {
|
CanvasRenderer.prototype.text = function(text, left, bottom) {
|
||||||
this.ctx.fillText(text, left, bottom);
|
this.ctx.fillText(text, left, bottom);
|
||||||
};
|
};
|
||||||
|
@ -11,7 +11,8 @@ var h2cSelector, h2cOptions;
|
|||||||
|
|
||||||
document.write(srcStart + '/tests/assets/jquery-1.6.2.js' + scrEnd);
|
document.write(srcStart + '/tests/assets/jquery-1.6.2.js' + scrEnd);
|
||||||
document.write(srcStart + '/tests/assets/jquery.plugin.html2canvas.js' + scrEnd);
|
document.write(srcStart + '/tests/assets/jquery.plugin.html2canvas.js' + scrEnd);
|
||||||
var html2canvas = ['log', 'nodecontainer', 'stackingcontext', 'textcontainer', 'support', 'imagecontainer', 'imageloader', 'nodeparser', 'core', 'renderer', 'promise', 'renderers/canvas'], i;
|
var html2canvas = ['log', 'nodecontainer', 'stackingcontext', 'textcontainer', 'support', 'imagecontainer',
|
||||||
|
'imageloader', 'nodeparser', 'font', 'fontmetrics', 'core', 'renderer', 'promise', 'renderers/canvas'], i;
|
||||||
for (i = 0; i < html2canvas.length; ++i) {
|
for (i = 0; i < html2canvas.length; ++i) {
|
||||||
document.write(srcStart + '/src/' + html2canvas[i] + '.js?' + Math.random() + scrEnd);
|
document.write(srcStart + '/src/' + html2canvas[i] + '.js?' + Math.random() + scrEnd);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user