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.
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);
},
/**

View file

@ -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));
},

View file

@ -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') {

View file

@ -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) {

View file

@ -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();