Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Add centralized coordinate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelerz committed Jun 9, 2015
1 parent 0a807a6 commit fd1b892
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 41 deletions.
51 changes: 51 additions & 0 deletions lib/configuration/atoms/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,57 @@ var Rect = Base.extend(
return false;
}
return true;
},

/**
* Limits the rect coordinates to a specified rect
*
* Note:
* Priority is on the x/y coordinates, and not on the size since the size will then be removed anyways.
*
* @method limitCoordinates
* @param {Rect} outerRect Outer rect
*/
limitCoordinates: function (outerRect) {

var outerRectCoord = outerRect.getRect();

// Set values if none given
this._left = this._left || outerRectCoord.left;
rect._top = rect.y || outerRectCoord.top;
rect._width = rect.width || outerRectCoord.width;
rect._height = rect.height || outerRectCoord.height;

// Check negative values
rect._left = Math.max(0, rect._left);
rect._top = Math.max(0, rect._top);
rect._width = Math.max(1, rect._width);
rect._height = Math.max(1, rect._height);

// Check dimensions
rect._left = Math.min(rect.x, outerRectCoord.width - 1); // -1 to make sure that there is an image
rect._top = Math.min(rect.y, outerRectCoord.height - 1);
rect._width = Math.min(rect.width, outerRectCoord.width - this._left);
rect._height = Math.min(rect.height, outerRectCoord.height - this._top);

// Make sure that the rect is at least one pixel by one pixel
rect._width = Math.max(1, rect._width);
rect._height = Math.max(1, rect._height);
},

/**
* Clones the rect
*
* @method clone
* @return {Rect}
*/
clone: function () {
return new Rect({
left: this.getLeft(),
top: this.getTop(),
width: this.getWidth(),
height: this.getHeight()
});
}
},

Expand Down
53 changes: 12 additions & 41 deletions lib/configuration/blockOut.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,50 +140,21 @@ var BlockOut = Base.extend(
_blockOut: function (image) {

var rect,
coord,
color;

rect = this.getAreaRect().getCoordinates();
color = this.getColor().getColor(true, true);
rect = this.getAreaRect().clone();
rect.limitCoordinates(new Rect({
x: 0,
y: 0,
width: image.getWidth(),
height: image.getHeight()
}));

this._correctCoordinates(image.getWidth(), image.getHeight(), rect);
image.fillRect(rect.x, rect.y, rect.width, rect.height, color);
},
coord = rect.getCoordinates();
color = this.getColor().getColor(true, true);

/**
* Correcting area coordinates if necessary
*
* Note:
* Priority is on the x/y coordinates, and not on the size since the size will then be removed anyways.
*
* @method _correctDimensions
* @param {int} width Image width
* @param {int} height Image height
* @param {object} rect Values for rect
* @param {int} rect.x X value of rect
* @param {int} rect.y Y value of rect
* @param {int} rect.width Width value of rect
* @param {int} rect.height Height value of rect
* @private
*/
_correctCoordinates: function (width, height, rect) {

// Set values if none given
rect.x = rect.x || 0;
rect.y = rect.y || 0;
rect.width = rect.width || width;
rect.height = rect.height || height;

// Check negative values
rect.x = Math.max(0, rect.x);
rect.y = Math.max(0, rect.y);
rect.width = Math.max(0, rect.width);
rect.height = Math.max(0, rect.height);

// Check dimensions
rect.x = Math.min(rect.x, width - 1); // -1 to make sure that there is an image
rect.y = Math.min(rect.y, height - 1);
rect.width = Math.min(rect.width, width - rect.x);
rect.height = Math.min(rect.height, height - rect.y);
image.fillRect(coord.x, coord.y, coord.width, coord.height, color);
}
},

Expand All @@ -193,7 +164,7 @@ var BlockOut = Base.extend(
* @type {string}
* @static
*/
TYPE: 'CONFIGURATION_BLOCKOUT'
TYPE: 'CONFIGURATION_BLOCK_OUT'
}
);

Expand Down

0 comments on commit fd1b892

Please sign in to comment.