diff --git a/src/mixins/object_origin.mixin.js b/src/mixins/object_origin.mixin.js index b3b28041..fbcce105 100644 --- a/src/mixins/object_origin.mixin.js +++ b/src/mixins/object_origin.mixin.js @@ -198,6 +198,45 @@ this.originX = to; }, + /** + * @private + * Sets the origin/position of the object to it's center point + * @return {void} + */ + _setOriginToCenter: function() { + this._originalOriginX = this.originX; + this._originalOriginY = this.originY; + + var center = this.getCenterPoint(); + + this.originX = 'center'; + this.originY = 'center'; + + this.left = center.x; + this.top = center.y; + }, + + /** + * @private + * Resets the origin/position of the object to it's original origin + * @return {void} + */ + _resetOrigin: function() { + var originPoint = this.translateToOriginPoint( + this.getCenterPoint(), + this._originalOriginX, + this._originalOriginY); + + this.originX = this._originalOriginX; + this.originY = this._originalOriginY; + + this.left = originPoint.x; + this.top = originPoint.y; + + this._originalOriginX = null; + this._originalOriginY = null; + }, + /** * @private */ diff --git a/src/shapes/object.class.js b/src/shapes/object.class.js index aaece8cd..a2cf787f 100644 --- a/src/shapes/object.class.js +++ b/src/shapes/object.class.js @@ -1310,7 +1310,7 @@ /** * Sets "color" of an instance (alias of `set('fill', …)`) * @param {String} color Color value - * @return {fabric.Text} thisArg + * @return {fabric.Object} thisArg * @chainable */ setColor: function(color) { @@ -1318,6 +1318,28 @@ return this; }, + /** + * Sets "angle" of an instance + * @param {Number} angle Angle value + * @return {fabric.Object} thisArg + * @chainable + */ + setAngle: function(angle) { + var shouldCenterOrigin = (this.originX !== 'center' || this.originY !== 'center') && this.centeredRotation; + + if (shouldCenterOrigin) { + this._setOriginToCenter(); + } + + this.set('angle', angle); + + if (shouldCenterOrigin) { + this._resetOrigin(); + } + + return this; + }, + /** * Centers object horizontally on canvas to which it was added last. * You might need to call `setCoords` on an object after centering, to update controls area.