mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Move Item#setBounds() close to #getBounds() and document #_getBounds().
This commit is contained in:
parent
cca562efc0
commit
4c6e152748
1 changed files with 25 additions and 21 deletions
|
@ -1046,6 +1046,27 @@ var Item = this.Item = Base.extend(/** @lends Item# */{
|
|||
return this._getBounds('getBounds');
|
||||
},
|
||||
|
||||
setBounds: function(rect) {
|
||||
rect = Rectangle.read(arguments);
|
||||
var bounds = this.getBounds(),
|
||||
matrix = new Matrix(),
|
||||
center = rect.getCenter();
|
||||
// Read this from bottom to top:
|
||||
// Translate to new center:
|
||||
matrix.translate(center);
|
||||
// Scale to new Size, if size changes and avoid divisions by 0:
|
||||
if (rect.width != bounds.width || rect.height != bounds.height) {
|
||||
matrix.scale(
|
||||
bounds.width != 0 ? rect.width / bounds.width : 1,
|
||||
bounds.height != 0 ? rect.height / bounds.height : 1);
|
||||
}
|
||||
// Translate to center:
|
||||
center = bounds.getCenter();
|
||||
matrix.translate(-center.x, -center.y);
|
||||
// Now execute the transformation:
|
||||
this.transform(matrix);
|
||||
},
|
||||
|
||||
/**
|
||||
* The bounding rectangle of the item including stroke width.
|
||||
*
|
||||
|
@ -1056,6 +1077,10 @@ var Item = this.Item = Base.extend(/** @lends Item# */{
|
|||
return this._getBounds('getStrokeBounds');
|
||||
},
|
||||
|
||||
/**
|
||||
* Loops through all children, gets their bounds and finds the bounds around
|
||||
* all of them.
|
||||
*/
|
||||
_getBounds: function(getter) {
|
||||
var children = this._children;
|
||||
// TODO: What to return if nothing is defined, e.g. empty Groups?
|
||||
|
@ -1088,27 +1113,6 @@ var Item = this.Item = Base.extend(/** @lends Item# */{
|
|||
rect.x, rect.y, rect.width, rect.height);
|
||||
},
|
||||
|
||||
setBounds: function(rect) {
|
||||
rect = Rectangle.read(arguments);
|
||||
var bounds = this.getBounds(),
|
||||
matrix = new Matrix(),
|
||||
center = rect.getCenter();
|
||||
// Read this from bottom to top:
|
||||
// Translate to new center:
|
||||
matrix.translate(center);
|
||||
// Scale to new Size, if size changes and avoid divisions by 0:
|
||||
if (rect.width != bounds.width || rect.height != bounds.height) {
|
||||
matrix.scale(
|
||||
bounds.width != 0 ? rect.width / bounds.width : 1,
|
||||
bounds.height != 0 ? rect.height / bounds.height : 1);
|
||||
}
|
||||
// Translate to center:
|
||||
center = bounds.getCenter();
|
||||
matrix.translate(-center.x, -center.y);
|
||||
// Now execute the transformation:
|
||||
this.transform(matrix);
|
||||
},
|
||||
|
||||
/**
|
||||
* The bounding rectangle of the item including stroke width and controls.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue