forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayer.js
More file actions
130 lines (122 loc) · 3.79 KB
/
Copy pathLayer.js
File metadata and controls
130 lines (122 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @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 = Group.extend(/** @lends Layer# */{
_class: 'Layer',
// 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.
*
* @name Layer#initialize
* @param {Item[]} [children] An array of items that will be added to the
* newly created layer.
*
* @example
* var layer = new Layer();
*/
/**
* 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.
*
* @name Layer#initialize
* @param {Object} object an object literal containing the properties to 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 Layer(/* 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 _remove(notify) {
if (this._parent)
return _remove.base.call(this, notify);
if (this._index != null) {
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 = true;
return true;
}
return false;
},
getNextSibling: function getNextSibling() {
return this._parent ? getNextSibling.base.call(this)
: this._project.layers[this._index + 1] || null;
},
getPreviousSibling: function getPreviousSibling() {
return this._parent ? getPreviousSibling.base.call(this)
: this._project.layers[this._index - 1] || null;
},
isInserted: function isInserted() {
return this._parent ? isInserted.base.call(this) : this._index != null;
},
/**
* Activates the layer.
*
* @example
* var firstLayer = project.activeLayer;
* var secondLayer = new Layer();
* console.log(project.activeLayer == secondLayer); // true
* firstLayer.activate();
* console.log(project.activeLayer == firstLayer); // true
*/
activate: function() {
this._project.activeLayer = this;
},
// Private helper for #insertAbove() / #insertBelow()
_insert: function _insert(above, item, _preserve) {
// 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],
item._index + (above ? 1 : 0), 0);
this._setProject(item._project);
return this;
}
return _insert.base.call(this, above, item, _preserve);
}
});