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

Closes .
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,21 +3117,24 @@ 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) {
var strokeWidth = style.getStrokeWidth();
if (strokeWidth > 0) {
ctx.strokeStyle = strokeColor.toCanvasStyle(ctx, matrix); ctx.strokeStyle = strokeColor.toCanvasStyle(ctx, matrix);
ctx.lineWidth = strokeWidth; ctx.lineWidth = strokeWidth;
var strokeJoin = style.getStrokeJoin(), var strokeJoin = style.getStrokeJoin(),
strokeCap = style.getStrokeCap(), strokeCap = style.getStrokeCap(),
miterLimit = style.getMiterLimit(), miterLimit = style.getMiterLimit();
dashArray = style.getDashArray(),
dashOffset = style.getDashOffset();
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) {
var dashArray = style.getDashArray(),
dashOffset = style.getDashOffset();
if (dashArray && dashArray.length) {
if ('setLineDash' in ctx) { if ('setLineDash' in ctx) {
ctx.setLineDash(dashArray); ctx.setLineDash(dashArray);
ctx.lineDashOffset = dashOffset; ctx.lineDashOffset = dashOffset;
@ -3137,13 +3144,18 @@ var Item = Base.extend(Callback, /** @lends Item# */{
} }
} }
} }
}
}
if (shadowColor) { if (shadowColor) {
var shadowBlur = style.getShadowBlur();
if (shadowBlur > 0) {
ctx.shadowColor = shadowColor.toCanvasStyle(ctx); ctx.shadowColor = shadowColor.toCanvasStyle(ctx);
ctx.shadowBlur = style.getShadowBlur(); ctx.shadowBlur = shadowBlur;
var offset = this.getShadowOffset(); var offset = this.getShadowOffset();
ctx.shadowOffsetX = offset.x; ctx.shadowOffsetX = offset.x;
ctx.shadowOffsetY = offset.y; ctx.shadowOffsetY = offset.y;
} }
}
}, },
// TODO: Implement View into the drawing. // TODO: Implement View into the drawing.

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);