Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional method for centering objects #3044

Merged
merged 9 commits into from
Jun 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
605 changes: 392 additions & 213 deletions dist/fabric.js

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions dist/fabric.min.js

Large diffs are not rendered by default.

Binary file modified dist/fabric.min.js.gz
Binary file not shown.
12,076 changes: 11 additions & 12,065 deletions dist/fabric.require.js

Large diffs are not rendered by default.

41 changes: 37 additions & 4 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,18 @@
* @chainable
*/
centerH: function () {
this.canvas.centerObjectH(this);
this.canvas && this.canvas.centerObjectH(this);
return this;
},

/**
* Centers object horizontally on current viewport of canvas to which it was added last.
* You might need to call `setCoords` on an object after centering, to update controls area.
* @return {fabric.Object} thisArg
* @chainable
*/
viewportCenterH: function () {
this.canvas && this.canvas.viewportCenterObjectH(this);
return this;
},

Expand All @@ -1565,7 +1576,18 @@
* @chainable
*/
centerV: function () {
this.canvas.centerObjectV(this);
this.canvas && this.canvas.centerObjectV(this);
return this;
},

/**
* Centers object vertically on current viewport of canvas to which it was added last.
* You might need to call `setCoords` on an object after centering, to update controls area.
* @return {fabric.Object} thisArg
* @chainable
*/
viewportCenterV: function () {
this.canvas && this.canvas.viewportCenterObjectV(this);
return this;
},

Expand All @@ -1576,7 +1598,18 @@
* @chainable
*/
center: function () {
this.canvas.centerObject(this);
this.canvas && this.canvas.centerObject(this);
return this;
},

/**
* Centers object on current viewport of canvas to which it was added last.
* You might need to call `setCoords` on an object after centering, to update controls area.
* @return {fabric.Object} thisArg
* @chainable
*/
viewportCenter: function () {
this.canvas && this.canvas.viewportCenterObject(this);
return this;
},

Expand All @@ -1586,7 +1619,7 @@
* @chainable
*/
remove: function() {
this.canvas.remove(this);
this.canvas && this.canvas.remove(this);
return this;
},

Expand Down
67 changes: 56 additions & 11 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -996,32 +996,28 @@
},

/**
* Centers object horizontally.
* Centers object horizontally in the canvas
* You might need to call `setCoords` on an object after centering, to update controls area.
* @param {fabric.Object} object Object to center horizontally
* @return {fabric.Canvas} thisArg
*/
centerObjectH: function (object) {
this._centerObject(object, new fabric.Point(this.getCenter().left, object.getCenterPoint().y));
this.renderAll();
return this;
return this._centerObject(object, new fabric.Point(this.getCenter().left, object.getCenterPoint().y));
},

/**
* Centers object vertically.
* Centers object vertically in the canvas
* You might need to call `setCoords` on an object after centering, to update controls area.
* @param {fabric.Object} object Object to center vertically
* @return {fabric.Canvas} thisArg
* @chainable
*/
centerObjectV: function (object) {
this._centerObject(object, new fabric.Point(object.getCenterPoint().x, this.getCenter().top));
this.renderAll();
return this;
return this._centerObject(object, new fabric.Point(object.getCenterPoint().x, this.getCenter().top));
},

/**
* Centers object vertically and horizontally.
* Centers object vertically and horizontally in the canvas
* You might need to call `setCoords` on an object after centering, to update controls area.
* @param {fabric.Object} object Object to center vertically and horizontally
* @return {fabric.Canvas} thisArg
Expand All @@ -1030,11 +1026,59 @@
centerObject: function(object) {
var center = this.getCenter();

this._centerObject(object, new fabric.Point(center.left, center.top));
this.renderAll();
return this._centerObject(object, new fabric.Point(center.left, center.top));
},

/**
* Centers object vertically and horizontally in the viewport
* You might need to call `setCoords` on an object after centering, to update controls area.
* @param {fabric.Object} object Object to center vertically and horizontally
* @return {fabric.Canvas} thisArg
* @chainable
*/
viewportCenterObject: function(object) {
var vpCenter = this.getVpCenter();

return this._centerObject(object, vpCenter);
},

/**
* Centers object horizontally in the viewport, object.top is unchanged
* You might need to call `setCoords` on an object after centering, to update controls area.
* @param {fabric.Object} object Object to center vertically and horizontally
* @return {fabric.Canvas} thisArg
* @chainable
*/
viewportCenterObjectH: function(object) {
var vpCenter = this.getVpCenter();
this._centerObject(object, new fabric.Point(vpCenter.x, object.getCenterPoint().y));
return this;
},

