var BoundsHelper = (function () {
function BoundsHelper(asset) {
this.assetRect = asset.getBoundingRect();
Object.defineProperty(BoundsHelper.prototype, "left", {
return this.assetRect.left;
Object.defineProperty(BoundsHelper.prototype, "top", {
return this.assetRect.top;
Object.defineProperty(BoundsHelper.prototype, "right", {
return this.assetRect.left + this.assetRect.width;
Object.defineProperty(BoundsHelper.prototype, "bottom", {
return this.assetRect.top + this.assetRect.height;
Object.defineProperty(BoundsHelper.prototype, "height", {
return this.assetRect.height;
Object.defineProperty(BoundsHelper.prototype, "width", {
return this.assetRect.width;
var canvas = new fabric.Canvas('c');
var rectangle = new fabric.Rect({
var rectangleBounds = new BoundsHelper(rectangle);
var image = new fabric.Image(i, {
canvas.on("object:scaling", function (event) {
var object = event.target;
objectBounds = new BoundsHelper(object);
imageBounds = new BoundsHelper(image);
if (objectBounds.left < imageBounds.left) {
desiredLength = imageBounds.left - objectBounds.right;
object.scaleX = desiredLength / object.width;
if (objectBounds.right > imageBounds.right) {
desiredLength = imageBounds.right - objectBounds.left;
object.scaleX = desiredLength / object.width;
if (objectBounds.top < imageBounds.top) {
desiredLength = imageBounds.top - objectBounds.bottom;
object.scaleY = desiredLength / object.height;
if (objectBounds.bottom > imageBounds.bottom) {
desiredLength = imageBounds.bottom - objectBounds.top;
object.scaleY = desiredLength / object.height;
canvas.onBeforeScaleRotate = function (targetObject) {
targetObject.lockScalingX = targetObject.lockScalingY = false;
canvas.on('after:render', function() {
canvas.contextContainer.strokeStyle = '#555';
var bound = image.getBoundingRect();
canvas.contextContainer.strokeRect(
canvas.centerObject(image);