Skip to content

Commit

Permalink
Account for bounding box in scaleToWidth/scaleToHeight. Fix #271. Ver…
Browse files Browse the repository at this point in the history
…sion 0.9.12.
  • Loading branch information
kangax committed Oct 8, 2012
1 parent 4ac8b68 commit 999f7f5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion HEADER.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! Fabric.js Copyright 2008-2012, Printio (Juriy Zaytsev, Maxim Chernyak) */

var fabric = fabric || { version: "0.9.11" };
var fabric = fabric || { version: "0.9.12" };

if (typeof exports != 'undefined') {
exports.fabric = fabric;
Expand Down
17 changes: 12 additions & 5 deletions dist/all.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* build: `node build.js modules=ALL` */
/*! Fabric.js Copyright 2008-2012, Printio (Juriy Zaytsev, Maxim Chernyak) */

var fabric = fabric || { version: "0.9.11" };
var fabric = fabric || { version: "0.9.12" };

if (typeof exports != 'undefined') {
exports.fabric = fabric;
Expand Down Expand Up @@ -8070,29 +8070,34 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
scale: function(value) {
this.scaleX = value;
this.scaleY = value;
this.setCoords();
return this;
},

/**
* Scales an object to a given width (scaling by x/y equally)
* Scales an object to a given width, with respect to bounding box (scaling by x/y equally)
* @method scaleToWidth
* @param value {Number} new width value
* @return {fabric.Object} thisArg
* @chainable
*/
scaleToWidth: function(value) {
return this.scale(value / this.width);
// adjust to bounding rect factor so that rotated shapes would fit as well
var boundingRectFactor = this.getBoundingRectWidth() / this.getWidth();
return this.scale(value / this.width / boundingRectFactor);
},

/**
* Scales an object to a given height (scaling by x/y equally)
* Scales an object to a given height, with respect to bounding box (scaling by x/y equally)
* @method scaleToHeight
* @param value {Number} new height value
* @return {fabric.Object} thisArg
* @chainable
*/
scaleToHeight: function(value) {
return this.scale(value / this.height);
// adjust to bounding rect factor so that rotated shapes would fit as well
var boundingRectFactor = this.getBoundingRectHeight() / this.getHeight();
return this.scale(value / this.height / boundingRectFactor);
},

/**
Expand Down Expand Up @@ -8221,6 +8226,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
* @return {Number} width value
*/
getBoundingRectWidth: function() {
this.oCoords || this.setCoords();
var xCoords = [this.oCoords.tl.x, this.oCoords.tr.x, this.oCoords.br.x, this.oCoords.bl.x];
var minX = fabric.util.array.min(xCoords);
var maxX = fabric.util.array.max(xCoords);
Expand All @@ -8233,6 +8239,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
* @return {Number} height value
*/
getBoundingRectHeight: function() {
this.oCoords || this.setCoords();
var yCoords = [this.oCoords.tl.y, this.oCoords.tr.y, this.oCoords.br.y, this.oCoords.bl.y];
var minY = fabric.util.array.min(yCoords);
var maxY = fabric.util.array.max(yCoords);
Expand Down
4 changes: 2 additions & 2 deletions dist/all.min.js

Large diffs are not rendered by default.

Binary file modified dist/all.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fabric",
"description": "Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.",
"version": "0.9.11",
"version": "0.9.12",
"author": "Juriy Zaytsev <kangax@gmail.com>",
"keywords": ["canvas", "graphic", "graphics", "SVG", "node-canvas", "parser", "HTML5", "object model"],
"repository": "git://github.com/kangax/fabric.js",
Expand Down
15 changes: 11 additions & 4 deletions src/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,29 +559,34 @@
scale: function(value) {
this.scaleX = value;
this.scaleY = value;
this.setCoords();
return this;
},

/**
* Scales an object to a given width (scaling by x/y equally)
* Scales an object to a given width, with respect to bounding box (scaling by x/y equally)
* @method scaleToWidth
* @param value {Number} new width value
* @return {fabric.Object} thisArg
* @chainable
*/
scaleToWidth: function(value) {
return this.scale(value / this.width);
// adjust to bounding rect factor so that rotated shapes would fit as well
var boundingRectFactor = this.getBoundingRectWidth() / this.getWidth();
return this.scale(value / this.width / boundingRectFactor);
},

/**
* Scales an object to a given height (scaling by x/y equally)
* Scales an object to a given height, with respect to bounding box (scaling by x/y equally)
* @method scaleToHeight
* @param value {Number} new height value
* @return {fabric.Object} thisArg
* @chainable
*/
scaleToHeight: function(value) {
return this.scale(value / this.height);
// adjust to bounding rect factor so that rotated shapes would fit as well
var boundingRectFactor = this.getBoundingRectHeight() / this.getHeight();
return this.scale(value / this.height / boundingRectFactor);
},

/**
Expand Down Expand Up @@ -710,6 +715,7 @@
* @return {Number} width value
*/
getBoundingRectWidth: function() {
this.oCoords || this.setCoords();
var xCoords = [this.oCoords.tl.x, this.oCoords.tr.x, this.oCoords.br.x, this.oCoords.bl.x];
var minX = fabric.util.array.min(xCoords);
var maxX = fabric.util.array.max(xCoords);
Expand All @@ -722,6 +728,7 @@
* @return {Number} height value
*/
getBoundingRectHeight: function() {
this.oCoords || this.setCoords();
var yCoords = [this.oCoords.tl.y, this.oCoords.tr.y, this.oCoords.br.y, this.oCoords.bl.y];
var minY = fabric.util.array.min(yCoords);
var maxY = fabric.util.array.max(yCoords);
Expand Down
14 changes: 14 additions & 0 deletions test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,20 @@
equal(cObj.get('scaleY'), 100/560);
});

test('scaleToWidth on rotated object', function() {
var obj = new fabric.Object({ height: 100, width: 100 });
obj.rotate(45);
obj.scaleToWidth(200);
equal(Math.round(obj.getBoundingRectWidth()), 200);
});

test('scaleToHeight on rotated object', function() {
var obj = new fabric.Object({ height: 100, width: 100 });
obj.rotate(45);
obj.scaleToHeight(300);
equal(Math.round(obj.getBoundingRectHeight()), 300);
});

test('setOpacity', function() {
var cObj = new fabric.Object();
ok(typeof cObj.setOpacity == 'function');
Expand Down

0 comments on commit 999f7f5

Please sign in to comment.