paper.js/src/item/Layer.js

81 lines
1.7 KiB
JavaScript
Raw Normal View History

2011-02-11 12:37:36 -05:00
Layer = Item.extend({
beans: true,
2011-02-11 12:37:36 -05:00
initialize: function() {
this.children = [];
this.document = Paper.document;
this.document.layers.push(this);
this.activate();
},
getIndex: function() {
return !this.parent ? this.document.layers.indexOf(this) : this.base();
},
/**
* Removes the layer from its document's layers list
* or its parent's children list.
*/
removeFromParent: function() {
if(!this.parent) {
this.document.layers.splice(this.index, 1);
} else {
this.base();
}
},
moveAbove: function(item) {
// if the item is a layer and contained within Document#layers
if(item instanceof Layer && !item.parent) {
this.removeFromParent();
item.document.layers.splice(item.index + 1, 0, this);
this.document = item.document;
return true;
} else {
this.base(item);
}
},
moveBelow: function(item) {
// if the item is a layer and contained within Document#layers
if(item instanceof Layer && !item.parent) {
this.removeFromParent();
item.document.layers.splice(item.index - 1, 0, this);
this.document = item.document;
return true;
} else {
this.base(item);
}
},
getNextSibling: function() {
if(!this.parent) {
var index = this.index + 1;
if (index < this.document.layers.length)
return this.document.layers[index];
} else {
return this.base();
}
},
getPreviousSibling: function() {
if(!this.parent) {
var index = this.index - 1;
if (index <= 0)
return this.document.layers[index];
} else {
return this.base();
}
},
activate: function() {
this.document.activeLayer = this;
2011-02-11 12:37:36 -05:00
},
2011-02-11 12:49:04 -05:00
draw: function(ctx) {
for (var i = 0, l = this.children.length; i < l; i++) {
2011-02-11 12:49:04 -05:00
this.children[i].draw(ctx);
2011-02-11 12:37:36 -05:00
}
}
});