/**
* Centers object Vertically in the viewport, object.top is unchanged
* You might need to call `setCoords` on an object after centering, to update controls area.
* @param {fabric.Object} object Object to center vertically and horizontally
* @return {fabric.Canvas} thisArg
* @chainable
*/
viewportCenterObjectV: function(object) {
var vpCenter = this.getVpCenter();

return this._centerObject(object, new fabric.Point(object.getCenterPoint().x, vpCenter.y));
},

/**
* Calculate the point in canvas that correspond to the center of actual viewport.
* @return {fabric.Point} vpCenter, viewport center
* @chainable
*/
getVpCenter: function() {
var center = this.getCenter(),
iVpt = fabric.util.invertTransform(this.viewportTransform);
return fabric.util.transformPoint({ x: center.left, y: center.top }, iVpt);
},

/**
* @private
* @param {fabric.Object} object Object to center
Expand All @@ -1044,6 +1088,7 @@
*/
_centerObject: function(object, center) {
object.setPositionByOrigin(center, 'center', 'center');
this.renderAll();
return this;
},

Expand Down
83 changes: 76 additions & 7 deletions test/unit/canvas_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@
}

QUnit.module('fabric.StaticCanvas', {
teardown: function() {
setup: function() {
canvas.clear();
canvas.backgroundColor = fabric.StaticCanvas.prototype.backgroundColor;
canvas.backgroundImage = fabric.StaticCanvas.prototype.backgroundImage;
canvas.overlayColor = fabric.StaticCanvas.prototype.overlayColor;
canvas.controlsAboveOverlay = fabric.StaticCanvas.prototype.controlsAboveOverlay;
canvas.preserveObjectStacking = fabric.StaticCanvas.prototype.preserveObjectStacking;
canvas.viewportTransform = fabric.StaticCanvas.prototype.viewportTransform;
canvas.calcOffset();
}
});
Expand Down Expand Up @@ -482,15 +483,21 @@
var rect = makeRect({ left: 102, top: 202 });
canvas.add(rect);
equal(canvas.centerObjectH(rect), canvas, 'should be chainable');
equal(rect.getCenterPoint().x, canvas.width / 2, 'object\'s "left" property should correspond to canvas element\'s center');
equal(rect.getCenterPoint().x, canvas.width / 2, 'object\'s "center.y" property should correspond to canvas element\'s center');
canvas.setZoom(4);
equal(rect.getCenterPoint().x, canvas.height / 2, 'object\'s "center.x" property should correspond to canvas element\'s center when canvas is transformed');

});

test('centerObjectV', function() {
ok(typeof canvas.centerObjectV == 'function');
var rect = makeRect({ left: 102, top: 202 });
canvas.add(rect);
equal(canvas.centerObjectV(rect), canvas, 'should be chainable');
equal(rect.getCenterPoint().y, canvas.height / 2, 'object\'s "top" property should correspond to canvas element\'s center');
equal(rect.getCenterPoint().y, canvas.height / 2, 'object\'s "center.y" property should correspond to canvas element\'s center');
canvas.setZoom(2);
equal(rect.getCenterPoint().y, canvas.height / 2, 'object\'s "center.y" property should correspond to canvas element\'s center when canvas is transformed');

});

test('centerObject', function() {
Expand All @@ -499,8 +506,70 @@
canvas.add(rect);
equal(canvas.centerObject(rect), canvas, 'should be chainable');

equal(rect.getCenterPoint().y, canvas.height / 2, 'object\'s "top" property should correspond to canvas element\'s center');
equal(rect.getCenterPoint().x, canvas.height / 2, 'object\'s "left" property should correspond to canvas element\'s center');
equal(rect.getCenterPoint().y, canvas.height / 2, 'object\'s "center.y" property should correspond to canvas element\'s center');
equal(rect.getCenterPoint().x, canvas.height / 2, 'object\'s "center.x" property should correspond to canvas element\'s center');
canvas.setZoom(4);
equal(rect.getCenterPoint().y, canvas.height / 2, 'object\'s "center.y" property should correspond to canvas element\'s center when canvas is transformed');
equal(rect.getCenterPoint().x, canvas.height / 2, 'object\'s "center.x" property should correspond to canvas element\'s center when canvas is transformed');
});

