mirror of
https://github.com/konvajs/konva.git
synced 2025-10-15 12:34:52 +08:00
flattened shadow object into shadowColor, shadowBlur, shadowOpacity, and shadowOffset attrs
This commit is contained in:
@@ -145,7 +145,7 @@
|
||||
*/
|
||||
fillStroke: function(shape) {
|
||||
this._fill(shape);
|
||||
this._stroke(shape, shape.getShadow() && shape.getFill());
|
||||
this._stroke(shape, shape.hasShadow() && shape.getFill());
|
||||
},
|
||||
/**
|
||||
* apply shadow
|
||||
@@ -215,11 +215,11 @@
|
||||
|
||||
Kinetic.SceneCanvas.prototype = {
|
||||
_fill: function(shape, skipShadow) {
|
||||
var context = this.context, fill = shape.getFill(), fillType = shape._getFillType(fill), shadow = shape.getShadow();
|
||||
var context = this.context, fill = shape.getFill(), fillType = shape._getFillType(fill);
|
||||
if(fill) {
|
||||
context.save();
|
||||
|
||||
if(!skipShadow && shadow) {
|
||||
if(!skipShadow && shape.hasShadow()) {
|
||||
this._applyShadow(shape);
|
||||
}
|
||||
|
||||
@@ -281,13 +281,13 @@
|
||||
|
||||
context.restore();
|
||||
|
||||
if(!skipShadow && shadow && shadow.opacity) {
|
||||
if(!skipShadow && shape.hasShadow()) {
|
||||
this._fill(shape, true);
|
||||
}
|
||||
}
|
||||
},
|
||||
_stroke: function(shape, skipShadow) {
|
||||
var context = this.context, stroke = shape.getStroke(), strokeWidth = shape.getStrokeWidth(), shadow = shape.getShadow(), dashArray = shape.getDashArray();
|
||||
var context = this.context, stroke = shape.getStroke(), strokeWidth = shape.getStrokeWidth(), dashArray = shape.getDashArray();
|
||||
if(stroke || strokeWidth) {
|
||||
context.save();
|
||||
this._applyLineCap(shape);
|
||||
@@ -299,7 +299,7 @@
|
||||
Kinetic.Global.warn('Could not apply dash array because your browser does not support it.');
|
||||
}
|
||||
}
|
||||
if(!skipShadow && shadow) {
|
||||
if(!skipShadow && shape.hasShadow()) {
|
||||
this._applyShadow(shape);
|
||||
}
|
||||
context.lineWidth = strokeWidth || 2;
|
||||
@@ -307,25 +307,25 @@
|
||||
context.stroke(context);
|
||||
context.restore();
|
||||
|
||||
if(!skipShadow && shadow && shadow.opacity) {
|
||||
if(!skipShadow && shape.hasShadow()) {
|
||||
this._stroke(shape, true);
|
||||
}
|
||||
}
|
||||
},
|
||||
_applyShadow: function(shape) {
|
||||
var context = this.context, shadow = shape.getShadow();
|
||||
if(shadow) {
|
||||
var context = this.context;
|
||||
if(shape.hasShadow()) {
|
||||
var aa = shape.getAbsoluteOpacity();
|
||||
// defaults
|
||||
var color = shadow.color || 'black';
|
||||
var blur = shadow.blur || 5;
|
||||
var offset = shadow.offset || {
|
||||
var color = shape.getShadowColor() || 'black';
|
||||
var blur = shape.getShadowBlur() || 5;
|
||||
var offset = shape.getShadowOffset() || {
|
||||
x: 0,
|
||||
y: 0
|
||||
};
|
||||
|
||||
if(shadow.opacity) {
|
||||
context.globalAlpha = shadow.opacity * aa;
|
||||
if(shape.getShadowOpacity()) {
|
||||
context.globalAlpha = shape.getShadowOpacity() * aa;
|
||||
}
|
||||
context.shadowColor = color;
|
||||
context.shadowBlur = blur;
|
||||
|
91
src/Shape.js
91
src/Shape.js
@@ -25,13 +25,12 @@
|
||||
* @param {Number} [config.strokeWidth] stroke width
|
||||
* @param {String} [config.lineJoin] line join can be miter, round, or bevel. The default
|
||||
* is miter
|
||||
* @param {Object} [config.shadow] shadow object
|
||||
* @param {String} [config.shadow.color]
|
||||
* @param {Number} [config.shadow.blur]
|
||||
* @param {Obect} [config.shadow.blur.offset]
|
||||
* @param {Number} [config.shadow.blur.offset.x]
|
||||
* @param {Number} [config.shadow.blur.offset.y]
|
||||
* @param {Number} [config.shadow.opacity] shadow opacity. Can be any real number
|
||||
* @param {String} [config.shadowColor]
|
||||
* @param {Number} [config.shadowBlur]
|
||||
* @param {Obect} [config.shadowOffset]
|
||||
* @param {Number} [config.shadowOffset.x]
|
||||
* @param {Number} [config.shadowOffset.y]
|
||||
* @param {Number} [config.shadowOpacity] shadow opacity. Can be any real number
|
||||
* between 0 and 1
|
||||
* @param {Array} [config.dashArray]
|
||||
* @param {Number} [config.width]
|
||||
@@ -78,6 +77,14 @@
|
||||
getCanvas: function() {
|
||||
return this.getLayer().getCanvas();
|
||||
},
|
||||
/**
|
||||
* returns whether or not a shadow will be rendered
|
||||
* @name hasShadow
|
||||
* @methodOf Kinetic.Shape.prototype
|
||||
*/
|
||||
hasShadow: function() {
|
||||
return !!(this.getShadowColor() || this.getShadowBlur() || this.getShadowOffset());
|
||||
},
|
||||
_getFillType: function(fill) {
|
||||
var type = Kinetic.Type;
|
||||
if(!fill) {
|
||||
@@ -100,21 +107,20 @@
|
||||
}
|
||||
},
|
||||
/**
|
||||
* set shadow object
|
||||
* @name setShadow
|
||||
* set shadow offset
|
||||
* @name setShadowOffset
|
||||
* @methodOf Kinetic.Shape.prototype
|
||||
* @param {Object} config
|
||||
* @param {String} config.color
|
||||
* @param {Number} config.blur
|
||||
* @param {Array|Object|Number} config.offset
|
||||
* @param {Number} config.opacity
|
||||
* @param {Number|Array|Object} offset
|
||||
*/
|
||||
setShadow: function(config) {
|
||||
var type = Kinetic.Type;
|
||||
if(config.offset !== undefined) {
|
||||
config.offset = type._getXY(config.offset);
|
||||
setShadowOffset: function() {
|
||||
var pos = Kinetic.Type._getXY([].slice.call(arguments));
|
||||
if(pos.x === undefined) {
|
||||
pos.x = this.getShadowOffset().x;
|
||||
}
|
||||
this.setAttr('shadow', type._merge(config, this.getShadow()));
|
||||
if(pos.y === undefined) {
|
||||
pos.y = this.getShadowOffset().y;
|
||||
}
|
||||
this.setAttr('shadowOffset', pos);
|
||||
},
|
||||
/**
|
||||
* set fill which can be a color, linear gradient object,
|
||||
@@ -239,8 +245,8 @@
|
||||
Kinetic.Global.extend(Kinetic.Shape, Kinetic.Node);
|
||||
|
||||
// add getters and setters
|
||||
Kinetic.Node.addGettersSetters(Kinetic.Shape, ['stroke', 'lineJoin', 'lineCap', 'strokeWidth', 'drawFunc', 'drawHitFunc', 'dashArray']);
|
||||
Kinetic.Node.addGetters(Kinetic.Shape, ['shadow', 'fill']);
|
||||
Kinetic.Node.addGettersSetters(Kinetic.Shape, ['stroke', 'lineJoin', 'lineCap', 'strokeWidth', 'drawFunc', 'drawHitFunc', 'dashArray', 'shadowColor', 'shadowBlur', 'shadowOpacity']);
|
||||
Kinetic.Node.addGetters(Kinetic.Shape, ['shadowOffset', 'fill']);
|
||||
|
||||
/**
|
||||
* set stroke color
|
||||
@@ -298,6 +304,27 @@
|
||||
* apart
|
||||
*/
|
||||
|
||||
/**
|
||||
* set shadow color
|
||||
* @name setShadowColor
|
||||
* @methodOf Kinetic.Shape.prototype
|
||||
* @param {String} color
|
||||
*/
|
||||
|
||||
/**
|
||||
* set shadow blur
|
||||
* @name setShadowBlur
|
||||
* @methodOf Kinetic.Shape.prototype
|
||||
* @param {Number} blur
|
||||
*/
|
||||
|
||||
/**
|
||||
* set shadow opacity
|
||||
* @name setShadowOpacity
|
||||
* @methodOf Kinetic.Shape.prototype
|
||||
* @param {Number} opacity must be a value between 0 and 1
|
||||
*/
|
||||
|
||||
/**
|
||||
* get stroke color
|
||||
* @name getStroke
|
||||
@@ -329,8 +356,26 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* get shadow object
|
||||
* @name getShadow
|
||||
* get shadow color
|
||||
* @name getShadowColor
|
||||
* @methodOf Kinetic.Shape.prototype
|
||||
*/
|
||||
|
||||
/**
|
||||
* get shadow blur
|
||||
* @name getShadowBlur
|
||||
* @methodOf Kinetic.Shape.prototype
|
||||
*/
|
||||
|
||||
/**
|
||||
* get shadow opacity
|
||||
* @name getShadowOpacity
|
||||
* @methodOf Kinetic.Shape.prototype
|
||||
*/
|
||||
|
||||
/**
|
||||
* get shadow offset
|
||||
* @name getShadowOffset
|
||||
* @methodOf Kinetic.Shape.prototype
|
||||
*/
|
||||
|
||||
|
@@ -48,7 +48,7 @@
|
||||
params = [this.attrs.image, 0, 0, width, height];
|
||||
}
|
||||
|
||||
if(this.getShadow()) {
|
||||
if(this.hasShadow()) {
|
||||
canvas.applyShadow(this, function() {
|
||||
that._drawImage(context, params);
|
||||
});
|
||||
|
Reference in New Issue
Block a user