paper.js/test/tests/Item_Bounds.js

76 lines
3 KiB
JavaScript
Raw Normal View History

2011-11-28 16:59:34 -05:00
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
2011-11-28 16:59:34 -05:00
* http://paperjs.org/
*
2014-01-03 19:47:16 -05:00
* Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey
* http://scratchdisk.com/ & http://jonathanpuckey.com/
2011-11-28 16:59:34 -05:00
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
module('Item Bounds');
test('item.bounds caching', function() {
2014-08-16 13:24:54 -04:00
var circle = new Path.Circle(new Point(100, 100), 50);
var rectangle = new Path.Rectangle(new Point(75, 75), new Point(175, 175));
var group = new Group([circle, rectangle]);
equals(group.bounds, new Rectangle(50, 50, 125, 125), 'group.bounds');
2014-08-16 13:24:54 -04:00
rectangle.remove();
equals(function() {
return group.children.length;
}, 1);
equals(group.bounds, new Rectangle(50, 50, 100, 100), 'group.bounds without rectangle');
2014-08-16 13:24:54 -04:00
group.addChild(rectangle);
equals(function() {
return group.children.length;
}, 2);
equals(group.bounds, new Rectangle(50, 50, 125, 125), 'group.bounds with rectangle');
2014-08-16 13:24:54 -04:00
circle.remove();
equals(function() {
return group.children.length;
}, 1);
equals(group.bounds, new Rectangle(75, 75, 100, 100), 'group.bounds without circle');
2014-08-16 13:24:54 -04:00
group.addChild(circle);
equals(function() {
return group.children.length;
}, 2);
equals(group.bounds, new Rectangle(50, 50, 125, 125), 'group.bounds with circle');
2011-11-28 16:59:34 -05:00
});
test('group.bounds when group contains empty group', function() {
2014-08-16 13:24:54 -04:00
var group = new Group();
var rectangle = new Path.Rectangle(new Point(75, 75), new Point(175, 175));
group.addChild(rectangle);
equals(group.bounds, new Rectangle(75, 75, 100, 100), 'group.bounds without empty group');
2014-08-16 13:24:54 -04:00
group.addChild(new Group());
equals(group.bounds, new Rectangle(75, 75, 100, 100), 'group.bounds with empty group');
});
test('group.bounds and position after children were modified', function() {
2014-08-16 13:24:54 -04:00
var group = new Group();
var rectangle = new Path.Rectangle(new Point(100, 100), new Point(200, 200));
group.addChild(rectangle);
equals(group.bounds, new Rectangle(100, 100, 100, 100), 'group.bounds before change');
equals(group.position, new Point(150, 150), 'group.position before change');
2014-08-16 13:24:54 -04:00
rectangle.firstSegment.point = [0, 0];
equals(group.bounds, new Rectangle(0, 0, 200, 200), 'group.bounds after change');
equals(group.position, new Point(100, 100), 'group.position after change');
});
test('path.bounds when contained in a transformed group', function() {
2014-08-16 13:24:54 -04:00
var path = new Path([10, 10], [60, 60]);
var group = new Group([path]);
equals(path.bounds, new Rectangle(10, 10, 50, 50), 'path.bounds before group translation');
2014-08-16 13:24:54 -04:00
group.translate(100, 100);
equals(path.bounds, new Rectangle(110, 110, 50, 50), 'path.bounds after group translation');
});
test('text.bounds', function() {
2014-08-16 13:24:54 -04:00
var text = new PointText(new Point(50, 100));
text.fillColor = 'black';
text.content = 'This is a test';
equals(text.bounds, new Rectangle(50, 89.2, 67, 14.4), 'text.bounds', { tolerance: 0.5 });
});