Clear ctx.shadowColor after ctx.fill() to avoid application to both fill and stroke.

Closes #314.
This commit is contained in:
Jürg Lehni 2013-10-30 00:00:04 +01:00
parent db882748b1
commit eecf68b7c3
6 changed files with 64 additions and 32 deletions

View file

@ -420,6 +420,11 @@ var Item = Base.extend(Callback, /** @lends Item# */{
// DOCS: Item#hasStroke() // DOCS: Item#hasStroke()
hasStroke: function() { hasStroke: function() {
return this.getStyle().hasStroke(); return this.getStyle().hasStroke();
},
// DOCS: Item#hasShadow()
hasShadow: function() {
return this.getStyle().hasShadow();
} }
}, Base.each(['locked', 'visible', 'blendMode', 'opacity', 'guide'], }, Base.each(['locked', 'visible', 'blendMode', 'opacity', 'guide'],
// Produce getter/setters for properties. We need setters because we want to // Produce getter/setters for properties. We need setters because we want to
@ -3105,7 +3110,6 @@ var Item = Base.extend(Callback, /** @lends Item# */{
// items without children, where styles would be merged. // items without children, where styles would be merged.
var style = this._style, var style = this._style,
matrix = this._matrix, matrix = this._matrix,
strokeWidth = style.getStrokeWidth(),
fillColor = style.getFillColor(), fillColor = style.getFillColor(),
strokeColor = style.getStrokeColor(), strokeColor = style.getStrokeColor(),
shadowColor = style.getShadowColor(); shadowColor = style.getShadowColor();
@ -3113,36 +3117,44 @@ var Item = Base.extend(Callback, /** @lends Item# */{
// see #toCanvasStyle() // see #toCanvasStyle()
if (fillColor) if (fillColor)
ctx.fillStyle = fillColor.toCanvasStyle(ctx, matrix); ctx.fillStyle = fillColor.toCanvasStyle(ctx, matrix);
if (strokeColor && strokeWidth > 0) { if (strokeColor) {
ctx.strokeStyle = strokeColor.toCanvasStyle(ctx, matrix); var strokeWidth = style.getStrokeWidth();
ctx.lineWidth = strokeWidth; if (strokeWidth > 0) {
var strokeJoin = style.getStrokeJoin(), ctx.strokeStyle = strokeColor.toCanvasStyle(ctx, matrix);
strokeCap = style.getStrokeCap(), ctx.lineWidth = strokeWidth;
miterLimit = style.getMiterLimit(), var strokeJoin = style.getStrokeJoin(),
dashArray = style.getDashArray(), strokeCap = style.getStrokeCap(),
dashOffset = style.getDashOffset(); miterLimit = style.getMiterLimit();
if (strokeJoin) if (strokeJoin)
ctx.lineJoin = strokeJoin; ctx.lineJoin = strokeJoin;
if (strokeCap) if (strokeCap)
ctx.lineCap = strokeCap; ctx.lineCap = strokeCap;
if (miterLimit) if (miterLimit)
ctx.miterLimit = miterLimit; ctx.miterLimit = miterLimit;
if (paper.support.nativeDash && dashArray && dashArray.length) { if (paper.support.nativeDash) {
if ('setLineDash' in ctx) { var dashArray = style.getDashArray(),
ctx.setLineDash(dashArray); dashOffset = style.getDashOffset();
ctx.lineDashOffset = dashOffset; if (dashArray && dashArray.length) {
} else { if ('setLineDash' in ctx) {
ctx.mozDash = dashArray; ctx.setLineDash(dashArray);
ctx.mozDashOffset = dashOffset; ctx.lineDashOffset = dashOffset;
} else {
ctx.mozDash = dashArray;
ctx.mozDashOffset = dashOffset;
}
}
} }
} }
} }
if (shadowColor) { if (shadowColor) {
ctx.shadowColor = shadowColor.toCanvasStyle(ctx); var shadowBlur = style.getShadowBlur();
ctx.shadowBlur = style.getShadowBlur(); if (shadowBlur > 0) {
var offset = this.getShadowOffset(); ctx.shadowColor = shadowColor.toCanvasStyle(ctx);
ctx.shadowOffsetX = offset.x; ctx.shadowBlur = shadowBlur;
ctx.shadowOffsetY = offset.y; var offset = this.getShadowOffset();
ctx.shadowOffsetX = offset.x;
ctx.shadowOffsetY = offset.y;
}
} }
}, },

View file

@ -204,8 +204,10 @@ var Shape = Item.extend(/** @lends Shape# */{
} }
if (!clip && (hasFill || hasStroke)) { if (!clip && (hasFill || hasStroke)) {
this._setStyles(ctx); this._setStyles(ctx);
if (hasFill) if (hasFill) {
ctx.fill(style.getWindingRule()); ctx.fill(style.getWindingRule());
ctx.shadowColor = 'rgba(0,0,0,0)';
}
if (hasStroke) if (hasStroke)
ctx.stroke(); ctx.stroke();
} }

View file

@ -225,8 +225,10 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
if (!param.clip) { if (!param.clip) {
this._setStyles(ctx); this._setStyles(ctx);
var style = this._style; var style = this._style;
if (style.hasFill()) if (style.hasFill()) {
ctx.fill(style.getWindingRule()); ctx.fill(style.getWindingRule());
ctx.shadowColor = 'rgba(0,0,0,0)';
}
if (style.hasStroke()) if (style.hasStroke())
ctx.stroke(); ctx.stroke();
} }

View file

@ -2024,8 +2024,13 @@ var Path = PathItem.extend(/** @lends Path# */{
// If the path is part of a compound path or doesn't have a fill // If the path is part of a compound path or doesn't have a fill
// or stroke, there is no need to continue. // or stroke, there is no need to continue.
this._setStyles(ctx); this._setStyles(ctx);
if (hasFill) // If shadowColor is defined, clear it after fill, so it won't
// be applied to both fill and stroke. If the path is only
// stroked, we don't have to clear it.
if (hasFill) {
ctx.fill(style.getWindingRule()); ctx.fill(style.getWindingRule());
ctx.shadowColor = 'rgba(0,0,0,0)';
}
if (hasStroke) { if (hasStroke) {
if (dashLength) { if (dashLength) {
// We cannot use the path created by drawSegments above // We cannot use the path created by drawSegments above

View file

@ -252,6 +252,11 @@ var Style = Base.extend(new function() {
return !!this.getStrokeColor() && this.getStrokeWidth() > 0; return !!this.getStrokeColor() && this.getStrokeWidth() > 0;
}, },
// DOCS: Style#hasShadow()
hasShadow: function() {
return !!this.getShadowColor() && this.getShadowBlur() > 0;
},
// Overrides // Overrides
getLeading: function getLeading() { getLeading: function getLeading() {

View file

@ -84,13 +84,19 @@ var PointText = TextItem.extend(/** @lends PointText# */{
this._setStyles(ctx); this._setStyles(ctx);
var style = this._style, var style = this._style,
lines = this._lines, lines = this._lines,
leading = style.getLeading(); leading = style.getLeading(),
shadowColor = ctx.shadowColor;
ctx.font = style.getFontStyle(); ctx.font = style.getFontStyle();
ctx.textAlign = style.getJustification(); ctx.textAlign = style.getJustification();
for (var i = 0, l = lines.length; i < l; i++) { for (var i = 0, l = lines.length; i < l; i++) {
// See Path._draw() for explanation about ctx.shadowColor
ctx.shadowColor = shadowColor;
var line = lines[i]; var line = lines[i];
if (style.hasFill()) if (style.hasFill()) {
ctx.fillText(line, 0, 0); ctx.fillText(line, 0, 0);
ctx.shadowColor = 'rgba(0,0,0,0)';
}
if (style.hasStroke()) if (style.hasStroke())
ctx.strokeText(line, 0, 0); ctx.strokeText(line, 0, 0);
ctx.translate(0, leading); ctx.translate(0, leading);