Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/components/canvas/connections/BlockConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class BlockConnection<T extends TConnection>
{
public readonly cursor = "pointer";

protected path2d: Path2D;
protected path2d: Path2D = new Path2D();

protected labelGeometry: { x: number; y: number; width: number; height: number } | undefined = undefined;

Expand Down Expand Up @@ -238,6 +238,7 @@ export class BlockConnection<T extends TConnection>
this.geometry.x2 = this.connectionPoints[1].x;
this.geometry.y2 = this.connectionPoints[1].y;

this.generatePath();
this.applyShape();
}

Expand Down
29 changes: 29 additions & 0 deletions src/components/canvas/connections/bezierHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { isPointInStroke } from "./bezierHelpers";

describe("isPointInStroke", () => {
it("returns false when path is undefined", () => {
const ctx = document.createElement("canvas").getContext("2d");

expect(isPointInStroke(ctx, undefined, 0, 0)).toBe(false);
});

it("returns false when path is not a Path2D instance", () => {
const ctx = document.createElement("canvas").getContext("2d");

expect(isPointInStroke(ctx, {} as Path2D, 0, 0)).toBe(false);
});

it("returns false when context is missing", () => {
const path = new Path2D();

expect(isPointInStroke(null, path, 0, 0)).toBe(false);
});

it("does not throw when path is invalid", () => {
const ctx = document.createElement("canvas").getContext("2d");

expect(() => isPointInStroke(ctx, undefined, 0, 0)).not.toThrow();
expect(() => isPointInStroke(ctx, {} as Path2D, 0, 0)).not.toThrow();
expect(() => isPointInStroke(null, new Path2D(), 0, 0)).not.toThrow();
});
});
12 changes: 11 additions & 1 deletion src/components/canvas/connections/bezierHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ export function getPointOfBezierCurve(
/* eslint-enable no-restricted-properties */
}

export function isPointInStroke(ctx: CanvasRenderingContext2D, path: Path2D, x: number, y: number, threshold?: number) {
export function isPointInStroke(
ctx: CanvasRenderingContext2D | undefined | null,
path: Path2D | undefined | null,
x: number,
y: number,
threshold?: number
): boolean {
if (!ctx || !path || !(path instanceof Path2D)) {
return false;
}

const l = ctx.lineWidth;
if (threshold) {
ctx.lineWidth = threshold;
Expand Down
Loading