2014-04-15 11:10:47 -04:00
|
|
|
/*
|
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
2020-05-23 16:24:42 -04:00
|
|
|
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
|
|
|
|
* http://juerglehni.com/ & https://puckey.studio/
|
2014-04-15 11:10:47 -04:00
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2016-01-27 13:57:07 -05:00
|
|
|
QUnit.module('Getting and Matching Items');
|
2014-04-15 11:10:47 -04:00
|
|
|
|
|
|
|
test('Item#getItems()', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var group = new Group([new Path({ selected: true }), new Raster()]);
|
|
|
|
equals(function() {
|
2014-10-18 08:32:21 -04:00
|
|
|
return group.getItems({
|
2014-08-16 13:24:54 -04:00
|
|
|
type: 'path'
|
2014-10-18 08:32:21 -04:00
|
|
|
}).length;
|
|
|
|
}, 1);
|
2014-08-16 13:24:54 -04:00
|
|
|
|
|
|
|
equals(function() {
|
2014-10-18 08:32:21 -04:00
|
|
|
return group.getItems({
|
2014-08-16 13:24:54 -04:00
|
|
|
selected: true
|
2014-10-18 08:32:21 -04:00
|
|
|
}).length;
|
|
|
|
}, 1);
|
2014-04-15 11:10:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Item#matches()', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path();
|
|
|
|
equals(function() {
|
|
|
|
return path.matches({
|
|
|
|
visible: false
|
|
|
|
});
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
equals(function() {
|
|
|
|
return path.matches({
|
|
|
|
visible: true
|
|
|
|
});
|
|
|
|
}, true);
|
2014-04-15 11:10:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Project#getItems()', function() {
|
2014-09-27 16:31:49 -04:00
|
|
|
var layer = new Layer();
|
|
|
|
|
2014-10-18 08:32:21 -04:00
|
|
|
var matches = paper.project.getItems({
|
2015-12-27 08:49:53 -05:00
|
|
|
class: Layer
|
2014-10-18 08:32:21 -04:00
|
|
|
});
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2014-09-27 16:31:49 -04:00
|
|
|
return matches.length == 1 && matches[0] == layer;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
|
|
|
|
2014-10-18 08:32:21 -04:00
|
|
|
var matches = paper.project.getItems({
|
|
|
|
class: Item
|
|
|
|
});
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2014-09-27 16:31:49 -04:00
|
|
|
return matches.length == 1 && matches[0] == layer;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
|
|
|
|
|
|
|
var path = new Path();
|
2014-10-18 08:32:21 -04:00
|
|
|
var matches = paper.project.getItems({
|
2015-12-27 08:49:53 -05:00
|
|
|
class: Path
|
2014-10-18 08:32:21 -04:00
|
|
|
});
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return matches.length == 1 && matches[0] == path;
|
|
|
|
}, true);
|
|
|
|
|
2015-12-27 08:49:53 -05:00
|
|
|
var group = new Group();
|
2014-10-18 08:32:21 -04:00
|
|
|
var matches = paper.project.getItems({
|
2015-12-27 08:49:53 -05:00
|
|
|
className: 'Group'
|
2014-10-18 08:32:21 -04:00
|
|
|
});
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2016-01-27 13:57:07 -05:00
|
|
|
return matches.length == 1 && matches[0] === group;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
|
|
|
|
2014-10-18 08:32:21 -04:00
|
|
|
var matches = paper.project.getItems({
|
|
|
|
type: 'group'
|
|
|
|
});
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2016-01-27 13:57:07 -05:00
|
|
|
return matches.length == 1 && matches[0] === group;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
|
|
|
|
|
|
|
var raster = new Raster();
|
2014-10-18 08:32:21 -04:00
|
|
|
var matches = paper.project.getItems({
|
2015-12-27 08:49:53 -05:00
|
|
|
class: Raster
|
2014-10-18 08:32:21 -04:00
|
|
|
});
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2016-01-27 13:57:07 -05:00
|
|
|
return matches.length == 1 && matches[0] === raster;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
|
|
|
|
|
|
|
equals(function() {
|
|
|
|
return paper.project.getItems({
|
|
|
|
selected: true
|
|
|
|
}).length;
|
|
|
|
}, 0);
|
|
|
|
|
2014-10-18 08:32:21 -04:00
|
|
|
raster.selected = true;
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return paper.project.getItems({
|
|
|
|
selected: true
|
|
|
|
}).length;
|
|
|
|
}, 2);
|
|
|
|
|
2014-10-18 08:32:21 -04:00
|
|
|
raster.selected = true;
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return paper.project.getItems({
|
|
|
|
selected: true,
|
2015-12-27 08:49:53 -05:00
|
|
|
class: Raster
|
2014-08-16 13:24:54 -04:00
|
|
|
}).length;
|
|
|
|
}, 1);
|
2014-04-15 11:10:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Project#getItems() with compare function', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var firstPath = new Path();
|
|
|
|
var path = new Path({
|
|
|
|
opacity: 0.5
|
|
|
|
});
|
|
|
|
|
2014-10-18 08:32:21 -04:00
|
|
|
var items = paper.project.getItems({
|
|
|
|
opacity: function(value) {
|
2016-01-27 13:57:07 -05:00
|
|
|
return value < 1;
|
2014-10-18 08:32:21 -04:00
|
|
|
}
|
|
|
|
});
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return items.length == 1 && items[0] == path;
|
|
|
|
}, true);
|
2014-04-15 11:10:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Project#getItems() with specific property value', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path();
|
|
|
|
var decoyPath = new Path({
|
|
|
|
opacity: 0.5
|
|
|
|
});
|
|
|
|
|
2014-10-18 08:32:21 -04:00
|
|
|
var items = paper.project.getItems({
|
|
|
|
opacity: 1,
|
|
|
|
type: 'path'
|
|
|
|
});
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return items.length == 1 && items[0] == path;
|
|
|
|
}, true);
|
2014-04-15 11:10:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Project#getItems() with color', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path({
|
|
|
|
fillColor: 'red'
|
|
|
|
});
|
|
|
|
|
|
|
|
var decoyPath = new Path({
|
|
|
|
fillColor: 'black'
|
|
|
|
});
|
|
|
|
|
2014-10-18 08:32:21 -04:00
|
|
|
var items = paper.project.getItems({
|
|
|
|
fillColor: 'red',
|
|
|
|
type: 'path'
|
|
|
|
});
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
|
|
|
return items.length == 1 && items[0] == path;
|
|
|
|
}, true);
|
2014-04-15 11:10:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Project#getItems() with regex function', function() {
|
2016-01-27 13:57:07 -05:00
|
|
|
var layer = paper.project.activeLayer;
|
|
|
|
var stopPath = new Path({
|
2014-08-16 13:24:54 -04:00
|
|
|
name: 'stop'
|
|
|
|
});
|
|
|
|
|
2016-01-27 13:57:07 -05:00
|
|
|
var pausePath = new Path({
|
2014-08-16 13:24:54 -04:00
|
|
|
name: 'pause'
|
|
|
|
});
|
|
|
|
|
2016-01-27 13:57:07 -05:00
|
|
|
var startPath = new Path({
|
2014-08-16 13:24:54 -04:00
|
|
|
name: 'starting'
|
|
|
|
});
|
|
|
|
|
2014-10-18 08:32:21 -04:00
|
|
|
var items = paper.project.getItems({
|
|
|
|
name: /^start/g
|
|
|
|
});
|
2016-01-27 13:57:07 -05:00
|
|
|
|
|
|
|
// console.log(paper.project.activeLayer);
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2016-01-27 13:57:07 -05:00
|
|
|
return items.length == 1 && items[0] == startPath;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
|
|
|
|
|
|
|
equals(function() {
|
|
|
|
var items = paper.project.getItems({
|
|
|
|
name: /^st/g
|
|
|
|
});
|
|
|
|
return items.length == 2;
|
|
|
|
}, true);
|
2014-04-15 11:10:47 -04:00
|
|
|
});
|
2014-10-20 12:19:47 -04:00
|
|
|
|
|
|
|
test('Project#getItems() empty: true', function() {
|
|
|
|
var layer = new Layer();
|
|
|
|
var empty1 = new Path();
|
|
|
|
var empty2 = new Path();
|
|
|
|
|
|
|
|
equals(function() {
|
|
|
|
return layer.children.length;
|
|
|
|
}, 2);
|
|
|
|
|
|
|
|
equals(function() {
|
|
|
|
return paper.project.getItems({
|
|
|
|
empty: true
|
|
|
|
}).length;
|
|
|
|
}, 2);
|
|
|
|
});
|
2016-02-12 13:58:32 -05:00
|
|
|
|
|
|
|
test('Project#getItems() overlapping', function() {
|
|
|
|
var path = new Path.Circle({
|
|
|
|
radius: 100,
|
|
|
|
center: [200, 200],
|
|
|
|
fillColor: 'red'
|
|
|
|
});
|
|
|
|
|
2016-02-12 14:09:41 -05:00
|
|
|
equals(function() {
|
|
|
|
var matches = project.getItems({
|
|
|
|
class: Path,
|
|
|
|
overlapping: [0, 0, 400, 400]
|
|
|
|
});
|
|
|
|
return matches.length == 1 && matches[0] == path;
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
equals(function() {
|
|
|
|
var matches = project.getItems({
|
|
|
|
class: Path,
|
|
|
|
overlapping: [200, 0, 400, 400]
|
|
|
|
});
|
|
|
|
return matches.length == 1 && matches[0] == path;
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
equals(function() {
|
|
|
|
var matches = project.getItems({
|
|
|
|
class: Path,
|
|
|
|
overlapping: [400, 0, 400, 400]
|
|
|
|
});
|
|
|
|
return matches.length == 0;
|
|
|
|
}, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Project#getItems() inside', function() {
|
|
|
|
var path = new Path.Circle({
|
|
|
|
radius: 100,
|
|
|
|
center: [200, 200],
|
|
|
|
fillColor: 'red'
|
|
|
|
});
|
2016-02-12 13:58:32 -05:00
|
|
|
|
|
|
|
equals(function() {
|
|
|
|
var matches = project.getItems({
|
|
|
|
class: Path,
|
2016-02-12 14:09:41 -05:00
|
|
|
inside: [0, 0, 400, 400]
|
2016-02-12 13:58:32 -05:00
|
|
|
});
|
|
|
|
return matches.length == 1 && matches[0] == path;
|
|
|
|
}, true);
|
2016-02-12 14:09:41 -05:00
|
|
|
|
|
|
|
equals(function() {
|
|
|
|
var matches = project.getItems({
|
|
|
|
class: Path,
|
|
|
|
inside: [200, 0, 400, 400]
|
|
|
|
});
|
|
|
|
return matches.length == 0;
|
|
|
|
}, true);
|
2016-02-12 13:58:32 -05:00
|
|
|
});
|