2011-07-01 06:17:45 -04:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-07-01 06:17:45 -04:00
|
|
|
* 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/
|
2011-07-01 06:17:45 -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('Symbol & Placed Symbol');
|
2011-02-26 07:48:31 -05:00
|
|
|
|
2016-01-31 10:52:51 -05:00
|
|
|
test('SymbolItem#bounds', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path.Circle([50, 50], 50);
|
|
|
|
path.strokeColor = 'black';
|
|
|
|
path.strokeWidth = 1;
|
|
|
|
path.strokeCap = 'round';
|
|
|
|
path.strokeJoin = 'round';
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(path.strokeBounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(-0.5, -0.5, 101, 101),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Path initial bounds');
|
2016-01-31 10:52:51 -05:00
|
|
|
var definition = new SymbolDefinition(path);
|
|
|
|
var item = new SymbolItem(definition);
|
2011-03-06 19:01:26 -05:00
|
|
|
|
2016-01-31 10:52:51 -05:00
|
|
|
equals(item.bounds,
|
2014-08-16 13:24:54 -04:00
|
|
|
new Rectangle(-50.5, -50.5, 101, 101),
|
2016-01-31 10:52:51 -05:00
|
|
|
'SymbolItem initial bounds');
|
2011-03-06 19:01:26 -05:00
|
|
|
|
2016-01-31 10:52:51 -05:00
|
|
|
item.scale(1, 0.5);
|
|
|
|
equals(item.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(-50.5, -25.25, 101, 50.5),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Bounds after scale');
|
2011-03-06 16:26:38 -05:00
|
|
|
|
2016-01-31 10:52:51 -05:00
|
|
|
item.rotate(40);
|
|
|
|
equals(item.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(-41.96283, -37.79252, 83.92567, 75.58503),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Bounds after rotation');
|
2011-03-06 10:14:57 -05:00
|
|
|
});
|
2011-05-21 14:32:41 -04:00
|
|
|
|
2016-01-31 10:52:51 -05:00
|
|
|
test('bounds of group of SymbolItem instances', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path.Circle(new Point(), 10);
|
2016-01-31 10:52:51 -05:00
|
|
|
var definition = new SymbolDefinition(path);
|
2014-08-16 13:24:54 -04:00
|
|
|
var instances = [];
|
|
|
|
for (var i = 0; i < 10; i++) {
|
2016-01-31 10:52:51 -05:00
|
|
|
var instance = definition.place(new Point(i * 20, 20));
|
2014-08-16 13:24:54 -04:00
|
|
|
instances.push(instance);
|
|
|
|
}
|
|
|
|
var group = new Group(instances);
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(group.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(-10, 10, 200, 20),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Group bounds');
|
2011-07-04 09:09:17 -04:00
|
|
|
});
|
|
|
|
|
2016-01-31 10:52:51 -05:00
|
|
|
test('bounds of a SymbolItem that contains a group of items', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path.Circle(new Point(), 10);
|
|
|
|
var path2 = path.clone();
|
|
|
|
path2.position.x += 20;
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(path.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(-10, -10, 20, 20),
|
2014-08-16 13:24:54 -04:00
|
|
|
'path bounds');
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(path2.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(10, -10, 20, 20),
|
2014-08-16 13:24:54 -04:00
|
|
|
'path2 bounds');
|
|
|
|
var group = new Group(path, path2);
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(group.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(-10, -10, 40, 20),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Group bounds');
|
2016-01-31 10:52:51 -05:00
|
|
|
var definition = new SymbolDefinition(group);
|
|
|
|
var instance = definition.place(new Point(50, 50));
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(instance.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(30, 40, 40, 20),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Instance bounds');
|
2011-07-04 09:09:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Changing the definition of a symbol should change the bounds of all instances of it.', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path.Circle(new Point(), 10);
|
|
|
|
var path2 = new Path.Circle(new Point(), 20);
|
2016-01-31 10:52:51 -05:00
|
|
|
var definition = new SymbolDefinition(path);
|
|
|
|
var instance = definition.place(new Point(0, 0));
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(instance.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(-10, -10, 20, 20),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Initial bounds');
|
2016-01-31 10:52:51 -05:00
|
|
|
definition.item = path2;
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(instance.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(-20, -20, 40, 40),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Bounds after changing symbol definition');
|
2016-01-31 10:52:51 -05:00
|
|
|
definition.item.scale(0.5, 0.5);
|
2014-12-28 08:59:48 -05:00
|
|
|
equals(instance.bounds,
|
2014-12-28 09:21:38 -05:00
|
|
|
new Rectangle(-10, -10, 20, 20),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Bounds after modifying symbol definition');
|
2011-07-04 09:09:17 -04:00
|
|
|
});
|
|
|
|
|
2016-06-10 15:32:22 -04:00
|
|
|
test('SymbolDefinition item selection', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path.Circle([50, 50], 50);
|
|
|
|
path.selected = true;
|
2016-01-31 10:52:51 -05:00
|
|
|
var definition = new SymbolDefinition(path);
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2017-03-22 09:42:11 -04:00
|
|
|
return definition.item.selected == false;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
|
|
|
equals(function() {
|
2016-01-31 10:52:51 -05:00
|
|
|
return paper.project.selectedItems.length === 0;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
2011-06-03 16:27:18 -04:00
|
|
|
});
|
|
|
|
|
2016-06-10 15:32:22 -04:00
|
|
|
test('SymbolDefinition#place()', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path.Circle([50, 50], 50);
|
2016-06-10 15:32:22 -04:00
|
|
|
var symbol = new SymbolDefinition(path);
|
|
|
|
var placed = symbol.place();
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2016-06-10 15:32:22 -04:00
|
|
|
return placed.parent == paper.project.activeLayer;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
2011-06-03 16:27:18 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2016-06-10 15:32:22 -04:00
|
|
|
return placed.definition == symbol;
|
2014-08-16 13:24:54 -04:00
|
|
|
}, true);
|
2011-06-03 16:27:18 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2016-06-10 15:32:22 -04:00
|
|
|
return placed.position.toString();
|
2014-08-16 13:24:54 -04:00
|
|
|
}, '{ x: 0, y: 0 }');
|
2011-06-03 16:27:18 -04:00
|
|
|
});
|
|
|
|
|
2016-06-10 15:32:22 -04:00
|
|
|
test('SymbolDefinition#place(position)', function() {
|
2014-08-16 13:24:54 -04:00
|
|
|
var path = new Path.Circle([50, 50], 50);
|
2016-06-10 15:32:22 -04:00
|
|
|
var symbol = new SymbolDefinition(path);
|
|
|
|
var placed = symbol.place(new Point(100, 100));
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(function() {
|
2016-06-10 15:32:22 -04:00
|
|
|
return placed.position.toString();
|
2014-08-16 13:24:54 -04:00
|
|
|
}, '{ x: 100, y: 100 }');
|
2011-06-03 16:27:18 -04:00
|
|
|
});
|
2016-06-10 15:32:22 -04:00
|
|
|
|
|
|
|
test('SymbolItem#bounds with #applyMatrix = false', function() {
|
|
|
|
var path = new Path.Rectangle({
|
|
|
|
point: [100, 100],
|
|
|
|
size: [50, 50],
|
|
|
|
strokeColor: 'red',
|
|
|
|
applyMatrix: false,
|
|
|
|
strokeWidth: 50
|
|
|
|
});
|
|
|
|
var symbol = new SymbolDefinition(path);
|
|
|
|
var placed = symbol.place([200, 200]);
|
|
|
|
equals(function() { return placed.bounds; },
|
|
|
|
{ x: 150, y: 150, width: 100, height: 100 });
|
|
|
|
});
|
2019-07-12 06:06:26 -04:00
|
|
|
|
|
|
|
test('SymbolItem#hitTestAll', function() {
|
|
|
|
var symbol = new SymbolDefinition(
|
|
|
|
new Path.Circle({
|
|
|
|
center: [0, 0],
|
|
|
|
radius: 10,
|
|
|
|
fillColor: 'orange'
|
|
|
|
})
|
|
|
|
);
|
|
|
|
var symbolItem = symbol.place([50, 50]);
|
|
|
|
|
|
|
|
var hitTestAll = symbolItem.hitTestAll([50, 50]);
|
|
|
|
equals(hitTestAll.length, 1);
|
|
|
|
equals(hitTestAll[0].item.id, symbolItem.id);
|
|
|
|
});
|