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

PencilBrush SHIFT/straight line functionality #7034

Merged
merged 12 commits into from
Sep 9, 2021
52 changes: 51 additions & 1 deletion src/brushes/pencil_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
*/
decimate: 0.4,

/**
* Draws a straight line between last recorded point to current pointer
* Used for `shift` functionality
*
* @example <caption>Override `shift` functionality</caption>
* brush._detach();
* @example <caption>Restore `shift` functionality</caption>
* brush._attachKeyboardListeners();
*
* @type boolean
* @default false
*/
drawStraightLine: false,

/**
* Constructor
* @param {fabric.Canvas} canvas
Expand All @@ -21,6 +35,37 @@
initialize: function(canvas) {
this.canvas = canvas;
this._points = [];
this._attachKeyboardListeners();
},

/**
* Listens to `shift` key press
* @private
* @returns disposer
*/
_attachKeyboardListeners: function () {
var _this = this;
var keyboardDown = function (e) {
if (e.shiftKey) {
e.preventDefault();
_this.drawStraightLine = true;
}
};
var keyboardUp = function () {
_this.drawStraightLine = false;
};

window.addEventListener('keydown', keyboardDown);
window.addEventListener('keyup', keyboardUp);
this._detach = function disposer() {
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved
window.removeEventListener('keydown', keyboardDown);
window.removeEventListener('keyup', keyboardUp);
};
return this._detach;
},
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved

needsFullRender: function () {
return this.callSuper("needsFullRender") || this._hasStraightLine;
},

/**
Expand Down Expand Up @@ -114,6 +159,10 @@
if (this._points.length > 1 && point.eq(this._points[this._points.length - 1])) {
return false;
}
if (this.drawStraightLine && this._points.length > 1) {
this._hasStraightLine = true;
this._points.pop();
}
this._points.push(point);
return true;
},
Expand All @@ -126,6 +175,7 @@
this._points = [];
this._setBrushStyles();
this._setShadow();
this._hasStraightLine = false;
},

/**
Expand Down Expand Up @@ -246,7 +296,7 @@
var zoom = this.canvas.getZoom(), adjustedDistance = Math.pow(distance / zoom, 2),
i, l = points.length - 1, lastPoint = points[0], newPoints = [lastPoint],
cDistance;
for (i = 1; i < l; i++) {
for (i = 1; i <= l; i++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needs to be validated against #6966

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is why there's a merge conflict

Copy link
Member

Choose a reason for hiding this comment

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

so master is at < l -1 to fix a tail issue with decimate.
Why did you add the equal in?
If you explain we can solve the conflict.
But to me is not clear why this has changed.

Copy link
Contributor Author

@ShaMan123 ShaMan123 Aug 4, 2021

Choose a reason for hiding this comment

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

The logic of straight line pops the last point and push the last pointer.
When mouse up fires and drawStraightLine=true it decimated the end and removes the last straight line part.
I don't know/understand what decimate does so I don't want to propose anything but this can be mitigated by pushing another point on mouse up if drawStraightLine=true (I don't like it though)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cDistance = Math.pow(lastPoint.x - points[i].x, 2) + Math.pow(lastPoint.y - points[i].y, 2);
if (cDistance >= adjustedDistance) {
lastPoint = points[i];
Expand Down