Add ChangeFlag.CLIPPING and use it for proper clip mask caching in Group.

This commit is contained in:
Jürg Lehni 2011-06-19 22:36:04 +01:00
parent 0df909dd71
commit 2b63aefc09
3 changed files with 34 additions and 15 deletions

View file

@ -27,10 +27,12 @@ var ChangeFlag = {
// Fill style or stroke color / dash
STYLE: 16,
// Item attributes: visible, blendMode, locked, name, opacity, clipMask ...
ATTRIBUTE: 32
ATTRIBUTE: 32,
// Clipping in one of the child items
CLIPPING: 64
};
// Shortcuts to the ChangeFlag to send to #_changed(), all including appearance
// Shortcuts to often used ChangeFlag values including APPEARANCE
var Change = {
HIERARCHY: ChangeFlag.HIERARCHY | ChangeFlag.APPEARANCE,
GEOMETRY: ChangeFlag.GEOMETRY | ChangeFlag.APPEARANCE,

View file

@ -74,13 +74,24 @@ var Group = this.Group = Item.extend({
|| typeof items[0] !== 'object' ? arguments : items);
},
_getClipMask: function() {
// TODO: Use caching once Change.HIERARCHY is implemented
_changed: function(flags) {
if (flags & (ChangeFlag.HIERARCHY | ChangeFlag.CLIPPING)) {
// Clear cached clip item whenever hierarchy changes
delete this._clipItem;
}
},
_getClipItem: function() {
// Allow us to set _clipItem to null when none is found and still return
// it as a defined value without searching again
if (this._clipItem !== undefined)
return this._clipItem;
for (var i = 0, l = this._children.length; i < l; i++) {
var child = this._children[i];
if (child._clipMask)
return child;
return this._clipItem = child;
}
return this._clipItem = null;
},
/**
@ -92,7 +103,7 @@ var Group = this.Group = Item.extend({
* @bean
*/
isClipped: function() {
return !!this._getClipMask();
return !!this._getClipItem();
},
setClipped: function(clipped) {
@ -103,12 +114,12 @@ var Group = this.Group = Item.extend({
},
draw: function(ctx, param) {
var clipMask = this._getClipMask();
if (clipMask)
Item.draw(clipMask, ctx, param);
var clipItem = this._getClipItem();
if (clipItem)
Item.draw(clipItem, ctx, param);
for (var i = 0, l = this._children.length; i < l; i++) {
var item = this._children[i];
if (item != clipMask)
if (item != clipItem)
Item.draw(item, ctx, param);
}
}

View file

@ -362,12 +362,18 @@ var Item = this.Item = Base.extend({
},
setClipMask: function(clipMask) {
this._clipMask = clipMask;
if (clipMask) {
this.setFillColor(null);
this.setStrokeColor(null);
// On-the-fly conversion to boolean:
if (this._clipMask != (clipMask = !!clipMask)) {
this._clipMask = clipMask;
if (clipMask) {
this.setFillColor(null);
this.setStrokeColor(null);
}
this._changed(Change.ATTRIBUTE);
// Tell the parent the clipping mask has changed
if (this._parent)
this._parent._changed(ChangeFlag.CLIPPING);
}
this._changed(Change.ATTRIBUTE);
},
_clipMask: false,