paper.js/test/tests/Style.js

180 lines
4.8 KiB
JavaScript
Raw Normal View History

/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* 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/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
module('Style');
2011-02-16 16:11:26 -05:00
test('style defaults', function() {
var path = new Path();
2011-05-29 16:35:57 -04:00
equals(function() {
return path.strokeWidth;
}, 1);
equals(function() {
2011-05-29 16:23:16 -04:00
return path.strokeCap;
}, 'butt');
equals(function() {
2011-05-29 16:23:16 -04:00
return path.strokeJoin;
}, 'miter');
equals(function() {
2011-05-29 16:23:16 -04:00
return path.miterLimit;
}, 10);
equals(function() {
2011-05-29 16:35:57 -04:00
return path.dashOffset;
}, 0);
equals(function() {
return path.dashArray + '';
}, [] + '');
});
2011-02-16 16:11:26 -05:00
test('currentStyle', function() {
paper.project.currentStyle.fillColor = 'black';
2011-02-16 16:11:26 -05:00
var path = new Path();
2013-04-08 03:19:47 -04:00
compareColors(path.fillColor, 'black', 'path.fillColor');
2011-07-07 10:09:02 -04:00
// When changing the current style of the project, the style of
// paths created using project.currentStyle should not change.
paper.project.currentStyle.fillColor = 'red';
2013-04-08 03:19:47 -04:00
compareColors(path.fillColor, 'black', 'path.fillColor');
2011-02-16 16:11:26 -05:00
});
test('setting currentStyle to an object', function() {
paper.project.currentStyle = {
2011-02-16 16:11:26 -05:00
fillColor: 'red',
strokeColor: 'green'
};
var path = new Path();
2013-04-08 03:19:47 -04:00
compareColors(path.fillColor, 'red', 'path.fillColor');
compareColors(path.strokeColor, 'green', 'path.strokeColor');
2011-02-16 16:11:26 -05:00
});
test('setting path styles to an object', function() {
var path = new Path();
path.style = {
fillColor: 'red',
strokeColor: 'green'
};
2013-04-08 03:19:47 -04:00
compareColors(path.fillColor, 'red', 'path.fillColor');
compareColors(path.strokeColor, 'green', 'path.strokeColor');
2011-02-16 16:11:26 -05:00
});
test('setting group styles to an object', function() {
var group = new Group();
var path = new Path();
group.addChild(path);
2011-02-16 16:11:26 -05:00
group.style = {
fillColor: 'red',
strokeColor: 'green'
};
2013-04-08 03:19:47 -04:00
compareColors(path.fillColor, 'red', 'path.fillColor');
compareColors(path.strokeColor, 'green', 'path.strokeColor');
2011-02-16 16:11:26 -05:00
});
test('getting group styles', function() {
var group = new Group();
var path = new Path();
path.fillColor = 'red';
group.addChild(path);
2011-02-16 16:11:26 -05:00
2013-04-08 03:19:47 -04:00
compareColors(group.fillColor, 'red', 'group.fillColor');
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
var secondPath = new Path();
secondPath.fillColor = 'black';
group.addChild(secondPath);
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
// the group now contains two paths with different fillColors and therefore
2011-05-05 08:37:57 -04:00
// should return undefined:
equals(function() {
return group.fillColor;
}, undefined);
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
//If we remove the first path, it should now return 'black':
group.children[0].remove();
2013-04-08 03:19:47 -04:00
compareColors(group.fillColor, 'black', 'group.fillColor');
2011-02-16 16:11:26 -05:00
});
test('getting group styles 2', function() {
2013-06-11 16:44:42 -04:00
var star = new Path.Circle({
center: [100, 100],
2013-06-11 16:44:42 -04:00
radius: 40,
fillColor: 'red'
});
var circle = new Path.Circle({
center: [100, 100],
radius: 25,
strokeColor: 'black'
});
// Create a group of the two items and clip it:
var group = new Group(circle, star);
equals(function() {
return group.fillColor;
}, undefined);
});
2011-02-16 16:11:26 -05:00
test('setting group styles', function() {
var group = new Group();
var path = new Path();
path.fillColor = 'red';
group.addChild(path);
2011-02-16 16:11:26 -05:00
var secondPath = new Path();
secondPath.fillColor = 'blue';
secondPath.strokeColor = 'red';
group.addChild(secondPath);
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
// Change the fill color of the group:
group.fillColor = 'black';
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
// the paths contained in the group should now both have their fillColor
// set to black:
2013-04-08 03:19:47 -04:00
compareColors(path.fillColor, 'black', 'path.fillColor');
compareColors(secondPath.fillColor, 'black', 'secondPath.fillColor');
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
// The second path still has its strokeColor set to red:
2013-04-08 03:19:47 -04:00
compareColors(secondPath.strokeColor, 'red', 'secondPath.strokeColor');
2011-02-16 16:11:26 -05:00
});
test('setting group styles 2', function() {
var group = new Group();
var path = new Path();
path.strokeColor = 'red';
2011-02-16 16:11:26 -05:00
path.fillColor = 'red';
group.addChild(path);
2011-07-07 10:09:02 -04:00
2013-04-08 03:19:47 -04:00
compareColors(group.fillColor, 'red', 'group.fillColor');
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
var secondPath = new Path();
secondPath.fillColor = 'blue';
secondPath.strokeColor = 'red';
group.addChild(secondPath);
2011-07-07 10:09:02 -04:00
2013-04-08 03:19:47 -04:00
compareColors(secondPath.fillColor, 'blue', 'secondPath.fillColor');
compareColors(secondPath.strokeColor, 'red', 'secondPath.strokeColor');
2011-02-16 16:11:26 -05:00
// By appending a path with a different fillcolor,
// the group's fillColor should return undefined:
2013-06-11 16:53:48 -04:00
equals(group.fillColor, undefined, 'group.fillColor');
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
// But, both paths have a red strokeColor, so:
2013-04-08 03:19:47 -04:00
compareColors(group.strokeColor, 'red', 'group.strokeColor');
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
// Change the fill color of the group's style:
group.style.fillColor = 'black';
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
// the paths contained in the group should now both have their fillColor
// set to black:
2013-04-08 03:19:47 -04:00
compareColors(path.fillColor, 'black', 'path.fillColor');
compareColors(secondPath.fillColor, 'black', 'secondPath.fillColor');
2011-07-07 10:09:02 -04:00
2011-02-16 16:11:26 -05:00
// The second path still has its strokeColor set to red:
2013-04-08 03:19:47 -04:00
compareColors(secondPath.strokeColor, 'red', 'secondPath.strokeColor');
2011-02-16 16:11:26 -05:00
});