Use #hasFill() and #hasStroke() more broadly and avoid drawing strokes when strokeWidth is set to 0.

This commit is contained in:
Jürg Lehni 2013-10-29 20:05:39 +01:00
parent 94623f6d3f
commit c43752dc3e
6 changed files with 45 additions and 35 deletions

View file

@ -413,12 +413,11 @@ var Item = Base.extend(Callback, /** @lends Item# */{
}, },
hasFill: function() { hasFill: function() {
return !!this.getStyle().getFillColor(); return this.getStyle().hasFill();
}, },
hasStroke: function() { hasStroke: function() {
var style = this.getStyle(); return this.getStyle().hasStroke();
return !!style.getStrokeColor() && style.getStrokeWidth() > 0;
} }
}, 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
@ -3104,29 +3103,28 @@ 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,
width = style.getStrokeWidth(), strokeWidth = style.getStrokeWidth(),
join = style.getStrokeJoin(),
cap = style.getStrokeCap(),
limit = style.getMiterLimit(),
fillColor = style.getFillColor(), fillColor = style.getFillColor(),
strokeColor = style.getStrokeColor(), strokeColor = style.getStrokeColor(),
shadowColor = style.getShadowColor(); shadowColor = style.getShadowColor();
if (width != null)
ctx.lineWidth = width;
if (join)
ctx.lineJoin = join;
if (cap)
ctx.lineCap = cap;
if (limit)
ctx.miterLimit = limit;
// We need to take matrix into account for gradients, // We need to take matrix into account for gradients,
// see #toCanvasStyle() // see #toCanvasStyle()
if (fillColor) if (fillColor)
ctx.fillStyle = fillColor.toCanvasStyle(ctx, matrix); ctx.fillStyle = fillColor.toCanvasStyle(ctx, matrix);
if (strokeColor) { if (strokeColor && strokeWidth > 0) {
ctx.strokeStyle = strokeColor.toCanvasStyle(ctx, matrix); ctx.strokeStyle = strokeColor.toCanvasStyle(ctx, matrix);
var dashArray = style.getDashArray(), ctx.lineWidth = strokeWidth;
var strokeJoin = style.getStrokeJoin(),
strokeCap = style.getStrokeCap(),
miterLimit = style.getMiterLimit(),
dashArray = style.getDashArray(),
dashOffset = style.getDashOffset(); dashOffset = style.getDashOffset();
if (strokeJoin)
ctx.lineJoin = strokeJoin;
if (strokeCap)
ctx.lineCap = strokeCap;
if (miterLimit)
ctx.miterLimit = miterLimit;
if (paper.support.nativeDash && dashArray && dashArray.length) { if (paper.support.nativeDash && dashArray && dashArray.length) {
if ('setLineDash' in ctx) { if ('setLineDash' in ctx) {
ctx.setLineDash(dashArray); ctx.setLineDash(dashArray);

View file

@ -152,10 +152,10 @@ var Shape = Item.extend(/** @lends Shape# */{
_draw: function(ctx, param) { _draw: function(ctx, param) {
var style = this._style, var style = this._style,
fillColor = style.getFillColor(), hasFill = style.hasFill(),
strokeColor = style.getStrokeColor(), hasStroke = style.hasStroke(),
clip = param.clip; clip = param.clip;
if (fillColor || strokeColor || clip) { if (hasFill || hasStroke || clip) {
var radius = this._radius, var radius = this._radius,
shape = this._shape; shape = this._shape;
ctx.beginPath(); ctx.beginPath();
@ -202,11 +202,11 @@ var Shape = Item.extend(/** @lends Shape# */{
} }
ctx.closePath(); ctx.closePath();
} }
if (!clip && (fillColor || strokeColor)) { if (!clip && (hasFill || hasStroke)) {
this._setStyles(ctx); this._setStyles(ctx);
if (fillColor) if (hasFill)
ctx.fill(style.getWindingRule()); ctx.fill(style.getWindingRule());
if (strokeColor) if (hasStroke)
ctx.stroke(); ctx.stroke();
} }
}, },

View file

@ -225,9 +225,9 @@ 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.getFillColor()) if (style.hasFill())
ctx.fill(style.getWindingRule()); ctx.fill(style.getWindingRule());
if (style.getStrokeColor()) if (style.hasStroke())
ctx.stroke(); ctx.stroke();
} }
} }

View file

@ -1999,11 +1999,11 @@ var Path = PathItem.extend(/** @lends Path# */{
ctx.beginPath(); ctx.beginPath();
var style = this.getStyle(), var style = this.getStyle(),
fillColor = style.getFillColor(), hasFill = style.hasFill(),
strokeColor = style.getStrokeColor(), hasStroke = style.hasStroke(),
dashArray = style.getDashArray(), dashArray = style.getDashArray(),
// dashLength is only set if we can't draw dashes natively // dashLength is only set if we can't draw dashes natively
dashLength = !paper.support.nativeDash && strokeColor dashLength = !paper.support.nativeDash && hasStroke
&& dashArray && dashArray.length; && dashArray && dashArray.length;
function getOffset(i) { function getOffset(i) {
@ -2014,19 +2014,19 @@ var Path = PathItem.extend(/** @lends Path# */{
// Prepare the canvas path if we have any situation that requires it // Prepare the canvas path if we have any situation that requires it
// to be defined. // to be defined.
if (fillColor || strokeColor && !dashLength || compound || clip) if (hasFill || hasStroke && !dashLength || compound || clip)
drawSegments(ctx, this); drawSegments(ctx, this);
if (this._closed) if (this._closed)
ctx.closePath(); ctx.closePath();
if (!clip && !compound && (fillColor || strokeColor)) { if (!clip && !compound && (hasFill || hasStroke)) {
// 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 (fillColor) if (hasFill)
ctx.fill(style.getWindingRule()); ctx.fill(style.getWindingRule());
if (strokeColor) { if (hasStroke) {
if (dashLength) { if (dashLength) {
// We cannot use the path created by drawSegments above // We cannot use the path created by drawSegments above
// Use CurveFlatteners to draw dashed paths: // Use CurveFlatteners to draw dashed paths:
@ -2517,7 +2517,7 @@ statics: {
} }
// TODO: Find a way to reuse 'bounds' cache instead? // TODO: Find a way to reuse 'bounds' cache instead?
if (!style.getStrokeColor() || !style.getStrokeWidth()) if (!style.hasStroke())
return Path.getBounds(segments, closed, style, matrix); return Path.getBounds(segments, closed, style, matrix);
var length = segments.length - (closed ? 0 : 1), var length = segments.length - (closed ? 0 : 1),
radius = style.getStrokeWidth() / 2, radius = style.getStrokeWidth() / 2,

View file

@ -242,6 +242,18 @@ var Style = Base.extend(new function() {
|| false; || false;
}, },
// DOCS: Style#hasFill()
hasFill: function() {
return !!this.getFillColor();
},
// DOCS: Style#hasStroke()
hasStroke: function() {
return !!this.getStrokeColor() && this.getStrokeWidth() > 0;
},
// Overrides
getLeading: function getLeading() { getLeading: function getLeading() {
// Override leading to return fontSize * 1.2 by default. // Override leading to return fontSize * 1.2 by default.
var leading = getLeading.base.call(this); var leading = getLeading.base.call(this);

View file

@ -89,9 +89,9 @@ var PointText = TextItem.extend(/** @lends PointText# */{
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++) {
var line = lines[i]; var line = lines[i];
if (style.getFillColor()) if (style.hasFill())
ctx.fillText(line, 0, 0); ctx.fillText(line, 0, 0);
if (style.getStrokeColor()) if (style.hasStroke())
ctx.strokeText(line, 0, 0); ctx.strokeText(line, 0, 0);
ctx.translate(0, leading); ctx.translate(0, leading);
} }