forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath.js
More file actions
270 lines (230 loc) · 6.59 KB
/
Copy pathPath.js
File metadata and controls
270 lines (230 loc) · 6.59 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* 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.
*/
module('Path');
test('path.join(path)', function() {
var path = new Path();
path.add(0, 0);
path.add(10, 0);
var path2 = new Path();
path2.add(10, 0);
path2.add(20, 10);
path.join(path2);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } }');
equals(function() {
return paper.project.activeLayer.children.length;
}, 1);
var path = new Path();
path.add(0, 0);
path.add(10, 0);
var path2 = new Path();
path2.add(20, 10);
path2.add(10, 0);
path.join(path2);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } }');
var path = new Path();
path.add(0, 0);
path.add(10, 0);
var path2 = new Path();
path2.add(30, 10);
path2.add(40, 0);
path.join(path2);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 30, y: 10 } },{ point: { x: 40, y: 0 } }');
var path = new Path();
path.add(0, 0);
path.add(10, 0);
path.add(20, 10);
var path2 = new Path();
path2.add(0, 0);
path2.add(10, 5);
path2.add(20, 10);
path.join(path2);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } },{ point: { x: 10, y: 5 } }');
equals(function() {
return path.closed;
}, true);
});
test('path.remove()', function() {
var path = new Path();
path.add(0, 0);
path.add(10, 0);
path.add(20, 0);
path.add(30, 0);
path.removeSegment(0);
equals(function() {
return path.segments.length;
}, 3);
path.removeSegment(0);
equals(function() {
return path.segments.length;
}, 2);
path.removeSegments(0, 1);
equals(function() {
return path.segments.length;
}, 1);
path.remove();
equals(function() {
return paper.project.activeLayer.children.length;
}, 0);
});
test('path.removeSegments()', function() {
var path = new Path();
path.add(0, 0);
path.add(10, 0);
path.add(20, 0);
path.add(30, 0);
path.removeSegments();
equals(function() {
return path.segments.length;
}, 0);
});
test('Is the path deselected after setting a new list of segments?', function() {
var path = new Path([0, 0]);
path.selected = true;
equals(function() {
return path.selected;
}, true);
equals(function() {
return paper.project.selectedItems.length;
}, 1);
path.segments = [[0, 10]];
equals(function() {
return path.selected;
}, true);
equals(function() {
return paper.project.selectedItems.length;
}, 1);
});
test('Setting Path#fullySelected=true on an empty path should only set path#selected=true', function() {
var path = new Path();
path.fullySelected = true;
equals(function() {
return path.fullySelected;
}, false);
equals(function() {
return path.selected;
}, true);
});
test('After removing all segments of a fully selected path, it should still be selected.', function() {
var path = new Path([10, 20], [30, 40]);
path.fullySelected = true;
equals(function() {
return path.fullySelected;
}, true);
path.removeSegments();
equals(function() {
return path.fullySelected;
}, false);
equals(function() {
return path.selected;
}, true);
});
test('After removing all segments of a selected path, it should still be selected.', function() {
var path = new Path([10, 20], [30, 40]);
path.selected = true;
path.removeSegments();
equals(function() {
return path.selected;
}, true);
});
test('After simplifying a path using #simplify(), the path should stay fullySelected', function() {
var path = new Path();
for (var i = 0; i < 30; i++) {
path.add(i * 10, 10);
}
path.fullySelected = true;
equals(function() {
return path.selected;
}, true);
path.simplify();
equals(function() {
return path.selected;
}, true);
equals(function() {
return path.fullySelected;
}, true);
});
test('After cloning a selected item, it should be added to the Project#selectedItems array', function() {
var path = new Path.Circle(new Size(80, 50), 35);
path.selected = true;
var copy = path.clone();
equals(function() {
return paper.project.selectedItems.length;
}, 2);
});
test('After simplifying a path using #simplify(), the path should stay selected', function() {
var path = new Path();
for (var i = 0; i < 30; i++) {
path.add(i * 10, (i % 2 ? 20 : 40));
}
path.selected = true;
path.simplify();
equals(function() {
return path.selected;
}, true);
});
test('After smoothing a path using #smooth(), the path should stay fullySelected', function() {
var path = new Path();
for (var i = 0; i < 30; i++) {
path.add(i * 10, (i % 2 ? 20 : 40));
}
path.fullySelected = true;
path.smooth();
equals(function() {
return path.fullySelected;
}, true);
});
test('After smoothing a path using #smooth(), the path should stay selected', function() {
var path = new Path();
for (var i = 0; i < 30; i++) {
path.add(i * 10, (i % 2 ? 20 : 40));
}
path.selected = true;
path.smooth();
equals(function() {
return path.selected;
}, true);
});
test('After selecting a segment, Path#selected should return true', function() {
var path = new Path();
path.add([10, 10]);
path.firstSegment.selected = true;
equals(function() {
return path.selected;
}, true);
});
test('Path#reverse', function() {
var path = new Path.Circle([100, 100], 30);
path.reverse();
equals(path.segments.toString(), '{ point: { x: 100, y: 130 }, handleIn: { x: -16.56854, y: 0 }, handleOut: { x: 16.56854, y: 0 } },{ point: { x: 130, y: 100 }, handleIn: { x: 0, y: 16.56854 }, handleOut: { x: 0, y: -16.56854 } },{ point: { x: 100, y: 70 }, handleIn: { x: 16.56854, y: 0 }, handleOut: { x: -16.56854, y: 0 } },{ point: { x: 70, y: 100 }, handleIn: { x: 0, y: -16.56854 }, handleOut: { x: 0, y: 16.56854 } }');
});
test('Path#reverse should adjust segment indices', function() {
var path = new Path([[0, 0], [10, 10], [20, 20]]);
path.reverse();
equals(path.segments[0].index, 0);
equals(path.segments[1].index, 1);
equals(path.segments[2].index, 2);
});
test('Path#fullySelected', function() {
var path = new Path.Circle([100, 100], 10);
path.fullySelected = true;
path.segments[1].selected = false;
equals(function() {
return path.fullySelected;
}, false);
});
test('Simplifying a path with three segments of the same position should not throw an error', function() {
var path = new Path([20, 20], [20, 20], [20, 20]);
path.simplify();
equals(function() {
return path.segments.length;
}, 1);
});