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
28 changes: 28 additions & 0 deletions src/brushes/pencil_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
*/
decimate: 0.4,

/**
* Draws a straight line between last recorded point to current pointer
* Used for `shift` functionality
*
* @type boolean
* @default false
*/
drawStraightLine: false,

/**
* The event modifier key that makes the brush draw a straight line.
* If `null` or 'none' or any other string that is not a modifier key the feature is disabled.
* @type {'altKey' | 'shiftKey' | 'ctrlKey' | 'none' | undefined | null}
*/
straightLineKey: 'shiftKey',

/**
* Constructor
* @param {fabric.Canvas} canvas
Expand All @@ -23,6 +39,10 @@
this._points = [];
},
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved

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

/**
* Invoked inside on mouse down and mouse move
* @param {Object} pointer
Expand All @@ -41,6 +61,7 @@
if (!this.canvas._isMainEvent(options.e)) {
return;
}
this.drawStraightLine = options.e[this.straightLineKey];
this._prepareForDrawing(pointer);
// capture coordinates immediately
// this allows to draw dots (when movement never occurs)
Expand All @@ -56,6 +77,7 @@
if (!this.canvas._isMainEvent(options.e)) {
return;
}
this.drawStraightLine = options.e[this.straightLineKey];
if (this.limitedToCanvasSize === true && this._isOutSideCanvas(pointer)) {
return;
}
Expand Down Expand Up @@ -88,6 +110,7 @@
if (!this.canvas._isMainEvent(options.e)) {
return true;
}
this.drawStraightLine = false;
this.oldEnd = undefined;
this._finalizeAndAddPath();
return false;
Expand All @@ -114,6 +137,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 +153,7 @@
this._points = [];
this._setBrushStyles();
this._setShadow();
this._hasStraightLine = false;
},

/**
Expand Down