diff --git a/src/shapes/line.class.js b/src/shapes/line.class.js index febb1093..70acdfa5 100644 --- a/src/shapes/line.class.js +++ b/src/shapes/line.class.js @@ -146,39 +146,6 @@ } ), - /** - * @private - * @return {Number} centerToCenterX Distance from center of path group to horizontal center of Line. - */ - _getCenterToCenterX: makeCenterToCenterGetter( - { // property names - origin: 'originX', - coordinate: 'left', - dimension: 'width', - }, - { // possible non-center values of origin - nearest: 'left', - farthest: 'right', - } - ), - - /** - * @private - * @return {Number} centerToOriginY Distance from center of path group to vertical center of Line. - */ - _getCenterToCenterY: makeCenterToCenterGetter( - { // property names - origin: 'originY', - coordinate: 'top', - dimension: 'height', - }, - { // possible non-center values of origin - nearest: 'top', - farthest: 'bottom', - } - ), - - /** * @private * @param {CanvasRenderingContext2D} ctx Context to render on @@ -188,9 +155,14 @@ var isInPathGroup = this.group && this.group.type === 'path-group'; if (isInPathGroup && !this.transformMatrix) { + // Line coords are distances from left-top of canvas to origin of line. + // + // To render line in a path-group, we need to translate them to + // distances from center of path-group to center of line. + var cp = this.getCenterPoint(); ctx.translate( - this._getCenterToCenterX(), - this._getCenterToCenterY() + -this.group.width/2 + cp.x, + -this.group.height / 2 + cp.y ); } @@ -350,35 +322,4 @@ } - /** - * Produces a function that calculates distance from path group center to center of Line dimension. - * - */ - function makeCenterToCenterGetter(propertyNames, originValues) { - var origin = propertyNames.origin, - coordinate = propertyNames.coordinate, - dimension = propertyNames.dimension, - nearest = originValues.nearest, - farthest = originValues.farthest; - - return function() { - /* - * Line coords are distances from left-top of canvas to origin of line. - * - * To render line in a path-group, we need to translate them to distances - * from center of path-group to center of line. - */ - var toPathGroupEdge = (-0.5 * this.group.get(dimension)) - var toLineCenter = 0; // assume center - - if (this.get(origin) === nearest) { - toLineCenter = +0.5 * this.get(dimension); - } else if (this.get(origin) === farthest) { - toLineCenter = -0.5 * this.get(dimension); - } // else center, don't change the initial value - - return toPathGroupEdge + this.get(coordinate) + toLineCenter; - }; - } - })(typeof exports !== 'undefined' ? exports : this); diff --git a/test/unit/line.js b/test/unit/line.js index 068720e0..1c0ced12 100644 --- a/test/unit/line.js +++ b/test/unit/line.js @@ -590,198 +590,4 @@ }); }); - var getCenterToCenterXCases = [ - { description: 'for center origin, is the distance to the left edge of group', - given: { - left: 0, - originX: 'center', - group: { width: 10 } - }, - expectedCenter: (-0.5 * 10), - }, - { description: 'includes negative offset for center origin', - given: { - left: 0-11, - originX: 'center', - group: { width: 20 } - }, - expectedCenter: (-0.5 * 20) - 11, - }, - { description: 'includes positive offset for center origin', - given: { - left: 0+7, - originX: 'center', - group: { width: 30 } - }, - expectedCenter: (-0.5 * 30) + 7, - }, - { description: 'for left origin, is the distance to the left edge of group, offset by half-line-width to the right', - givenWidth: 17, - given: { - left: 0, - originX: 'left', - group: { width: 12 } - }, - expectedCenter: (-0.5 * 12) + (0.5 * 17), - }, - { description: 'includes negative offset for left origin', - givenWidth: 17, - given: { - left: 0-13, - originX: 'left', - group: { width: 22 } - }, - expectedCenter: (-0.5 * 22) + (0.5 * 17) - 13, - }, - { description: 'includes positive offset for left origin', - givenWidth: 17, - given: { - left: 0+19, - originX: 'left', - group: { width: 32 } - }, - expectedCenter: (-0.5 * 32) + (0.5 * 17) + 19, - }, - { description: 'for right origin, is the distance to the left edge of group, offset by half-line-width to the left', - givenWidth: 17, - given: { - left: 0, - originX: 'right', - group: { width: 13 } - }, - expectedCenter: (-0.5 * 13) - (0.5 * 17), - }, - { description: 'includes negative offset for right origin', - givenWidth: 17, - given: { - left: 0-15, - originX: 'right', - group: { width: 23 } - }, - expectedCenter: (-0.5 * 23) - (0.5 * 17) - 15, - }, - { description: 'includes positive offset for right origin', - givenWidth: 17, - given: { - left: 0+21, - originX: 'right', - group: { width: 33 } - }, - expectedCenter: (-0.5 * 33) - (0.5 * 17) + 21, - }, - ]; - - getCenterToCenterXCases.forEach(function (c_) { - test('Line._getCenterToCenterX ' + c_.description, function () { - var line = new fabric.Line( - [0, 0, c_.givenWidth, 0] - ); - for (var prop in c_.given) { - line.set(prop, c_.given[prop]); - } - line.group.get = function (prop) { - return this[prop]; - }; - - equal(line._getCenterToCenterX(), c_.expectedCenter); - }); - }); - - var getCenterToCenterYCases = [ - { description: 'for center origin, is the distance to the top edge of group', - given: { - top: 0, - originY: 'center', - group: { height: 10 } - }, - expectedCenter: (-0.5 * 10), - }, - { description: 'includes negative offset for center origin', - given: { - top: 0-11, - originY: 'center', - group: { height: 20 } - }, - expectedCenter: (-0.5 * 20) - 11, - }, - { description: 'includes positive offset for center origin', - given: { - top: 0+7, - originY: 'center', - group: { height: 30 } - }, - expectedCenter: (-0.5 * 30) + 7, - }, - { description: 'for top origin, is the distance to the top edge of group, offset by half-line-height to the bottom', - givenHeight: 17, - given: { - top: 0, - originY: 'top', - group: { height: 12 } - }, - expectedCenter: (-0.5 * 12) + (0.5 * 17), - }, - { description: 'includes negative offset for top origin', - givenHeight: 17, - given: { - top: 0-13, - originY: 'top', - group: { height: 22 } - }, - expectedCenter: (-0.5 * 22) + (0.5 * 17) - 13, - }, - { description: 'includes positive offset for top origin', - givenHeight: 17, - given: { - top: 0+19, - originY: 'top', - group: { height: 32 } - }, - expectedCenter: (-0.5 * 32) + (0.5 * 17) + 19, - }, - { description: 'for bottom origin, is the distance to the top edge of group, offset by half-line-height to the top', - givenHeight: 17, - given: { - top: 0, - originY: 'bottom', - group: { height: 13 } - }, - expectedCenter: (-0.5 * 13) - (0.5 * 17), - }, - { description: 'includes negative offset for bottom origin', - givenHeight: 17, - given: { - top: 0-15, - originY: 'bottom', - group: { height: 23 } - }, - expectedCenter: (-0.5 * 23) - (0.5 * 17) - 15, - }, - { description: 'includes positive offset for bottom origin', - givenHeight: 17, - given: { - top: 0+21, - originY: 'bottom', - group: { height: 33 } - }, - expectedCenter: (-0.5 * 33) - (0.5 * 17) + 21, - }, - ]; - - getCenterToCenterYCases.forEach(function (c_) { - test('Line._getCenterToCenterY ' + c_.description, function () { - var line = new fabric.Line( - [0, 0, 0, c_.givenHeight] - ); - for (var prop in c_.given) { - line.set(prop, c_.given[prop]); - } - line.group.get = function (prop) { - return this[prop]; - }; - - equal(line._getCenterToCenterY(), c_.expectedCenter); - }); - }); - })();