Skip to content

Commit

Permalink
fix(controls): Make controls offsetX/offsetY work (fabricjs#6236)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Mar 28, 2020
1 parent 2863024 commit 234d8a0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
27 changes: 13 additions & 14 deletions src/control.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';

var fabric = global.fabric || (global.fabric = { }),
degreesToRadians = fabric.util.degreesToRadians,
util = fabric.util,
renderCircleControl = fabric.controlRenderers.renderCircleControl,
renderSquareControl = fabric.controlRenderers.renderSquareControl;

Expand Down Expand Up @@ -198,29 +198,28 @@


positionHandler: function(dim, finalMatrix, fabricObject /* currentControl */ ) {
var padding = fabricObject.padding, angle = degreesToRadians(fabricObject.angle),
cos = fabric.util.cos(angle), sin = fabric.util.sin(angle), offsetX = this.offsetX,
var padding = fabricObject.padding, angle = util.degreesToRadians(fabricObject.angle),
cos = util.cos(angle), sin = util.sin(angle), offsetX = this.offsetX,
offsetY = this.offsetY, cosP = cos * padding, sinP = sin * padding, cosY = cos * offsetY,
cosX = cos * offsetX, sinY = sin * offsetY, sinX = sin * offsetX,
point = fabric.util.transformPoint({
x: (this.x * dim.x),
y: (this.y * dim.y) }, finalMatrix);
point = util.transformPoint({
x: this.x * dim.x,
y: this.y * dim.y }, finalMatrix);
if (this.x > 0) {
point.y += sinP + sinX + cosY;
point.x += cosP + cosX - sinY;
point.y += sinP + sinX + cosY;
}
if (this.x < 0) {
point.x += -cosP + cosX - sinY;
point.y += -sinP + sinX + cosY;
}
if (this.y > 0) {
point.y += cosP + sinX + cosY;
point.x += -sinP + cosX - sinY;
}
// to be verified
if (this.x < 0) {
point.y += -sinP - sinX - cosY;
point.x += -cosP - cosX + sinY;
point.y += cosP + sinX + cosY;
}
if (this.y < 0) {
point.y += -cosP - sinX + cosY;
point.x += sinP + cosX - sinY;
point.y += -cosP - sinX + cosY;
}
return point;
},
Expand Down
24 changes: 14 additions & 10 deletions src/controls.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@
};
}

function getLocalPoint(target, originX, originY, x, y) {
var zoom = target.canvas.getZoom(),
function getLocalPoint(transform, originX, originY, x, y) {
var target = transform.target,
control = target.controls[transform.corner],
zoom = target.canvas.getZoom(),
padding = target.padding / zoom,
localPoint = target.toLocalPoint(new fabric.Point(x, y), originX, originY);
if (localPoint.x >= padding) {
Expand All @@ -138,6 +140,8 @@
if (localPoint.y <= padding) {
localPoint.y += padding;
}
localPoint.x -= control.offsetX;
localPoint.y -= control.offsetY;
return localPoint;
}

Expand All @@ -157,7 +161,7 @@
var target = transform.target,
// find how big the object would be, if there was no skewX. takes in account scaling
dimNoSkew = target._getTransformedDimensions(0, target.skewY),
localPoint = getLocalPoint(target, transform.originX, transform.originY, x, y),
localPoint = getLocalPoint(transform, transform.originX, transform.originY, x, y),
// the mouse is in the center of the object, and we want it to stay there.
// so the object will grow twice as much as the mouse.
// this makes the skew growth to localPoint * 2 - dimNoSkew.
Expand All @@ -168,7 +172,7 @@
newSkew = 0;
}
else {
newSkew = fabric.util.radiansToDegrees(
newSkew = radiansToDegrees(
Math.atan2((totalSkewSize / target.scaleX), (dimNoSkew.y / target.scaleY))
);
// now we have to find the sign of the skew.
Expand Down Expand Up @@ -197,7 +201,7 @@
var target = transform.target,
// find how big the object would be, if there was no skewX. takes in account scaling
dimNoSkew = target._getTransformedDimensions(target.skewX, 0),
localPoint = getLocalPoint(target, transform.originX, transform.originY, x, y),
localPoint = getLocalPoint(transform, transform.originX, transform.originY, x, y),
// the mouse is in the center of the object, and we want it to stay there.
// so the object will grow twice as much as the mouse.
// this makes the skew growth to localPoint * 2 - dimNoSkew.
Expand All @@ -208,7 +212,7 @@
newSkew = 0;
}
else {
newSkew = fabric.util.radiansToDegrees(
newSkew = radiansToDegrees(
Math.atan2((totalSkewSize / target.scaleY), (dimNoSkew.x / target.scaleX))
);
// now we have to find the sign of the skew.
Expand Down Expand Up @@ -246,7 +250,7 @@
return false;
}
if (currentSkew === 0) {
var localPointFromCenter = getLocalPoint(target, CENTER, CENTER, x, y);
var localPointFromCenter = getLocalPoint(transform, CENTER, CENTER, x, y);
if (localPointFromCenter.x > 0) {
// we are pulling right, anchor left;
originX = LEFT;
Expand Down Expand Up @@ -288,7 +292,7 @@
return false;
}
if (currentSkew === 0) {
var localPointFromCenter = getLocalPoint(target, CENTER, CENTER, x, y);
var localPointFromCenter = getLocalPoint(transform, CENTER, CENTER, x, y);
if (localPointFromCenter.y > 0) {
// we are pulling down, anchor up;
originY = TOP;
Expand Down Expand Up @@ -373,7 +377,7 @@
}

dim = target._getTransformedDimensions();
newPoint = getLocalPoint(target, transform.originX, transform.originY, x, y);
newPoint = getLocalPoint(transform, transform.originX, transform.originY, x, y);

// missing detection of flip and logic to switch the origin
if (scaleProportionally && !by) {
Expand Down Expand Up @@ -443,7 +447,7 @@

// currently unusued, needed for the textbox.
function changeWidth(eventData, transform, x, y) {
var target = transform.target, localPoint = getLocalPoint(target, transform.originX, transform.originY, x, y),
var target = transform.target, localPoint = getLocalPoint(transform, transform.originX, transform.originY, x, y),
strokePadding = target.strokeWidth / (target.strokeUniform ? target.scaleX : 1),
newWidth = Math.abs(localPoint.x / target.scaleX) - strokePadding;
target.set('width', Math.max(newWidth, 0));
Expand Down
1 change: 1 addition & 0 deletions test/unit/controls_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
originX: 'left',
orginY: 'top',
target: target,
corner: 'mr',
};
});
hooks.afterEach(function() {
Expand Down

0 comments on commit 234d8a0

Please sign in to comment.