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
|
|
|
*/
|
|
|
|
|
2011-03-04 08:34:31 -05:00
|
|
|
var PathStyle = this.PathStyle = Base.extend(new function() {
|
2011-05-22 21:41:20 -04:00
|
|
|
/** @lends PathStyle# */
|
|
|
|
|
|
|
|
// TODO: remove windingRule / resolution / fillOverprint / strokeOverprint?
|
2011-02-17 18:34:45 -05:00
|
|
|
var keys = ['windingRule', 'resolution', 'strokeColor', 'strokeWidth',
|
|
|
|
'strokeCap', 'strokeJoin', 'dashOffset','dashArray', 'miterLimit',
|
2011-05-07 08:39:17 -04:00
|
|
|
'strokeOverprint', 'fillColor', 'fillOverprint'],
|
|
|
|
strokeFlags = {
|
|
|
|
strokeWidth: true,
|
|
|
|
strokeCap: true,
|
|
|
|
strokeJoin: true,
|
|
|
|
miterLimit: true
|
|
|
|
};
|
2011-02-17 18:34:45 -05:00
|
|
|
|
|
|
|
var fields = {
|
|
|
|
beans: true,
|
2011-02-16 16:09:51 -05:00
|
|
|
|
2011-05-22 21:41:20 -04:00
|
|
|
// DOCS: why isn't the example code showing up?
|
|
|
|
/**
|
|
|
|
* Creates a new PathStyle object.
|
|
|
|
*
|
|
|
|
* @name PathStyle
|
|
|
|
* @constructor
|
|
|
|
* @param {object} style
|
|
|
|
*
|
|
|
|
* @class PathStyle is used for changing the visual styles of items
|
|
|
|
* contained within a Paper.js project and is returned by
|
2011-05-22 21:43:49 -04:00
|
|
|
* {@link Item#style} and {@link Project#currentStyle}.
|
2011-05-22 21:41:20 -04:00
|
|
|
*
|
|
|
|
* All properties of PathStyle are also reflected directly in {@link Item},
|
|
|
|
* i.e.: {@link Item#fillColor}.
|
|
|
|
*
|
|
|
|
* To set multiple style properties in one go, you can pass an object to
|
|
|
|
* {@link Item#style}. This is a convenient way to define a style once and
|
|
|
|
* apply it to a series of items:
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* var circleStyle = {
|
|
|
|
* fillColor: new RGBColor(1, 0, 0),
|
|
|
|
* strokeColor: new GrayColor(1),
|
|
|
|
* strokeWidth: 5
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* var path = new Path.Circle(new Point(50, 50), 50);
|
|
|
|
* path.style = circleStyle;
|
|
|
|
*/
|
2011-05-04 19:00:41 -04:00
|
|
|
initialize: function(style) {
|
2011-05-16 14:20:32 -04:00
|
|
|
// Note: This relies on bean setters that get implicetly
|
|
|
|
// called when setting values on this[key].
|
|
|
|
for (var i = 0, l = style && keys.length; i < l; i++) {
|
|
|
|
var key = keys[i],
|
|
|
|
value = style[key];
|
|
|
|
if (value !== undefined)
|
|
|
|
this[key] = value;
|
2011-02-16 16:09:51 -05:00
|
|
|
}
|
2011-05-04 19:00:41 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
statics: {
|
2011-05-16 14:21:36 -04:00
|
|
|
create: function(item) {
|
2011-05-04 19:00:41 -04:00
|
|
|
var style = new PathStyle(PathStyle.dont);
|
|
|
|
style._item = item;
|
|
|
|
return style;
|
|
|
|
}
|
2011-02-16 16:09:51 -05:00
|
|
|
}
|
2011-02-19 11:25:16 -05:00
|
|
|
};
|
2011-02-16 16:15:19 -05:00
|
|
|
|
2011-02-18 02:58:59 -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-02-17 18:34:45 -05:00
|
|
|
|
2011-03-04 19:23:16 -05:00
|
|
|
fields[set] = function(value) {
|
2011-05-14 13:07:10 -04:00
|
|
|
var children = this._item && this._item._children;
|
2011-05-05 06:16:26 -04:00
|
|
|
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);
|
2011-02-16 16:09:51 -05:00
|
|
|
} else {
|
2011-05-05 06:16:26 -04:00
|
|
|
var old = this['_' + key];
|
|
|
|
if (old != value && !(old && old.equals && old.equals(value))) {
|
2011-05-21 09:28:08 -04:00
|
|
|
this['_' + key] = value && value.clone ? value.clone() : value;
|
2011-05-07 08:39:17 -04:00
|
|
|
if (this._item) {
|
|
|
|
this._item._changed(ChangeFlags.STYLE
|
|
|
|
| (strokeFlags[key] ? ChangeFlags.STROKE : 0));
|
|
|
|
}
|
2011-05-05 06:16:26 -04:00
|
|
|
}
|
2011-02-16 16:09:51 -05:00
|
|
|
}
|
2011-03-06 09:44:54 -05:00
|
|
|
return this;
|
2011-02-16 16:09:51 -05:00
|
|
|
};
|
2011-02-17 18:34:45 -05:00
|
|
|
|
2011-03-04 19:23:16 -05:00
|
|
|
fields[get] = function() {
|
2011-05-14 13:07:10 -04:00
|
|
|
var children = this._item && this._item._children,
|
2011-05-04 19:05:36 -04:00
|
|
|
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;
|
2011-05-21 09:28:08 -04:00
|
|
|
} else if (style != childStyle && !(style && style.equals
|
|
|
|
&& style.equals(childStyle))) {
|
2011-05-04 19:05:36 -04:00
|
|
|
// If there is another item with a different style,
|
|
|
|
// the style is not defined:
|
2011-05-07 09:18:27 -04:00
|
|
|
// PORT: Change this in Sg (currently returns null)
|
2011-03-04 19:23:16 -05:00
|
|
|
return undefined;
|
2011-02-17 18:37:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return style;
|
2011-02-16 16:09:51 -05:00
|
|
|
} else {
|
|
|
|
return this['_' + key];
|
|
|
|
}
|
|
|
|
};
|
2011-02-16 16:15:19 -05:00
|
|
|
|
2011-05-21 09:28:08 -04:00
|
|
|
// Style-getters and setters for Item:
|
2011-05-16 13:29:53 -04:00
|
|
|
// 'this' = the Base.each() side-car = the object that is returned from
|
|
|
|
// Base.each and 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-02-16 16:09:51 -05:00
|
|
|
};
|
2011-02-17 18:34:45 -05:00
|
|
|
|
2011-03-04 19:23:16 -05:00
|
|
|
this[get] = function() {
|
2011-03-04 21:40:38 -05:00
|
|
|
return this._style[get]();
|
2011-02-16 16:09:51 -05:00
|
|
|
};
|
2011-02-18 02:58:59 -05:00
|
|
|
}, { beans: true }));
|
2011-02-17 18:34:45 -05:00
|
|
|
|
|
|
|
return fields;
|
2011-03-03 11:32:55 -05:00
|
|
|
});
|
2011-05-22 21:41:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@grouptitle Stroke Style}
|
|
|
|
*
|
|
|
|
* The color of the stroke.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Create a circle shaped path at { x: 50, y: 50 } with a radius of 10:
|
|
|
|
* var circle = new Path.Circle(new Point(50, 50), 10);
|
|
|
|
*
|
|
|
|
* // Set the stroke color of the circle to RGB red:
|
|
|
|
* circle.strokeColor = new RGB(1, 0, 0);
|
|
|
|
*
|
|
|
|
* @property
|
|
|
|
* @name PathStyle#strokeColor
|
|
|
|
* @type RGBColor|HSBColor|GrayColor
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The width of the stroke.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Create a circle shaped path at { x: 50, y: 50 } with a radius of 10:
|
|
|
|
* var circle = new Path.Circle(new Point(50, 50), 10);
|
|
|
|
*
|
|
|
|
* // Set the stroke width of the circle to 3pt:
|
|
|
|
* circle.strokeWidth = 3;
|
|
|
|
*
|
|
|
|
* @property
|
|
|
|
* @name PathStyle#strokeWidth
|
|
|
|
* @type number
|
|
|
|
*/
|
|
|
|
|
|
|
|
// DOCS: PathStyle#strokeCap: describe the different options
|
|
|
|
/**
|
|
|
|
* The cap of the stroke.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Create a line from { x: 0, y: 50 } to { x: 50, y: 50 };
|
|
|
|
* var line = new Path.Line(new Point(0, 50), new Point(50, 50));
|
|
|
|
*
|
|
|
|
* // Set the stroke cap of the line to be round:
|
|
|
|
* line.strokeCap = 'round';
|
|
|
|
*
|
|
|
|
* @property
|
|
|
|
* @name PathStyle#strokeCap
|
|
|
|
* @type string
|
|
|
|
*/
|
|
|
|
|
|
|
|
// DOCS: PathStyle#strokeJoin: describe the different options
|
|
|
|
/**
|
|
|
|
* The join of the stroke.
|
|
|
|
*
|
|
|
|
* @property
|
|
|
|
* @name PathStyle#strokeJoin
|
|
|
|
* @type string
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The dash offset of the stroke.
|
|
|
|
*
|
|
|
|
* @property
|
|
|
|
* @name PathStyle#dashOffset
|
|
|
|
* @type string
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specifies an array containing the dash and gap lengths of the stroke.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Create a line from { x: 0, y: 50 } to { x: 50, y: 50 };
|
|
|
|
* var line = new Path.Line(new Point(0, 50), new Point(50, 50));
|
|
|
|
*
|
|
|
|
* line.strokeWidth = 3;
|
|
|
|
*
|
|
|
|
* // Set the dashed stroke to [10pt dash, 5pt gap, 8pt dash, 10pt gap]:
|
|
|
|
* line.dashArray = [10, 5, 8, 10];
|
|
|
|
*
|
|
|
|
* @property
|
|
|
|
* @name PathStyle#dashArray
|
|
|
|
* @type array
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The miter limit of the stroke.
|
|
|
|
*
|
|
|
|
* @property
|
|
|
|
* @name PathStyle#miterLimit
|
|
|
|
* @type number
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@grouptitle Fill Style}
|
|
|
|
*
|
|
|
|
* The fill color.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Create a circle shaped path at { x: 50, y: 50 } with a radius of 10:
|
|
|
|
* var circle = new Path.Circle(new Point(50, 50), 10);
|
|
|
|
*
|
|
|
|
* // Set the fill color of the circle to RGB red:
|
|
|
|
* circle.fillColor = new RGBColor(1, 0, 0, );
|
|
|
|
*
|
|
|
|
* @property
|
|
|
|
* @name PathStyle#fillColor
|
|
|
|
* @type RGBColor|HSBColor|GrayColor
|
|
|
|
*/
|