Merge remote branch 'origin/master'

This commit is contained in:
Jonathan Puckey 2011-05-07 13:56:03 +01:00
commit ba6139cd48
16 changed files with 71 additions and 45 deletions

View file

@ -98,7 +98,7 @@ var Document = this.Document = Base.extend({
}, },
setCurrentStyle: function(style) { setCurrentStyle: function(style) {
this._currentStyle = PathStyle.create(this, style); this._currentStyle = PathStyle.create(null, style);
}, },
activate: function() { activate: function() {

21
src/item/ChangeFlags.js Normal file
View file

@ -0,0 +1,21 @@
/*
* 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.
* http://paperjs.org/
* http://scriptographer.org/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* All rights reserved.
*/
var ChangeFlags = {
PATH: 1, // Path geometry
STROKE: 2, // Stroke geometry
STYLE: 4 // Fille style or stroke color / dash
};

View file

@ -22,6 +22,12 @@ var Item = this.Item = Base.extend({
this.setStyle(this._document.getCurrentStyle()); this.setStyle(this._document.getCurrentStyle());
}, },
_changed: function(flags) {
if (flags & ChangeFlags.PATH) {
delete this._position;
}
},
/** /**
* When passed a document, copies the item to the document, * When passed a document, copies the item to the document,
* or duplicates it within the same document. When passed an item, * or duplicates it within the same document. When passed an item,
@ -542,14 +548,16 @@ var Item = this.Item = Base.extend({
* </code> * </code>
*/ */
getPosition: function() { getPosition: function() {
return this.getBounds().getCenter(); // Cache position value
if (!this._position)
this._position = this.getBounds().getCenter();
return this._position.clone();
}, },
setPosition: function(point) { setPosition: function(point) {
point = Point.read(arguments); point = Point.read(arguments);
if (point) { if (point)
this.translate(point.subtract(this.getPosition())); this.translate(point.subtract(this.getPosition()));
}
}, },
/** /**
@ -563,6 +571,9 @@ var Item = this.Item = Base.extend({
// TODO: Call transform on chidren only if 'children' flag is provided // TODO: Call transform on chidren only if 'children' flag is provided
if (this._transform) if (this._transform)
this._transform(matrix, flags); this._transform(matrix, flags);
// Transform position as well
if (this._position)
this._position = matrix._transformPoint(this._position);
if (this.children) { if (this.children) {
for (var i = 0, l = this.children.length; i < l; i++) { for (var i = 0, l = this.children.length; i < l; i++) {
var child = this.children[i]; var child = this.children[i];
@ -602,8 +613,7 @@ var Item = this.Item = Base.extend({
*/ */
translate: function(delta) { translate: function(delta) {
var mx = new Matrix(); var mx = new Matrix();
mx.translate.apply(mx, arguments); return this.transform(mx.translate.apply(mx, arguments));
return this.transform(mx);
}, },
/** /**

View file

@ -17,7 +17,13 @@
var PathStyle = this.PathStyle = Base.extend(new function() { var PathStyle = this.PathStyle = Base.extend(new function() {
var keys = ['windingRule', 'resolution', 'strokeColor', 'strokeWidth', var keys = ['windingRule', 'resolution', 'strokeColor', 'strokeWidth',
'strokeCap', 'strokeJoin', 'dashOffset','dashArray', 'miterLimit', 'strokeCap', 'strokeJoin', 'dashOffset','dashArray', 'miterLimit',
'strokeOverprint', 'fillColor', 'fillOverprint']; 'strokeOverprint', 'fillColor', 'fillOverprint'],
strokeFlags = {
strokeWidth: true,
strokeCap: true,
strokeJoin: true,
miterLimit: true
};
var fields = { var fields = {
beans: true, beans: true,
@ -63,10 +69,10 @@ var PathStyle = this.PathStyle = Base.extend(new function() {
var old = this['_' + key]; var old = this['_' + key];
if (old != value && !(old && old.equals && old.equals(value))) { if (old != value && !(old && old.equals && old.equals(value))) {
this['_' + key] = value; this['_' + key] = value;
// TODO: Tell _item what exactly has changed. Maybe introduce if (this._item) {
// ChangeFlags, e.g. STROKE, COLOR, FILL, GEOMETRY, etc? this._item._changed(ChangeFlags.STYLE
if (this._item && this._item._changed) | (strokeFlags[key] ? ChangeFlags.STROKE : 0));
this._item._changed(); }
} }
} }
return this; return this;

View file

@ -19,22 +19,12 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend({
initialize: function(symbol, matrixOrOffset) { initialize: function(symbol, matrixOrOffset) {
this.base(); this.base();
if (symbol instanceof Symbol) { this.symbol = symbol instanceof Symbol ? symbol : new Symbol(symbol);
this.symbol = symbol; this.matrix = matrixOrOffset !== undefined
} else { ? matrixOrOffset instanceof Matrix
this.symbol = new Symbol(symbol); ? matrixOrOffset
} : new Matrix().translate(Point.read(arguments, 1))
this._position = this.symbol._definition.getPosition(); : new Matrix();
if (matrixOrOffset !== undefined) {
if (matrixOrOffset instanceof Matrix) {
this.matrix = matrixOrOffset;
} else {
this.matrix = new Matrix().translate(Point.read(arguments, 1));
}
this._position = this.matrix._transformPoint(this._position);
} else {
this.matrix = new Matrix();
}
}, },
_transform: function(matrix, flags) { _transform: function(matrix, flags) {
@ -42,12 +32,6 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend({
// raster, simply preconcatenate the internal matrix with the provided // raster, simply preconcatenate the internal matrix with the provided
// one. // one.
this.matrix.preConcatenate(matrix); this.matrix.preConcatenate(matrix);
// Transform position as well
this._position = matrix._transformPoint(this._position);
},
getPosition: function() {
return this._position.clone();
}, },
getBounds: function() { getBounds: function() {

View file

@ -36,6 +36,7 @@ var sources = [
'src/document/Document.js', 'src/document/Document.js',
'src/document/Symbol.js', 'src/document/Symbol.js',
'src/item/ChangeFlags.js',
'src/item/Item.js', 'src/item/Item.js',
'src/item/Group.js', 'src/item/Group.js',
'src/item/Layer.js', 'src/item/Layer.js',

View file

@ -100,6 +100,7 @@ Base.inject({
//#include "document/Document.js" //#include "document/Document.js"
//#include "document/Symbol.js" //#include "document/Symbol.js"
//#include "item/ChangeFlags.js"
//#include "item/Item.js" //#include "item/Item.js"
//#include "item/Group.js" //#include "item/Group.js"
//#include "item/Layer.js" //#include "item/Layer.js"

View file

@ -28,11 +28,15 @@ var Path = this.Path = PathItem.extend({
|| typeof segments[0] !== 'object' ? arguments : segments); || typeof segments[0] !== 'object' ? arguments : segments);
}, },
_changed: function() { _changed: function(flags) {
// Clear cached values. if (flags & ChangeFlags.PATH) {
delete this._length; delete this._length;
delete this._bounds; delete this._bounds;
delete this._strokeBounds; delete this._position;
delete this._strokeBounds;
} else if (flags & ChangeFlags.STROKE) {
delete this._strokeBounds;
}
}, },
/** /**
@ -112,7 +116,7 @@ var Path = this.Path = PathItem.extend({
this._curves[i = length - 1] = Curve.create(this, this._curves[i = length - 1] = Curve.create(this,
this._segments[i], this._segments[0]); this._segments[i], this._segments[0]);
} }
this._changed(); this._changed(ChangeFlags.PATH);
} }
}, },
@ -127,7 +131,7 @@ var Path = this.Path = PathItem.extend({
this._segments[i]._transformCoordinates(matrix, coords, true); this._segments[i]._transformCoordinates(matrix, coords, true);
} }
} }
this._changed(); this._changed(ChangeFlags.PATH);
}, },
/** /**
@ -175,7 +179,7 @@ var Path = this.Path = PathItem.extend({
if (curve) if (curve)
curve._segment1 = segments[index + amount]; curve._segment1 = segments[index + amount];
} }
this._changed(); this._changed(ChangeFlags.PATH);
return segs; return segs;
}, },
@ -259,7 +263,7 @@ var Path = this.Path = PathItem.extend({
if (last && this._closed && (curve = curves[curves.length - 1])) if (last && this._closed && (curve = curves[curves.length - 1]))
curve._segment2 = segments[0]; curve._segment2 = segments[0];
} }
this._changed(); this._changed(ChangeFlags.PATH);
return removed; return removed;
}, },
@ -304,7 +308,6 @@ var Path = this.Path = PathItem.extend({
segment._handleIn = segment._handleOut; segment._handleIn = segment._handleOut;
segment._handleOut = handleIn; segment._handleOut = handleIn;
} }
this._changed();
}, },
join: function(path) { join: function(path) {
@ -339,7 +342,7 @@ var Path = this.Path = PathItem.extend({
last1.remove(); last1.remove();
this.setClosed(true); this.setClosed(true);
} }
this._changed(); this._changed(ChangeFlags.PATH);
return true; return true;
} }
return false; return false;

View file

@ -69,7 +69,7 @@ var Segment = this.Segment = Base.extend({
} }
} }
} }
this._path._changed(); this._path._changed(ChangeFlags.PATH);
} }
}, },