paper.js/src/item/Layer.js

137 lines
3.7 KiB
JavaScript
Raw Normal View History

2011-03-06 19:50:44 -05:00
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
2011-03-07 20:41:50 -05:00
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
2011-03-06 19:50:44 -05:00
* http://lehni.org/ & http://jonathanpuckey.com/
*
2011-07-01 06:17:45 -04:00
* Distributed under the MIT license. See LICENSE file for details.
*
2011-03-07 20:41:50 -05:00
* All rights reserved.
2011-03-06 19:50:44 -05:00
*/
/**
* @name Layer
*
* @class The Layer item represents a layer in a Paper.js project.
*
* The layer which is currently active can be accessed through
* {@link Project#activeLayer}.
* An array of all layers in a project can be accessed through
* {@link Project#layers}.
*
* @extends Group
*/
var Layer = this.Layer = Group.extend(/** @lends Layer# */{
2013-04-06 03:16:08 -04:00
_class: 'Layer',
2011-05-23 11:00:37 -04:00
// DOCS: improve constructor code example.
/**
* Creates a new Layer item and places it at the end of the
* {@link Project#layers} array. The newly created layer will be activated,
* so all newly created items will be placed within it.
*
2013-03-16 09:03:13 -04:00
* @name Layer#initialize
* @param {Item[]} [children] An array of items that will be added to the
* newly created layer.
*
* @example
* var layer = new Layer();
2011-05-23 11:00:37 -04:00
*/
2013-03-16 09:03:13 -04:00
/**
* Creates a new Layer item and places it at the end of the
* {@link Project#layers} array. The newly created layer will be activated,
* so all newly created items will be placed within it.
*
* @param {Object} object An object literal containing properties to
2013-03-16 09:03:13 -04:00
* be set on the layer.
*
* @example {@paperscript}
* var path = new Path([100, 100], [100, 200]);
* var path2 = new Path([50, 150], [150, 150]);
*
* // Create a layer. The properties in the object literal
* // are set on the newly created layer.
* var layer = new Layer({
* children: [path, path2],
* strokeColor: 'black',
* position: view.center
* });
*/
initialize: function(items) {
this._project = paper.project;
// Push it onto project.layers and set index:
this._index = this._project.layers.push(this) - 1;
Group.apply(this, arguments);
this.activate();
},
/**
* Removes the layer from its project's layers list
* or its parent's children list.
*/
_remove: function(notify) {
if (this._parent)
return this.base(notify);
if (this._index != null) {
2013-04-23 01:48:36 -04:00
if (this._project.activeLayer === this)
this._project.activeLayer = this.getNextSibling()
|| this.getPreviousSibling();
Base.splice(this._project.layers, null, this._index, 1);
// Tell project we need a redraw. This is similar to _changed()
// mechanism.
this._project._needsRedraw();
return true;
}
return false;
},
getNextSibling: function() {
return this._parent ? this.base()
: this._project.layers[this._index + 1] || null;
},
getPreviousSibling: function() {
return this._parent ? this.base()
: this._project.layers[this._index - 1] || null;
},
isInserted: function() {
return this._parent ? this.base() : this._index != null;
},
2011-05-23 11:00:37 -04:00
/**
* Activates the layer.
*
2011-05-23 11:00:37 -04:00
* @example
2011-11-12 12:30:21 -05:00
* var firstLayer = project.activeLayer;
* var secondLayer = new Layer();
* console.log(project.activeLayer == secondLayer); // true
* firstLayer.activate();
* console.log(project.activeLayer == firstLayer); // true
2011-05-23 11:00:37 -04:00
*/
activate: function() {
this._project.activeLayer = this;
2011-02-11 12:37:36 -05:00
}
}, new function () {
function insert(above) {
return function(item) {
// If the item is a layer and contained within Project#layers, use
// our own version of move().
if (item instanceof Layer && !item._parent
&& this._remove(true)) {
Base.splice(item._project.layers, [this],
2012-11-19 23:41:04 -05:00
item._index + (above ? 1 : 0), 0);
this._setProject(item._project);
return true;
}
2011-05-07 12:27:19 -04:00
return this.base(item);
2011-05-02 06:23:42 -04:00
};
}
return {
insertAbove: insert(true),
insertBelow: insert(false)
};
});