Skip to content

Commit 61dca0b

Browse files
committed
Introduce standard way of passing matrices/points to Item constructors.
1 parent a21d180 commit 61dca0b

5 files changed

Lines changed: 18 additions & 22 deletions

File tree

src/item/Item.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
101101
});
102102
},
103103

104-
initialize: function() {
104+
initialize: function(pointOrMatrix) {
105105
// Define this Item's unique id.
106106
this._id = ++Item._id;
107107
// If _project is already set, the item was already moved into the DOM
@@ -112,7 +112,11 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
112112
if (!this._style)
113113
this._style = PathStyle.create(this);
114114
this.setStyle(this._project.getCurrentStyle());
115-
this._matrix = new Matrix();
115+
this._matrix = pointOrMatrix !== undefined
116+
? pointOrMatrix instanceof Matrix
117+
? pointOrMatrix.clone()
118+
: new Matrix().translate(Point.read(arguments, 0))
119+
: new Matrix();
116120
},
117121

118122
/**
@@ -1863,7 +1867,7 @@ function(name) {
18631867
// Set _matrix to the identity
18641868
this._matrix.setIdentity();
18651869
// TODO: This needs a _changed notification, but the GEOMETRY
1866-
// actually sdoesnt change! What to do?
1870+
// actually doesn't change! What to do?
18671871
}
18681872
},
18691873

src/item/PlacedSymbol.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol
2727
* Creates a new PlacedSymbol Item.
2828
*
2929
* @param {Symbol} symbol the symbol to place
30-
* @param {Point|Matrix} [matrixOrOffset] the center point of the placed
30+
* @param {Point|Matrix} [pointOrMatrix] the center point of the placed
3131
* symbol or a {@link Matrix} transformation to transform the placed symbol
3232
* with.
3333
*
@@ -61,15 +61,9 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol
6161
* instance.scale(0.25 + Math.random() * 0.75);
6262
* }
6363
*/
64-
initialize: function(symbol, matrixOrOffset) {
65-
this.base();
64+
initialize: function(symbol, pointOrMatrix) {
65+
this.base(pointOrMatrix);
6666
this.setSymbol(symbol instanceof Symbol ? symbol : new Symbol(symbol));
67-
// XXX: Define one way of creating matrices and passing them to ctors
68-
this._matrix = matrixOrOffset !== undefined
69-
? matrixOrOffset instanceof Matrix
70-
? matrixOrOffset
71-
: new Matrix().translate(Point.read(arguments, 1))
72-
: new Matrix();
7367
},
7468

7569
/**

src/item/Raster.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
3434
*
3535
* @param {HTMLImageElement|Canvas|string} [object]
3636
*/
37-
initialize: function(object) {
38-
this.base();
37+
initialize: function(object, pointOrMatrix) {
38+
this.base(pointOrMatrix);
3939
if (object.getContext) {
4040
this.setCanvas(object);
4141
} else {

src/text/PointText.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@ var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{
3535
* text.fillColor = 'black';
3636
* text.content = 'The contents of the point text';
3737
*/
38-
initialize: function(point) {
39-
this.base();
40-
this._point = Point.read(arguments).clone();
41-
// XXX: Define one way of creating matrices and passing them to ctors
42-
this._matrix = new Matrix().translate(this._point);
38+
initialize: function(pointOrMatrix) {
39+
this.base(pointOrMatrix);
40+
this._point = this._matrix.getTranslation();
4341
},
4442

4543
clone: function() {
46-
return this._clone(new PointText(this._point));
44+
return this._clone(new PointText(this._matrix));
4745
},
4846

4947
/**

src/text/TextItem.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
3030
// so use the same name for all of them
3131
_boundsType: 'bounds',
3232

33-
initialize: function() {
33+
initialize: function(pointOrMatrix) {
3434
// Note that internally #characterStyle is the same as #style, but
3535
// defined as an instance of CharacterStyle. We need to define it before
3636
// calling this.base(), to override the default PathStyle instance.
3737
this._style = CharacterStyle.create(this);
3838
this._paragraphStyle = ParagraphStyle.create(this);
39-
this.base();
39+
this.base(pointOrMatrix);
4040
// No need to call setStyle(), since base() handles this already.
4141
// Call with no parameter to initalize defaults now.
4242
this.setParagraphStyle();

0 commit comments

Comments
 (0)