Skip to content

Commit

Permalink
Fix line drawing issue: Prevent selection of previous drawings, imple…
Browse files Browse the repository at this point in the history
…ment straight line drawing with Ctrl/Shift, and fix deletion issues.
  • Loading branch information
siddharthisrani committed Jul 25, 2024
1 parent 07c58aa commit 0e7fe60
Showing 1 changed file with 55 additions and 34 deletions.
89 changes: 55 additions & 34 deletions src/display/editor/ink.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class InkEditor extends AnnotationEditor {

static _editorType = AnnotationEditorType.INK;

#isDrawingStraightLine = false;

constructor(params) {
super({ ...params, name: "inkEditor" });
this.color = params.color || null;
Expand Down Expand Up @@ -393,6 +395,8 @@ class InkEditor extends AnnotationEditor {
this.opacity ??= InkEditor._defaultOpacity;
}
this.currentPath.push([x, y]);
this.#currentPath2D = new Path2D();
this.#currentPath2D.moveTo(x, y);
this.#hasSomethingToDraw = false;
this.#setStroke();

Expand All @@ -411,34 +415,33 @@ class InkEditor extends AnnotationEditor {
* @param {number} y
*/
#draw(x, y) {
const [lastX, lastY] = this.currentPath.at(-1);
if (this.currentPath.length > 1 && x === lastX && y === lastY) {
return;
}
const currentPath = this.currentPath;
let path2D = this.#currentPath2D;
currentPath.push([x, y]);
this.#hasSomethingToDraw = true;

Check failure on line 418 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Delete `⏎`
if (currentPath.length <= 2) {
path2D.moveTo(...currentPath[0]);
path2D.lineTo(x, y);
return;
}

if (currentPath.length === 3) {
this.#currentPath2D = path2D = new Path2D();
path2D.moveTo(...currentPath[0]);
if (this.#isDrawingStraightLine) {
const [startX, startY] = this.currentPath[0];
this.currentPath = [[startX, startY], [x, y]];

Check failure on line 421 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Replace `[startX,·startY],·[x,·y]` with `⏎········[startX,·startY],⏎········[x,·y],⏎······`
this.#currentPath2D = new Path2D();
this.#currentPath2D.moveTo(startX, startY);
this.#currentPath2D.lineTo(x, y);
} else {

Check failure on line 425 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Delete `·`
const [lastX, lastY] = this.currentPath.at(-1);
if (this.currentPath.length > 1 && x === lastX && y === lastY) {
return;
}
this.currentPath.push([x, y]);
if (this.currentPath.length > 2) {
this.#makeBezierCurve(
this.#currentPath2D,
...this.currentPath.at(-3),
...this.currentPath.at(-2),
x,
y
);
} else {
this.#currentPath2D.lineTo(x, y);
}
}

this.#makeBezierCurve(
path2D,
...currentPath.at(-3),
...currentPath.at(-2),
x,
y
);
}
this.#hasSomethingToDraw = true;
}

Check failure on line 444 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Insert `··`

#endPath() {

Check failure on line 446 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

'#endPath' is defined but never used
if (this.currentPath.length === 0) {
Expand All @@ -460,8 +463,8 @@ class InkEditor extends AnnotationEditor {
y = Math.min(Math.max(y, 0), this.canvas.height);

this.#draw(x, y);
this.#endPath();

// this.#endPath();
// this.#isDrawingStraightLine = false;
// Interpolate the path entered by the user with some
// Bezier's curves in order to have a smoother path and
// to reduce the data size used to draw it in the PDF.
Expand All @@ -470,8 +473,8 @@ class InkEditor extends AnnotationEditor {
bezier = this.#generateBezierPoints();
} else {
// We have only one point finally.
const xy = [x, y];
bezier = [[xy, xy.slice(), xy.slice(), xy]];
const [x, y] = this.currentPath[0];

Check failure on line 476 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

'x' is already declared in the upper scope on line 459 column 16

Check failure on line 476 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

'y' is already declared in the upper scope on line 459 column 19
bezier = [[[x, y], [x, y], [x, y], [x, y]]];

Check failure on line 477 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Replace `[[x,·y],·[x,·y],·[x,·y],·[x,·y]]` with `⏎········[⏎··········[x,·y],⏎··········[x,·y],⏎··········[x,·y],⏎··········[x,·y],⏎········],⏎······`
}
const path2D = this.#currentPath2D;
const currentPath = this.currentPath;
Expand Down Expand Up @@ -501,6 +504,10 @@ class InkEditor extends AnnotationEditor {
};

this.addCommands({ cmd, undo, mustExec: true });
this.#isDrawingStraightLine = false;

Check failure on line 508 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Delete `····`
// Re-enable selection

Check failure on line 509 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Delete `·`
setTimeout(() => this.parent.setEditingState(true), 0);
}

#drawPoints() {
Expand Down Expand Up @@ -557,8 +564,18 @@ class InkEditor extends AnnotationEditor {

#generateBezierPoints() {
const path = this.currentPath;
if (path.length <= 2) {
return [[path[0], path[0], path.at(-1), path.at(-1)]];

if (!path || path.length === 0) {
return [];
}

if (path.length === 1) {
// If we only have one point, duplicate it to create a tiny line
return [[path[0], path[0], path[0], path[0]]];
}

if (path.length === 2 && this.#isDrawingStraightLine) {
return [[path[0], path[0], path[1], path[1]]];
}

const bezierPoints = [];
Expand All @@ -581,6 +598,7 @@ class InkEditor extends AnnotationEditor {
[x0, y0] = [x3, y3];
}

if (path.length > 2) {
const [x1, y1] = path[i];
const [x2, y2] = path[i + 1];

Expand All @@ -589,6 +607,7 @@ class InkEditor extends AnnotationEditor {
const control2 = [x2 + (2 * (x1 - x2)) / 3, y2 + (2 * (y1 - y2)) / 3];

bezierPoints.push([[x0, y0], control1, control2, [x2, y2]]);
}
return bezierPoints;
}

Expand Down Expand Up @@ -664,16 +683,18 @@ class InkEditor extends AnnotationEditor {

// We want to draw on top of any other editors.
// Since it's the last child, there's no need to give it a higher z-index.
this.setInForeground();
// this.setInForeground();

event.preventDefault();
event.stopPropagation();

if (!this.div.contains(document.activeElement)) {
this.div.focus({
preventScroll: true /* See issue #17327 */,
});
}

this.#isDrawingStraightLine = event.shiftKey || event.ctrlKey;
this.parent.setEditingState(false);
this.#startDrawing(event.offsetX, event.offsetY);
}

Expand Down

0 comments on commit 0e7fe60

Please sign in to comment.