paper.js/src/item/PathStyle.js

92 lines
2.2 KiB
JavaScript
Raw Normal View History

2011-03-06 19:50:44 -05:00
/*
* Paper.js
*
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
* based on Scriptographer.org and designed to be largely API compatible.
2011-03-07 20:41:50 -05:00
* http://paperjs.org/
2011-03-06 19:50:44 -05:00
* http://scriptographer.org/
*
2011-03-07 20:41:50 -05:00
* Distributed under the MIT license. See LICENSE file for details.
*
2011-03-06 19:50:44 -05:00
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
2011-03-07 20:41:50 -05:00
* All rights reserved.
2011-03-06 19:50:44 -05:00
*/
var PathStyle = this.PathStyle = Base.extend(new function() {
var keys = ['windingRule', 'resolution', 'strokeColor', 'strokeWidth',
'strokeCap', 'strokeJoin', 'dashOffset','dashArray', 'miterLimit',
'strokeOverprint', 'fillColor', 'fillOverprint'];
var fields = {
beans: true,
initialize: function(style) {
2011-02-20 21:32:39 -05:00
if (style) {
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i],
value = style[key];
if (value !== undefined)
this[key] = value;
}
}
},
statics: {
create: function(item, other) {
var style = new PathStyle(PathStyle.dont);
style._item = item;
style.initialize(other);
return style;
}
}
};
2011-02-16 16:15:19 -05:00
Item.inject(Base.each(keys, function(key) {
2011-03-04 19:23:16 -05:00
var isColor = !!(key.match(/Color$/)),
set = 'set' + Base.capitalize(key),
get = 'get' + Base.capitalize(key);
2011-03-04 19:23:16 -05:00
fields[set] = function(value) {
if (this._item && this._item.children) {
for (var i = 0, l = this._item.children.length; i < l; i++) {
this._item.children[i]._style[set](value);
}
} else {
this['_' + key] = isColor ? Color.read(arguments) : value;
}
2011-03-06 09:44:54 -05:00
return this;
};
2011-03-04 19:23:16 -05:00
fields[get] = function() {
if (this._item && this._item.children) {
var style;
for (var i = 0, l = this._item.children.length; i < l; i++) {
var childStyle = this._item.children[i]._style[get]();
2011-02-20 21:32:39 -05:00
if (!style) {
2011-03-04 19:24:32 -05:00
style = childStyle;
} else if (style != childStyle) {
// If there is another item with a different style:
2011-03-04 19:23:16 -05:00
return undefined;
}
}
return style;
} else {
return this['_' + key];
}
};
2011-02-16 16:15:19 -05:00
2011-03-04 19:23:16 -05:00
this[set] = function(value) {
2011-03-04 21:40:38 -05:00
this._style[set](value);
2011-03-06 09:44:54 -05:00
return this;
};
2011-03-04 19:23:16 -05:00
this[get] = function() {
2011-03-04 21:40:38 -05:00
return this._style[get]();
};
}, { beans: true }));
return fields;
2011-03-03 11:32:55 -05:00
});