mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-19 14:10:14 -05:00
Find an elegant way to implement all variations of Rectangle(object) constructors.
This commit is contained in:
parent
7c5478cd93
commit
1394c36907
1 changed files with 29 additions and 26 deletions
|
@ -259,19 +259,21 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
|
||||
setSize: function(size) {
|
||||
size = Size.read(arguments);
|
||||
this._fixed = this._fixed || {};
|
||||
if (this._fixed.centerX)
|
||||
this.x += (this.width - size.width) / 2;
|
||||
else if (this._fixed.right)
|
||||
this.x += this.width - size.width;
|
||||
if (this._fixed.centerY)
|
||||
this.y += (this.height - size.height) / 2;
|
||||
else if (this._fixed.bottom)
|
||||
this.y += this.height - size.height;
|
||||
// Keep track of how dimensions were specified through this._fix*
|
||||
// attributes.
|
||||
// _fixX / Y can either be 0 (l), 0.5 (center) or 1 (r), and is used as
|
||||
// direct factors to calculate the x / y adujstments from the size
|
||||
// differences.
|
||||
// _fixW / H is either 0 (off) or 1 (on), and is used to protect
|
||||
// widht / height values against changes.
|
||||
if (this._fixX)
|
||||
this.x += (this.width - size.width) * this._fixX;
|
||||
if (this._fixY)
|
||||
this.y += (this.height - size.height) * this._fixY;
|
||||
this.width = size.width;
|
||||
this.height = size.height;
|
||||
this._fixed.width = true;
|
||||
this._fixed.height = true;
|
||||
this._fixW = 1;
|
||||
this._fixH = 1;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -288,11 +290,10 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
},
|
||||
|
||||
setLeft: function(left) {
|
||||
this._fixed = this._fixed || {};
|
||||
if (!this._fixed.width)
|
||||
if (!this._fixW)
|
||||
this.width -= left - this.x;
|
||||
this.x = left;
|
||||
this._fixed.left = true;
|
||||
this._fixX = 0;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -307,11 +308,10 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
},
|
||||
|
||||
setTop: function(top) {
|
||||
this._fixed = this._fixed || {};
|
||||
if (!this._fixed.height)
|
||||
if (!this._fixH)
|
||||
this.height -= top - this.y;
|
||||
this.y = top;
|
||||
this._fixed.top = true;
|
||||
this._fixY = 0;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -326,12 +326,14 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
},
|
||||
|
||||
setRight: function(right) {
|
||||
this._fixed = this._fixed || {};
|
||||
if (this._fixed.width)
|
||||
// Turn _fixW off if we specify two _fixX values
|
||||
if (this._fixX !== undefined && this._fixX !== 1)
|
||||
this._fixW = 0;
|
||||
if (this._fixW)
|
||||
this.x = right - this.width;
|
||||
else
|
||||
this.width = right - this.x;
|
||||
this._fixed.right = true;
|
||||
this._fixX = 1;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -346,11 +348,14 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
},
|
||||
|
||||
setBottom: function(bottom) {
|
||||
if (this._fixed && this._fixed.height)
|
||||
// Turn _fixH off if we specify two _fixY values
|
||||
if (this._fixY !== undefined && this._fixY !== 1)
|
||||
this._fixH = 0;
|
||||
if (this._fixH)
|
||||
this.y = bottom - this.height;
|
||||
else
|
||||
this.height = bottom - this.y;
|
||||
this._fixed.bottom = true;
|
||||
this._fixY = 1;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -365,9 +370,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
},
|
||||
|
||||
setCenterX: function(x) {
|
||||
this._fixed = this._fixed || {};
|
||||
this._fixed.centerX = true;
|
||||
this.x = x - this.width * 0.5;
|
||||
this._fixX = 0.5;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -382,9 +386,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
},
|
||||
|
||||
setCenterY: function(y) {
|
||||
this._fixed = this._fixed || {};
|
||||
this._fixed.centerY = true;
|
||||
this.y = y - this.height * 0.5;
|
||||
this._fixY = 0.5;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue