Skip to content

Commit

Permalink
Implement consistent checks for fill / stroke / shadow styles in test…
Browse files Browse the repository at this point in the history
… functions on Style class.

And use them in Item#_setStyles()
  • Loading branch information
lehni committed Feb 12, 2016
1 parent e38829e commit c6bcf43
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
30 changes: 11 additions & 19 deletions src/item/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3918,16 +3918,13 @@ new function() { // // Scope to inject various item event handlers
_setStyles: function(ctx) {
// We can access internal properties since we're only using this on
// items without children, where styles would be merged.
var style = this._style,
fillColor = style.getFillColor(),
strokeColor = style.getStrokeColor(),
shadowColor = style.getShadowColor();
if (fillColor)
ctx.fillStyle = fillColor.toCanvasStyle(ctx);
if (strokeColor) {
var style = this._style;
if (style.hasFill())
ctx.fillStyle = style.getFillColor().toCanvasStyle(ctx);
if (style.hasStroke()) {
var strokeWidth = style.getStrokeWidth();
if (strokeWidth > 0) {
ctx.strokeStyle = strokeColor.toCanvasStyle(ctx);
ctx.strokeStyle = style.getStrokeColor().toCanvasStyle(ctx);
ctx.lineWidth = strokeWidth;
var strokeJoin = style.getStrokeJoin(),
strokeCap = style.getStrokeCap(),
Expand All @@ -3953,17 +3950,12 @@ new function() { // // Scope to inject various item event handlers
}
}
}
if (shadowColor) {
var blur = style.getShadowBlur(),
offset = this.getShadowOffset();
// In order to draw a shadow, we need either a shadow blur or an
// offset, or both.
if (blur > 0 || !offset.isZero()) {
ctx.shadowColor = shadowColor.toCanvasStyle(ctx);
ctx.shadowBlur = blur;
ctx.shadowOffsetX = offset.x;
ctx.shadowOffsetY = offset.y;
}
if (style.hasShadow()) {
var offset = this.getShadowOffset();
ctx.shadowColor = style.getShadowColor().toCanvasStyle(ctx);
ctx.shadowBlur = style.getShadowBlur();
ctx.shadowOffsetX = offset.x;
ctx.shadowOffsetY = offset.y;
}
},

Expand Down
12 changes: 9 additions & 3 deletions src/style/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,23 @@ var Style = Base.extend(new function() {

// DOCS: Style#hasFill()
hasFill: function() {
return !!this.getFillColor();
var color = this.getFillColor();
return !!color && color.alpha > 0;
},

// DOCS: Style#hasStroke()
hasStroke: function() {
return !!this.getStrokeColor() && this.getStrokeWidth() > 0;
var color = this.getStrokeColor();
return !!color && color.alpha > 0 && this.getStrokeWidth() > 0;
},

// DOCS: Style#hasShadow()
hasShadow: function() {
return !!this.getShadowColor();
var color = this.getShadowColor();
// In order to draw a shadow, we need either a shadow blur or an
// offset, or both.
return !!color && color.alpha > 0 && (this.getShadowBlur() > 0
|| !this.getShadowOffset().isZero());
},

/**
Expand Down

0 comments on commit c6bcf43

Please sign in to comment.