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

feat(fabric.controlsUtils) Move drag to actions to control handlers #6617

Merged
merged 8 commits into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 1 addition & 21 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@
}

var pointer = this.getPointer(e), corner = target.__corner,
actionHandler = !!corner && target.controls[corner].getActionHandler(),
actionHandler = !!corner ? target.controls[corner].getActionHandler() : fabric.controlsUtils.dragHandler,
action = this._getActionFromCorner(alreadySelected, corner, e, target),
origin = this._getOriginFromCorner(target, corner),
altKey = e[this.centeredKey],
Expand Down Expand Up @@ -683,26 +683,6 @@
this._beforeTransform(e);
},

/**
* Translates object by "setting" its left/top
* @private
* @param {Number} x pointer's x coordinate
* @param {Number} y pointer's y coordinate
* @return {Boolean} true if the translation occurred
*/
_translateObject: function (x, y) {
var transform = this._currentTransform,
target = transform.target,
newLeft = x - transform.offsetX,
newTop = y - transform.offsetY,
moveX = !target.get('lockMovementX') && target.left !== newLeft,
moveY = !target.get('lockMovementY') && target.top !== newTop;

moveX && target.set('left', newLeft);
moveY && target.set('top', newTop);
return moveX || moveY;
},

/**
* Set the cursor type of the canvas element
* @param {String} value Cursor type of the canvas element.
Expand Down
27 changes: 26 additions & 1 deletion src/controls.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
function fireEvent(eventName, options) {
var target = options.transform.target,
canvas = target.canvas,
canvasOptions = Object.assign({}, options, { target: target });
canvasOptions = fabric.util.clone(options);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix #6565

canvasOptions.target = target;
canvas && canvas.fire('object:' + eventName, canvasOptions);
target.fire(eventName, options);
}
Expand Down Expand Up @@ -690,6 +691,29 @@
return hasResized;
}

/**
* Action handler
* @private
* @param {Event} eventData javascript event that is doing the transform
* @param {Object} transform javascript object containing a series of information around the current transform
* @param {number} x current mouse x position, canvas normalized
* @param {number} y current mouse y position, canvas normalized
* @return {Boolean} true if the translation occurred
*/
function dragHandler(eventData, transform, x, y) {
var target = transform.target,
newLeft = x - transform.offsetX,
newTop = y - transform.offsetY,
moveX = !target.get('lockMovementX') && target.left !== newLeft,
moveY = !target.get('lockMovementY') && target.top !== newTop;
moveX && target.set('left', newLeft);
moveY && target.set('top', newTop);
if (moveX || moveY) {
fireEvent('moving', commonEventInfo(eventData, transform, x, y));
}
return moveX || moveY;
}

controls.scaleCursorStyleHandler = scaleCursorStyleHandler;
controls.skewCursorStyleHandler = skewCursorStyleHandler;
controls.scaleSkewCursorStyleHandler = scaleSkewCursorStyleHandler;
Expand All @@ -702,6 +726,7 @@
controls.changeWidth = wrapWithFixedAnchor(changeWidth);
controls.skewHandlerX = skewHandlerX;
controls.skewHandlerY = skewHandlerY;
controls.dragHandler = dragHandler;
controls.scaleOrSkewActionName = scaleOrSkewActionName;
controls.rotationStyleHandler = rotationStyleHandler;
controls.fireEvent = fireEvent;
Expand Down
22 changes: 7 additions & 15 deletions src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@

if (transform.actionPerformed || (this.stateful && target.hasStateChanged())) {
if (transform.actionPerformed) {
// this is not friendly to the new control api.
eventName = this._addEventOptions(options, transform);
this._fire(eventName, options);
}
Expand Down Expand Up @@ -934,24 +935,15 @@
y = pointer.y,
action = transform.action,
actionPerformed = false,
actionHandler = transform.actionHandler,
actionHandler = transform.actionHandler;
// this object could be created from the function in the control handlers
options = {
target: transform.target,
e: e,
transform: transform,
pointer: pointer
};

if (action === 'drag') {
actionPerformed = this._translateObject(x, y);
if (actionPerformed) {
this._fire('moving', options);
this.setCursor(options.target.moveCursor || this.moveCursor);
}

if (actionHandler) {
actionPerformed = actionHandler(e, transform, x, y);
}
else if (actionHandler) {
(actionPerformed = actionHandler(e, transform, x, y)) && this._fire(action, options);
if (action === 'drag' && actionPerformed) {
this.setCursor(transform.target.moveCursor || this.moveCursor);
}
transform.actionPerformed = transform.actionPerformed || actionPerformed;
},
Expand Down