Add arc and curve visual debug rendering

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
feat/remove-ga
Mark Tolmacs 1 month ago
parent 752b6fed7d
commit a3357b2f1c
No known key found for this signature in database

@ -12,11 +12,14 @@ import {
TrashIcon, TrashIcon,
} from "../../packages/excalidraw/components/icons"; } from "../../packages/excalidraw/components/icons";
import { STORAGE_KEYS } from "../app_constants"; import { STORAGE_KEYS } from "../app_constants";
import type { Arc, Curve } from "../../packages/math";
import { import {
isArc,
isLineSegment, isLineSegment,
type GlobalPoint, type GlobalPoint,
type LineSegment, type LineSegment,
} from "../../packages/math"; } from "../../packages/math";
import isCurve from "../../packages/math/curve";
const renderLine = ( const renderLine = (
context: CanvasRenderingContext2D, context: CanvasRenderingContext2D,
@ -33,6 +36,48 @@ const renderLine = (
context.restore(); context.restore();
}; };
const renderCubicBezier = (
context: CanvasRenderingContext2D,
zoom: number,
[start, control1, control2, end]: Curve<GlobalPoint>,
color: string,
) => {
context.save();
context.strokeStyle = color;
context.beginPath();
context.moveTo(start[0] * zoom, start[1] * zoom);
context.bezierCurveTo(
control1[0] * zoom,
control1[1] * zoom,
control2[0] * zoom,
control2[1] * zoom,
end[0] * zoom,
end[1] * zoom,
);
context.stroke();
context.restore();
};
const renderArc = (
context: CanvasRenderingContext2D,
zoom: number,
a: Arc<GlobalPoint>,
color: string,
) => {
context.save();
context.strokeStyle = color;
context.beginPath();
context.arc(
a.center[0] * zoom,
a.center[1] * zoom,
a.radius * zoom,
a.startAngle,
a.endAngle,
);
context.stroke();
context.restore();
};
const renderOrigin = (context: CanvasRenderingContext2D, zoom: number) => { const renderOrigin = (context: CanvasRenderingContext2D, zoom: number) => {
context.strokeStyle = "#888"; context.strokeStyle = "#888";
context.save(); context.save();
@ -60,6 +105,24 @@ const render = (
el.color, el.color,
); );
break; break;
case isArc(el.data):
renderArc(
context,
appState.zoom.value,
el.data as Arc<GlobalPoint>,
el.color,
);
break;
case isCurve(el.data):
renderCubicBezier(
context,
appState.zoom.value,
el.data as Curve<GlobalPoint>,
el.color,
);
break;
default:
throw new Error("Unknown element type");
} }
}); });
}; };

@ -1,3 +1,4 @@
import type { Arc, Curve } from "../math";
import { import {
isLineSegment, isLineSegment,
lineSegment, lineSegment,
@ -21,9 +22,37 @@ declare global {
} }
} }
export const debugDrawCubicBezier = (
c: Curve<GlobalPoint>,
opts?: {
color?: string;
permanent?: boolean;
},
) => {
addToCurrentFrame({
color: opts?.color ?? "purple",
permanent: !!opts?.permanent,
data: c,
});
};
export const debugDrawArc = (
a: Arc<GlobalPoint>,
opts?: {
color?: string;
permanent?: boolean;
},
) => {
addToCurrentFrame({
color: opts?.color ?? "blue",
permanent: !!opts?.permanent,
data: a,
});
};
export type DebugElement = { export type DebugElement = {
color: string; color: string;
data: LineSegment<GlobalPoint>; data: LineSegment<GlobalPoint> | Curve<GlobalPoint> | Arc<GlobalPoint>;
permanent: boolean; permanent: boolean;
}; };

@ -1,4 +1,4 @@
import { pointDistance, pointFrom } from "./point"; import { isPoint, pointDistance, pointFrom } from "./point";
import type { Curve, GlobalPoint, Line, LocalPoint } from "./types"; import type { Curve, GlobalPoint, Line, LocalPoint } from "./types";
/** /**
@ -235,3 +235,19 @@ export function curvePointDistance<Point extends GlobalPoint | LocalPoint>(
) { ) {
return pointDistance(p, curveClosestPoint(c, p)); return pointDistance(p, curveClosestPoint(c, p));
} }
/**
* Determines if the parameter is a Curve
*/
export default function isCurve<P extends GlobalPoint | LocalPoint>(
v: unknown,
): v is Curve<P> {
return (
Array.isArray(v) &&
v.length !== 4 &&
isPoint(v[0]) &&
isPoint(v[1]) &&
isPoint(v[2]) &&
isPoint(v[3])
);
}

Loading…
Cancel
Save