From ded73142a91705720fbab6b32629febe857d41be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Tue, 25 Dec 2012 17:57:04 +0100 Subject: [PATCH] Simplify pointOrMatrix parameters and only allow points. Also remove internal _point cache from PointText and directly link #point to #matrix. --- src/item/Item.js | 10 ++++------ src/item/PlacedSymbol.js | 8 +++----- src/item/Raster.js | 4 ++-- src/text/PointText.js | 19 ++++++------------- src/text/TextItem.js | 4 ++-- 5 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/item/Item.js b/src/item/Item.js index ff06e99c..f58a259a 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -95,7 +95,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{ ); }, - initialize: function(pointOrMatrix) { + initialize: function(point) { // Define this Item's unique id. this._id = ++Item._id; // If _project is already set, the item was already moved into the DOM @@ -106,11 +106,9 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{ if (!this._style) this._style = PathStyle.create(this); this.setStyle(this._project.getCurrentStyle()); - this._matrix = pointOrMatrix !== undefined - ? pointOrMatrix instanceof Matrix - ? pointOrMatrix.clone() - : new Matrix().translate(Point.read(arguments)) - : new Matrix(); + this._matrix = new Matrix(); + if (point) + this._matrix.translate(point); }, /** diff --git a/src/item/PlacedSymbol.js b/src/item/PlacedSymbol.js index bc617689..f9b3f7aa 100644 --- a/src/item/PlacedSymbol.js +++ b/src/item/PlacedSymbol.js @@ -28,9 +28,7 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol * Creates a new PlacedSymbol Item. * * @param {Symbol} symbol the symbol to place - * @param {Point|Matrix} [pointOrMatrix] the center point of the placed - * symbol or a {@link Matrix} transformation to transform the placed symbol - * with. + * @param {Point} [point] the center point of the placed symbol * * @example {@paperscript split=true height=240} * // Placing 100 instances of a symbol: @@ -62,8 +60,8 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol * instance.scale(0.25 + Math.random() * 0.75); * } */ - initialize: function(symbol, pointOrMatrix) { - this.base(pointOrMatrix); + initialize: function(symbol, point) { + this.base(Point.read(arguments, 1)); this.setSymbol(symbol instanceof Symbol ? symbol : new Symbol(symbol)); }, diff --git a/src/item/Raster.js b/src/item/Raster.js index c6af4360..e5f6c7a8 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -35,8 +35,8 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{ * * @param {HTMLImageElement|Canvas|string} [object] */ - initialize: function(object, pointOrMatrix) { - this.base(pointOrMatrix, arguments); + initialize: function(object, point) { + this.base(Point.read(arguments, 1)); if (object.getContext) { this.setCanvas(object); } else if (typeof object === 'string') { diff --git a/src/text/PointText.js b/src/text/PointText.js index 7bec834d..c68c3e2a 100644 --- a/src/text/PointText.js +++ b/src/text/PointText.js @@ -28,6 +28,7 @@ var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{ /** * Creates a point text item * + * @name PointText#initialize * @param {Point} point the position where the text will start * * @example @@ -36,13 +37,9 @@ var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{ * text.fillColor = 'black'; * text.content = 'The contents of the point text'; */ - initialize: function(pointOrMatrix) { - this.base(pointOrMatrix); - this._point = this._matrix.getTranslation(); - }, clone: function() { - return this._clone(new PointText(this._matrix)); + return this._clone(new PointText()); }, /** @@ -54,17 +51,13 @@ var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{ getPoint: function() { // Se Item#getPosition for an explanation why we create new LinkedPoint // objects each time. - return LinkedPoint.create(this, 'setPoint', - this._point.x, this._point.y); + var point = this._matrix.getTranslation(); + return LinkedPoint.create(this, 'setPoint', point.x, point.y); }, setPoint: function(point) { - this.translate(Point.read(arguments).subtract(this._point)); - }, - - _transform: function(matrix) { - // Transform _point: - matrix._transformPoint(this._point, this._point); + this.translate(Point.read(arguments).subtract( + this._matrix.getTranslation())); }, draw: function(ctx) { diff --git a/src/text/TextItem.js b/src/text/TextItem.js index d9eb404a..60164650 100644 --- a/src/text/TextItem.js +++ b/src/text/TextItem.js @@ -30,13 +30,13 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{ // so use the same name for all of them _boundsGetter: 'getBounds', - initialize: function(pointOrMatrix) { + initialize: function(point) { // Note that internally #characterStyle is the same as #style, but // defined as an instance of CharacterStyle. We need to define it before // calling this.base(), to override the default PathStyle instance. this._style = CharacterStyle.create(this); this._paragraphStyle = ParagraphStyle.create(this); - this.base(pointOrMatrix); + this.base(Point.read(arguments)); // No need to call setStyle(), since base() handles this already. // Call with no parameter to initalize defaults now. this.setParagraphStyle();