paper.js/src/item/PathStyle.js

112 lines
2.8 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;
}
}
},
2011-05-04 19:01:17 -04:00
clone: function() {
return new PathStyle(this);
},
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-05-04 19:08:55 -04:00
var isColor = !!key.match(/Color$/),
part = Base.capitalize(key),
set = 'set' + part,
get = 'get' + part;
2011-03-04 19:23:16 -05:00
fields[set] = function(value) {
2011-05-04 19:08:55 -04:00
var children = this._item && this._item.children;
value = isColor ? Color.read(arguments) : value;
2011-05-04 19:08:55 -04:00
if (children) {
for (var i = 0, l = children.length; i < l; i++)
children[i]._style[set](value);
} else {
var old = this['_' + key];
if (old != value && !(old && old.equals && old.equals(value))) {
this['_' + key] = value;
// TODO: Tell _item what exactly has changed. Maybe introduce
// ChangeFlags, e.g. STROKE, COLOR, FILL, GEOMETRY, etc?
if (this._item && this._item._changed)
this._item._changed();
}
}
2011-03-06 09:44:54 -05:00
return this;
};
2011-03-04 19:23:16 -05:00
fields[get] = function() {
2011-05-04 19:05:36 -04:00
var children = this._item && this._item.children,
style;
// If this item has children, walk through all of them and see if
// they all have the same style.
if (children) {
for (var i = 0, l = children.length; i < l; i++) {
var childStyle = 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) {
2011-05-04 19:05:36 -04:00
// If there is another item with a different style,
// the style is not defined:
// TODO: Port back to Sg (currently returns null)
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-05-04 19:05:36 -04:00
// 'this' = the Base.each() side-car = the object that is injected into
// Item above:
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
});