forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.js
More file actions
666 lines (568 loc) · 15.4 KB
/
Copy pathItem.js
File metadata and controls
666 lines (568 loc) · 15.4 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*
* 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('Item');
test('copyTo(project)', function() {
var project = paper.project;
var path = new Path();
var secondDoc = new Project();
var copy = path.copyTo(secondDoc);
equals(function() {
return secondDoc.activeLayer.children.indexOf(copy) != -1;
}, true);
equals(function() {
return project.activeLayer.children.indexOf(copy) == -1;
}, true);
equals(function() {
return copy != path;
}, true);
});
test('copyTo(layer)', function() {
var project = paper.project;
var path = new Path();
var layer = new Layer();
var copy = path.copyTo(layer);
equals(function() {
return layer.children.indexOf(copy) != -1;
}, true);
equals(function() {
return project.layers[0].children.indexOf(copy) == -1;
}, true);
});
test('clone()', function() {
var project = paper.project;
var path = new Path();
var copy = path.clone();
equals(function() {
return project.activeLayer.children.length;
}, 2);
equals(function() {
return path != copy;
}, true);
});
test('addChild(item)', function() {
var project = paper.project;
var path = new Path();
project.activeLayer.addChild(path);
equals(function() {
return project.activeLayer.children.length;
}, 1);
});
test('setting item.parent', function() {
var layer1 = paper.project.activeLayer;
var layer2 = new Layer();
layer1.activate();
var group = new Group();
var path = new Path();
equals(function() {
return path.parent === layer1;
}, true, 'Path is a child of layer1 because it is active');
path.parent = layer2;
equals(function() {
return path.parent === layer2;
}, true, 'The parent of path was set to layer2');
path.parent = group;
equals(function() {
return path.parent === group;
}, true, 'The parent of path was set to group');
equals(function() {
return layer2.children.indexOf(path) === -1;
}, true, 'The path is no longer a child of layer2');
var path2 = new Path({
parent: group
});
equals(function() {
return path2.parent === group;
}, true, 'setting the parent in the constructor');
equals(function() {
return group.children.indexOf(path2) == 1;
}, true, 'the index of path2 is 1, because group already contains path from before');
});
test('item.parent / item.isChild / item.isParent / item.layer', function() {
var project = paper.project;
var secondDoc = new Project();
var path = new Path();
project.activeLayer.addChild(path);
equals(function() {
return project.activeLayer.children.indexOf(path) != -1;
}, true);
equals(function() {
return path.layer == project.activeLayer;
}, true);
secondDoc.activeLayer.addChild(path);
equals(function() {
return project.activeLayer.isChild(path);
}, false);
equals(function() {
return path.layer == secondDoc.activeLayer;
}, true);
equals(function() {
return path.isParent(project.activeLayer);
}, false);
equals(function() {
return secondDoc.activeLayer.isChild(path);
}, true);
equals(function() {
return path.isParent(secondDoc.activeLayer);
}, true);
equals(function() {
return project.activeLayer.children.indexOf(path) == -1;
}, true);
equals(function() {
return secondDoc.activeLayer.children.indexOf(path) == 0;
}, true);
});
test('item.lastChild / item.firstChild', function() {
var project = paper.project;
var path = new Path();
var secondPath = new Path();
equals(function() {
return project.activeLayer.firstChild == path;
}, true);
equals(function() {
return project.activeLayer.lastChild == secondPath;
}, true);
});
test('insertChild(0, item)', function() {
var project = paper.project;
var path = new Path();
var secondPath = new Path();
project.activeLayer.insertChild(0, secondPath);
equals(function() {
return secondPath.index < path.index;
}, true);
});
test('insertAbove(item)', function() {
var project = paper.project;
var path = new Path();
var secondPath = new Path();
path.insertAbove(secondPath);
equals(function() {
return project.activeLayer.lastChild == path;
}, true);
});
test('insertBelow(item)', function() {
var project = paper.project;
var firstPath = new Path();
var secondPath = new Path();
equals(function() {
return secondPath.index > firstPath.index;
}, true);
secondPath.insertBelow(firstPath);
equals(function() {
return secondPath.index < firstPath.index;
}, true);
});
test('sendToBack()', function() {
var project = paper.project;
var firstPath = new Path();
var secondPath = new Path();
secondPath.sendToBack();
equals(function() {
return secondPath.index == 0;
}, true);
});
test('bringToFront()', function() {
var project = paper.project;
var firstPath = new Path();
var secondPath = new Path();
firstPath.bringToFront();
equals(function() {
return firstPath.index == 1;
}, true);
});
test('isDescendant(item) / isAncestor(item)', function() {
var project = paper.project;
var path = new Path();
equals(function() {
return path.isDescendant(project.activeLayer);
}, true);
equals(function() {
return project.activeLayer.isDescendant(path);
}, false);
equals(function() {
return path.isAncestor(project.activeLayer);
}, false);
equals(function() {
return project.activeLayer.isAncestor(path);
}, true);
// an item can't be its own descendant:
equals(function() {
return project.activeLayer.isDescendant(project.activeLayer);
}, false);
// an item can't be its own ancestor:
equals(function() {
return project.activeLayer.isAncestor(project.activeLayer);
}, false);
});
test('addChildren(items)', function() {
var project = paper.project;
var path1 = new Path(),
path2 = new Path(),
path3 = new Path(),
group = new Group([path1, path2, path3]);
function check(i1, i2, i3) {
equals(function() {
return group.children.length;
}, 3);
equals(function() {
return path1.index;
}, i1);
equals(function() {
return path2.index;
}, i2);
equals(function() {
return path3.index;
}, i3);
}
check(0, 1, 2);
group.addChild(path1);
check(2, 0, 1);
group.addChild(path2);
check(1, 2, 0);
group.addChildren([path1, path2, path3]);
check(0, 1, 2);
});
test('isGroupedWith', function() {
var project = paper.project;
var path = new Path();
var secondPath = new Path();
var group = new Group([path]);
var secondGroup = new Group([secondPath]);
equals(function() {
return path.isGroupedWith(secondPath);
}, false);
secondGroup.addChild(path);
equals(function() {
return path.isGroupedWith(secondPath);
}, true);
equals(function() {
return path.isGroupedWith(group);
}, false);
equals(function() {
return path.isDescendant(secondGroup);
}, true);
equals(function() {
return secondGroup.isDescendant(path);
}, false);
equals(function() {
return secondGroup.isDescendant(secondGroup);
}, false);
equals(function() {
return path.isGroupedWith(secondGroup);
}, false);
paper.project.activeLayer.addChild(path);
equals(function() {
return path.isGroupedWith(secondPath);
}, false);
paper.project.activeLayer.addChild(secondPath);
equals(function() {
return path.isGroupedWith(secondPath);
}, false);
});
test('getPreviousSibling() / getNextSibling()', function() {
var firstPath = new Path();
var secondPath = new Path();
equals(function() {
return firstPath.nextSibling == secondPath;
}, true);
equals(function() {
return secondPath.previousSibling == firstPath;
}, true);
equals(function() {
return secondPath.nextSibling == null;
}, true);
});
test('reverseChildren()', function() {
var project = paper.project;
var path = new Path();
var secondPath = new Path();
var thirdPath = new Path();
equals(function() {
return project.activeLayer.firstChild == path;
}, true);
project.activeLayer.reverseChildren();
equals(function() {
return project.activeLayer.firstChild == path;
}, false);
equals(function() {
return project.activeLayer.firstChild == thirdPath;
}, true);
equals(function() {
return project.activeLayer.lastChild == path;
}, true);
});
test('Check item#project when moving items across projects', function() {
var project = paper.project;
var doc1 = new Project();
var path = new Path();
var group = new Group();
group.addChild(new Path());
equals(function() {
return path.project == doc1;
}, true);
var doc2 = new Project();
doc2.activeLayer.addChild(path);
equals(function() {
return path.project == doc2;
}, true);
doc2.activeLayer.addChild(group);
equals(function() {
return group.children[0].project == doc2;
}, true);
});
test('group.selected', function() {
var path = new Path([0, 0]);
var path2 = new Path([0, 0]);
var group = new Group([path, path2]);
path.selected = true;
equals(function() {
return group.selected;
}, true);
path.selected = false;
equals(function() {
return group.selected;
}, false);
group.selected = true;
equals(function() {
return path.selected;
}, true);
equals(function() {
return path2.selected;
}, true);
group.selected = false;
equals(function() {
return path.selected;
}, false);
equals(function() {
return path2.selected;
}, false);
});
test('Check parent children object for named item', function() {
var path = new Path();
path.name = 'test';
equals(function() {
return paper.project.activeLayer.children['test'] == path;
}, true);
var path2 = new Path();
path2.name = 'test';
equals(function() {
return paper.project.activeLayer.children['test'] == path2;
}, true);
path2.remove();
equals(function() {
return paper.project.activeLayer.children['test'] == path;
}, true);
path.remove();
equals(function() {
return !paper.project.activeLayer.children['test'];
}, true);
});
test('Named child access 1', function() {
var path = new Path();
path.name = 'test';
var path2 = new Path();
path2.name = 'test';
path.remove();
equals(function() {
return paper.project.activeLayer.children['test'] == path2;
}, true);
});
test('Named child access 2', function() {
var path = new Path();
path.name = 'test';
var path2 = new Path();
path2.name = 'test';
path.remove();
equals(function() {
return paper.project.activeLayer.children['test'] == path2;
}, true);
equals(function() {
return paper.project.activeLayer._namedChildren['test'].length == 1;
}, true);
path2.remove();
equals(function() {
return !paper.project.activeLayer._namedChildren['test'];
}, true);
equals(function() {
return paper.project.activeLayer.children['test'] === undefined;
}, true);
});
test('Named child access 3', function() {
var path = new Path();
path.name = 'test';
var path2 = new Path();
path2.name = 'test';
var group = new Group();
group.addChild(path2);
equals(function() {
return paper.project.activeLayer.children['test'] == path;
}, true);
// TODO: Tests should not access internal properties
equals(function() {
return paper.project.activeLayer._namedChildren['test'].length;
}, 1);
equals(function() {
return group.children['test'] == path2;
}, true);
equals(function() {
return group._namedChildren['test'].length == 1;
}, true);
equals(function() {
return paper.project.activeLayer._namedChildren['test'][0] == path;
}, true);
paper.project.activeLayer.appendTop(path2);
equals(function() {
return group.children['test'] == null;
}, true);
equals(function() {
return group._namedChildren['test'] === undefined;
}, true);
equals(function() {
return paper.project.activeLayer.children['test'] == path2;
}, true);
equals(function() {
return paper.project.activeLayer._namedChildren['test'].length;
}, 2);
});
test('Setting name of child back to null', function() {
var path = new Path();
path.name = 'test';
var path2 = new Path();
path2.name = 'test';
equals(function() {
return paper.project.activeLayer.children['test'] == path2;
}, true);
path2.name = null;
equals(function() {
return paper.project.activeLayer.children['test'] == path;
}, true);
path.name = null;
equals(function() {
return paper.project.activeLayer.children['test'] === undefined;
}, true);
});
test('Renaming item', function() {
var path = new Path();
path.name = 'test';
path.name = 'test2';
equals(function() {
return paper.project.activeLayer.children['test'] === undefined;
}, true);
equals(function() {
return paper.project.activeLayer.children['test2'] == path;
}, true);
});
test('Changing item#position.x', function() {
var path = new Path.Circle(new Point(50, 50), 50);
path.position.x += 5;
equals(path.position.toString(), '{ x: 55, y: 50 }',
'path.position.x += 5');
});
test('Naming a removed item', function() {
var path = new Path();
path.remove();
path.name = 'test';
equals(function() {
return path.name;
}, 'test');
});
test('Naming a layer', function() {
var layer = new Layer();
layer.name = 'test';
equals(function() {
return layer.name;
}, 'test');
});
test('Cloning a linked size', function() {
var path = new Path([40, 75], [140, 75]);
var error = null;
try {
var cloneSize = path.bounds.size.clone();
} catch (e) {
error = e;
}
var description = 'Cloning a linked size should not throw an error';
if (error)
description += ': ' + error;
equals(error == null, true, description);
});
test('Item#type', function() {
equals(new Group().type, 'group');
equals(new Path().type, 'path');
equals(new CompoundPath().type, 'compound-path');
var canvas = document.createElement('canvas');
equals(new Raster(canvas).type, 'raster');
equals(new PlacedSymbol().type, 'placed-symbol');
equals(new PointText().type, 'point-text');
});
test('Item#isInserted', function() {
var item = new Path();
equals(item.isInserted(), true);
item.remove();
equals(item.isInserted(), false);
var group = new Group(item);
equals(item.isInserted(), true);
group.remove();
equals(item.isInserted(), false);
});
test('Item#data', function() {
var item = new Path();
var description = 'When accessed before any data was set, a plain object is created for us';
equals(Base.isPlainObject(item.data), true, description);
var item = new Path();
item.data.test = true;
equals(item.data.test, true, description);
var item = new Path();
var point = new Point();
item.data.point = point;
equals(item.data.point, point, 'We can set basic types on data');
var item = new Path();
item.data = {
testing: true
}
equals(item.data.testing, true, 'we can set data using an object literal');
var item = new Path({
data: {
testing: true
}
});
equals(item.data.testing, true,
'we can set data using an object literal constructor');
// TODO: add tests to see if importing and exporting of Item#data works
});
test('Item#blendMode in a transformed Group', function() {
var layer = new Layer();
var path1 = new Path.Rectangle({
size: [100, 100],
fillColor: new Color(1, 0, 0)
});
var path2 = new Path.Circle({
radius: 25,
center: [50, 50],
fillColor: new Color(0, 1, 0),
blendMode: 'screen'
});
var raster = layer.rasterize();
compareColors(raster.getPixel(0, 0), new Color(1, 0, 0, 1),
'Top left pixel should be red:');
compareColors(raster.getPixel(50, 50), new Color(1, 1, 0, 1),
'Middle center pixel should be yellow:');
raster.remove();
path2.position = [0, 0];
var group = new Group(path2);
group.position = [50, 50];
var raster = layer.rasterize();
compareColors(raster.getPixel(0, 0), new Color(1, 0, 0, 1),
'Top left pixel should be red:');
compareColors(raster.getPixel(50, 50), new Color(1, 1, 0, 1),
'Middle center pixel should be yellow:');
});