Simplify pointOrMatrix parameters and only allow points. Also remove internal _point cache from PointText and directly link #point to #matrix.

This commit is contained in:
Jürg Lehni 2012-12-25 17:57:04 +01:00
parent 623bed4c4a
commit ded73142a9
5 changed files with 17 additions and 28 deletions

View file

@ -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. // Define this Item's unique id.
this._id = ++Item._id; this._id = ++Item._id;
// If _project is already set, the item was already moved into the DOM // 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) if (!this._style)
this._style = PathStyle.create(this); this._style = PathStyle.create(this);
this.setStyle(this._project.getCurrentStyle()); this.setStyle(this._project.getCurrentStyle());
this._matrix = pointOrMatrix !== undefined this._matrix = new Matrix();
? pointOrMatrix instanceof Matrix if (point)
? pointOrMatrix.clone() this._matrix.translate(point);
: new Matrix().translate(Point.read(arguments))
: new Matrix();
}, },
/** /**

View file

@ -28,9 +28,7 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol
* Creates a new PlacedSymbol Item. * Creates a new PlacedSymbol Item.
* *
* @param {Symbol} symbol the symbol to place * @param {Symbol} symbol the symbol to place
* @param {Point|Matrix} [pointOrMatrix] the center point of the placed * @param {Point} [point] the center point of the placed symbol
* symbol or a {@link Matrix} transformation to transform the placed symbol
* with.
* *
* @example {@paperscript split=true height=240} * @example {@paperscript split=true height=240}
* // Placing 100 instances of a symbol: * // 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); * instance.scale(0.25 + Math.random() * 0.75);
* } * }
*/ */
initialize: function(symbol, pointOrMatrix) { initialize: function(symbol, point) {
this.base(pointOrMatrix); this.base(Point.read(arguments, 1));
this.setSymbol(symbol instanceof Symbol ? symbol : new Symbol(symbol)); this.setSymbol(symbol instanceof Symbol ? symbol : new Symbol(symbol));
}, },

View file

@ -35,8 +35,8 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
* *
* @param {HTMLImageElement|Canvas|string} [object] * @param {HTMLImageElement|Canvas|string} [object]
*/ */
initialize: function(object, pointOrMatrix) { initialize: function(object, point) {
this.base(pointOrMatrix, arguments); this.base(Point.read(arguments, 1));
if (object.getContext) { if (object.getContext) {
this.setCanvas(object); this.setCanvas(object);
} else if (typeof object === 'string') { } else if (typeof object === 'string') {

View file

@ -28,6 +28,7 @@ var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{
/** /**
* Creates a point text item * Creates a point text item
* *
* @name PointText#initialize
* @param {Point} point the position where the text will start * @param {Point} point the position where the text will start
* *
* @example * @example
@ -36,13 +37,9 @@ var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{
* text.fillColor = 'black'; * text.fillColor = 'black';
* text.content = 'The contents of the point text'; * text.content = 'The contents of the point text';
*/ */
initialize: function(pointOrMatrix) {
this.base(pointOrMatrix);
this._point = this._matrix.getTranslation();
},
clone: function() { 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() { getPoint: function() {
// Se Item#getPosition for an explanation why we create new LinkedPoint // Se Item#getPosition for an explanation why we create new LinkedPoint
// objects each time. // objects each time.
return LinkedPoint.create(this, 'setPoint', var point = this._matrix.getTranslation();
this._point.x, this._point.y); return LinkedPoint.create(this, 'setPoint', point.x, point.y);
}, },
setPoint: function(point) { setPoint: function(point) {
this.translate(Point.read(arguments).subtract(this._point)); this.translate(Point.read(arguments).subtract(
}, this._matrix.getTranslation()));
_transform: function(matrix) {
// Transform _point:
matrix._transformPoint(this._point, this._point);
}, },
draw: function(ctx) { draw: function(ctx) {

View file

@ -30,13 +30,13 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
// so use the same name for all of them // so use the same name for all of them
_boundsGetter: 'getBounds', _boundsGetter: 'getBounds',
initialize: function(pointOrMatrix) { initialize: function(point) {
// Note that internally #characterStyle is the same as #style, but // Note that internally #characterStyle is the same as #style, but
// defined as an instance of CharacterStyle. We need to define it before // defined as an instance of CharacterStyle. We need to define it before
// calling this.base(), to override the default PathStyle instance. // calling this.base(), to override the default PathStyle instance.
this._style = CharacterStyle.create(this); this._style = CharacterStyle.create(this);
this._paragraphStyle = ParagraphStyle.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. // No need to call setStyle(), since base() handles this already.
// Call with no parameter to initalize defaults now. // Call with no parameter to initalize defaults now.
this.setParagraphStyle(); this.setParagraphStyle();