test('viewportCenterObjectH', function() {
ok(typeof canvas.viewportCenterObjectH == 'function');
var rect = makeRect({ left: 102, top: 202 }), pan = 10;
canvas.viewportTransform = [1, 0, 0, 1, 0, 0];
canvas.add(rect);
var oldY = rect.top;
equal(canvas.viewportCenterObjectH(rect), canvas, 'should be chainable');
equal(rect.getCenterPoint().x, canvas.width / 2, 'object\'s "center.x" property should correspond to canvas element\'s center when canvas is not transformed');
equal(rect.top, oldY, 'object\'s "top" should not change');
canvas.setZoom(2);
canvas.viewportCenterObjectH(rect);
equal(rect.getCenterPoint().x, canvas.width / (2 * canvas.getZoom()), 'object\'s "center.x" property should correspond to viewport center');
equal(rect.top, oldY, 'object\'s "top" should not change');
canvas.absolutePan({x: pan, y: pan});
canvas.viewportCenterObjectH(rect);
equal(rect.getCenterPoint().x, (canvas.width / 2 + pan) / canvas.getZoom(), 'object\'s "center.x" property should correspond to viewport center');
equal(rect.top, oldY, 'object\'s "top" should not change');
});

test('viewportCenterObjectV', function() {
ok(typeof canvas.viewportCenterObjectV == 'function');
var rect = makeRect({ left: 102, top: 202 }), pan = 10;
canvas.viewportTransform = [1, 0, 0, 1, 0, 0];
canvas.add(rect);
var oldX = rect.left;
equal(canvas.viewportCenterObjectV(rect), canvas, 'should be chainable');
equal(rect.getCenterPoint().y, canvas.height / 2, 'object\'s "center.y" property should correspond to canvas element\'s center when canvas is not transformed');
equal(rect.left, oldX, 'x position did not change');
canvas.setZoom(2);
canvas.viewportCenterObjectV(rect);
equal(rect.getCenterPoint().y, canvas.height / (2 * canvas.getZoom()), 'object\'s "center.y" property should correspond to viewport center');
equal(rect.left, oldX, 'x position did not change');
canvas.absolutePan({x: pan, y: pan});
canvas.viewportCenterObjectV(rect);
equal(rect.getCenterPoint().y, (canvas.height / 2 + pan) / canvas.getZoom(), 'object\'s "top" property should correspond to viewport center');
equal(rect.left, oldX, 'x position did not change');
});

test('viewportCenterObject', function() {
ok(typeof canvas.viewportCenterObject == 'function');
var rect = makeRect({ left: 102, top: 202 }), pan = 10;
canvas.viewportTransform = [1, 0, 0, 1, 0, 0];
canvas.add(rect);
equal(canvas.viewportCenterObject(rect), canvas, 'should be chainable');
equal(rect.getCenterPoint().y, canvas.height / 2, 'object\'s "center.y" property should correspond to canvas element\'s center when canvas is not transformed');
equal(rect.getCenterPoint().x, canvas.width / 2, 'object\'s "center.x" property should correspond to canvas element\'s center when canvas is not transformed');

canvas.setZoom(2);
canvas.viewportCenterObject(rect);
equal(rect.getCenterPoint().y, canvas.height / (2 * canvas.getZoom()), 'object\'s "center.y" property should correspond to viewport center');
equal(rect.getCenterPoint().x, canvas.width / (2 * canvas.getZoom()), 'object\'s "center.x" property should correspond to viewport center');

canvas.absolutePan({x: pan, y: pan});
canvas.viewportCenterObject(rect);
equal(rect.getCenterPoint().y, (canvas.height / 2 + pan) / canvas.getZoom(), 'object\'s "center.y" property should correspond to viewport center');
equal(rect.getCenterPoint().x, (canvas.width / 2 + pan) / canvas.getZoom(), 'object\'s "center.x" property should correspond to viewport center');
canvas.viewportTransform = [1, 0, 0, 1, 0, 0];
});

test('straightenObject', function() {
Expand All @@ -522,15 +591,15 @@
test('toSVG', function() {
ok(typeof canvas.toSVG == 'function');
canvas.clear();

canvas.viewportTransform = [1, 0, 0, 1, 0, 0];
var svg = canvas.toSVG();
equal(svg, CANVAS_SVG);
});

test('toSVG with different encoding (ISO-8859-1)', function() {
ok(typeof canvas.toSVG == 'function');
canvas.clear();

canvas.viewportTransform = [1, 0, 0, 1, 0, 0];
var svg = canvas.toSVG({encoding: 'ISO-8859-1'});
var svgDefaultEncoding = canvas.toSVG();
ok(svg != svgDefaultEncoding);
Expand Down
Loading