mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-30 17:11:01 +03:00
* Initial commit * Segments detection * Remove aspect ratio * Refactor * Fix tests * Toggle shapeSnap * Fix linting * debug: live preview * Rebase fixes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * Bind to elements (no hover background) * Shape closed * Fix rad calc + rename segments * Comments * Fix binding highlighting + lints * Rename to convert to shape * fix: Binding Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Refactor spline simplification Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Remove unused doBoundsIntersect Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Restore type signature Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Propose new button shapes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Remove unused types Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Icon shapes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Element cloning Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Element cloning Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Remove unused Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Shape settings in recognition mode Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Remove debug Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Actions Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Lint Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Arrowheads for freedraw Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Draw shape tool instead of freedraw Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Test Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Lint Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Keyboard shortcut visibility Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Unique cursor for draw shape Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Remove selection Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Shape selection Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Improve shape recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Further refine shape recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Preview Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Add hint Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Refined shape recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Less ellipse more rectanguloid Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Refined arrow recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Refinerecognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Lower linear threshold Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Rectangle head arrow Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: More arrowhead sizes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Arrow remains an arrow Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Draw line between elements converts to arrow Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * remove pendingDrawShapeElement * fix: Arrow start point binding Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Different arrow endpoint heuristic Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Point type Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Various small fixes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Avoid recompute Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Type issues Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * test(editor): cover the drawShape (snap-to-shape) tool The drawShape tool had no dedicated tests, which let master's toolbar / active-tool rewrite silently break it during the merge: actionFinalize reverted to the selection tool after every sketch, because the drawShape flow finalizes without a `newElement` and so failed master's new `&& element` guard. Covers the behavior the merge had to preserve: - sketch recognition (rectangle, ellipse) and the too-small no-op - the tool stays active after finalizing, so shapes can be sketched consecutively (regression guard for the above) - activation via the S shortcut and the extra-tools dropdown - the tool's own cursor Points are fed into the trail directly rather than through pointermove, which is rAF-throttled and would drop all but one point per frame — the same approach lasso.test.tsx uses. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: Add test Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Breakout from App.tsx Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Shortcut is now Shift+X Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: No selecting arrows for shape recog Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Double history Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: PCA + feature extraction recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * reduce and normalize min-size gates * never select newElement when shape recog tool active * simplify lifecycle & remove double finalize * make arrows bindings orbit * funnel though actionFinalize & fix two unrelated cleanup cases * use crosshair cursor * show strokeWidth * switch icons for drawShape & extraTools * dedupe getBoundsFromPoints * remove unused helpers * jsdoc cleanup * dynamic trail size * improve rectangle recognition * disallow linears to deviate too much from a straight line * put under freedraw tool on tablet/mobile * tweak label * rename `drawShape` -> `autoshape` --------- Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com> Co-authored-by: Mark Tolmacs <mark@lazycat.hu> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
379 lines
9.3 KiB
TypeScript
379 lines
9.3 KiB
TypeScript
import * as m from "./math";
|
|
import { douglasPeucker } from "./simplify";
|
|
|
|
import type { Point } from "./math";
|
|
|
|
export type SizeMappingDetails = {
|
|
pressure: number;
|
|
runningLength: number;
|
|
currentIndex: number;
|
|
totalLength: number;
|
|
};
|
|
|
|
export type LaserPointerOptions = {
|
|
/** @default 2 */
|
|
size: number;
|
|
|
|
streamline: number;
|
|
simplify: number;
|
|
simplifyPhase: "tail" | "output" | "input";
|
|
|
|
keepHead: boolean;
|
|
|
|
sizeMapping: (details: SizeMappingDetails) => number;
|
|
};
|
|
|
|
export class LaserPointer {
|
|
static defaults: LaserPointerOptions = {
|
|
size: 2,
|
|
streamline: 0.45,
|
|
simplify: 0.1,
|
|
simplifyPhase: "output",
|
|
keepHead: false,
|
|
|
|
sizeMapping: () => 1,
|
|
};
|
|
|
|
static constants = {
|
|
cornerDetectionMaxAngle: 75,
|
|
cornerDetectionVariance: (s: number) => (s > 35 ? 0.5 : 1),
|
|
maxTailLength: 50,
|
|
};
|
|
|
|
options: LaserPointerOptions;
|
|
constructor(options: Partial<LaserPointerOptions>) {
|
|
this.options = Object.assign({}, LaserPointer.defaults, options);
|
|
}
|
|
|
|
originalPoints: Point[] = [];
|
|
|
|
private stablePoints: Point[] = [];
|
|
private tailPoints: Point[] = [];
|
|
|
|
private isFresh = true;
|
|
|
|
private get lastPoint(): Point {
|
|
return (
|
|
this.tailPoints[this.tailPoints.length - 1] ??
|
|
this.stablePoints[this.stablePoints.length - 1]
|
|
);
|
|
}
|
|
|
|
addPoint(point: Point) {
|
|
const lastPoint = this.originalPoints[this.originalPoints.length - 1];
|
|
|
|
if (lastPoint && lastPoint[0] === point[0] && lastPoint[1] === point[1]) {
|
|
return;
|
|
}
|
|
|
|
this.originalPoints.push(point);
|
|
|
|
if (this.isFresh) {
|
|
this.isFresh = false;
|
|
this.stablePoints.push(point);
|
|
return;
|
|
}
|
|
|
|
if (this.options.streamline > 0) {
|
|
point = m.plerp(this.lastPoint, point, 1 - this.options.streamline);
|
|
}
|
|
|
|
this.tailPoints.push(point);
|
|
|
|
if (m.runLength(this.tailPoints) > LaserPointer.constants.maxTailLength) {
|
|
this.stabilizeTail();
|
|
}
|
|
}
|
|
|
|
close() {
|
|
this.stabilizeTail();
|
|
}
|
|
|
|
stabilizeTail() {
|
|
if (this.options.simplify > 0 && this.options.simplifyPhase === "tail") {
|
|
throw new Error("Not implemented yet");
|
|
} else {
|
|
this.stablePoints.push(...this.tailPoints);
|
|
this.tailPoints = [];
|
|
}
|
|
}
|
|
|
|
private getSize(
|
|
sizeOverride: number | undefined,
|
|
pressure: number,
|
|
index: number,
|
|
totalLength: number,
|
|
runningLength: number,
|
|
) {
|
|
return (
|
|
(sizeOverride ?? this.options.size) *
|
|
this.options.sizeMapping({
|
|
pressure,
|
|
runningLength,
|
|
currentIndex: index,
|
|
totalLength,
|
|
})
|
|
);
|
|
}
|
|
|
|
getStrokeOutline(sizeOverride?: number | undefined): Point[] {
|
|
if (this.isFresh) {
|
|
return [];
|
|
}
|
|
|
|
let points = [...this.stablePoints, ...this.tailPoints];
|
|
|
|
if (this.options.simplify > 0 && this.options.simplifyPhase === "input") {
|
|
points = douglasPeucker(points, this.options.simplify);
|
|
}
|
|
|
|
const len = points.length;
|
|
|
|
if (len === 0) {
|
|
return [];
|
|
}
|
|
|
|
if (len === 1) {
|
|
const c = points[0];
|
|
|
|
const size = this.getSize(sizeOverride, c[2], 0, len, 0);
|
|
|
|
if (size < 0.5) {
|
|
return [];
|
|
}
|
|
|
|
const ps: Point[] = [];
|
|
|
|
for (let theta = 0; theta <= Math.PI * 2; theta += Math.PI / 16) {
|
|
ps.push(m.add(c, m.smul(m.rot([1, 0, 0] as Point, theta), size)));
|
|
}
|
|
|
|
ps.push(
|
|
m.add(
|
|
c,
|
|
m.smul(
|
|
[1, 0, 0] as Point,
|
|
this.getSize(sizeOverride, c[2], 0, len, 0),
|
|
),
|
|
),
|
|
);
|
|
|
|
return ps;
|
|
}
|
|
|
|
if (len === 2) {
|
|
const c = points[0];
|
|
const n = points[1];
|
|
|
|
const cSize = this.getSize(sizeOverride, c[2], 0, len, 0);
|
|
const nSize = this.getSize(sizeOverride, n[2], 0, len, 0);
|
|
|
|
if (cSize < 0.5 || nSize < 0.5) {
|
|
return [];
|
|
}
|
|
|
|
const ps: Point[] = [];
|
|
|
|
const pAngle = m.angle(c, [c[0], c[1] - 100, c[2]] as Point, n);
|
|
|
|
for (
|
|
let theta = pAngle;
|
|
theta <= Math.PI + pAngle;
|
|
theta += Math.PI / 16
|
|
) {
|
|
ps.push(m.add(c, m.smul(m.rot([1, 0, 0] as Point, theta), cSize)));
|
|
}
|
|
|
|
for (
|
|
let theta = Math.PI + pAngle;
|
|
theta <= Math.PI * 2 + pAngle;
|
|
theta += Math.PI / 16
|
|
) {
|
|
ps.push(m.add(n, m.smul(m.rot([1, 0, 0] as Point, theta), nSize)));
|
|
}
|
|
|
|
ps.push(ps[0]);
|
|
|
|
return ps;
|
|
}
|
|
|
|
const forwardPoints: Point[] = [];
|
|
const backwardPoints: Point[] = [];
|
|
|
|
let speed = 0;
|
|
let prevSpeed = 0;
|
|
|
|
let visibleStartIndex = 0;
|
|
let runningLength = 0;
|
|
|
|
for (let i = 1; i < len - 1; i++) {
|
|
const p = points[i - 1];
|
|
const c = points[i];
|
|
const n = points[i + 1];
|
|
|
|
const pressure = c[2];
|
|
|
|
const d = m.dist(p, c);
|
|
runningLength += d;
|
|
speed = prevSpeed + (d - prevSpeed) * 0.2;
|
|
|
|
const cSize = this.getSize(sizeOverride, pressure, i, len, runningLength);
|
|
|
|
if (cSize === 0) {
|
|
visibleStartIndex = i + 1;
|
|
continue;
|
|
}
|
|
|
|
const dirPC = m.norm(m.sub(p, c));
|
|
const dirNC = m.norm(m.sub(n, c));
|
|
const p1dirPC = m.rot(dirPC, Math.PI / 2);
|
|
const p2dirPC = m.rot(dirPC, -Math.PI / 2);
|
|
const p1dirNC = m.rot(dirNC, Math.PI / 2);
|
|
const p2dirNC = m.rot(dirNC, -Math.PI / 2);
|
|
|
|
const p1PC = m.add(c, m.smul(p1dirPC, cSize));
|
|
const p2PC = m.add(c, m.smul(p2dirPC, cSize));
|
|
const p1NC = m.add(c, m.smul(p1dirNC, cSize));
|
|
const p2NC = m.add(c, m.smul(p2dirNC, cSize));
|
|
|
|
const ftdir = m.add(p1dirPC, p2dirNC);
|
|
const btdir = m.add(p2dirPC, p1dirNC);
|
|
|
|
const paPC = m.add(
|
|
c,
|
|
m.smul(m.mag(ftdir) === 0 ? dirPC : m.norm(ftdir), cSize),
|
|
);
|
|
const paNC = m.add(
|
|
c,
|
|
m.smul(m.mag(btdir) === 0 ? dirNC : m.norm(btdir), cSize),
|
|
);
|
|
|
|
const cAngle = m.normAngle(m.angle(c, p, n));
|
|
const D_ANGLE =
|
|
(LaserPointer.constants.cornerDetectionMaxAngle / 180) *
|
|
Math.PI *
|
|
LaserPointer.constants.cornerDetectionVariance(speed);
|
|
|
|
if (Math.abs(cAngle) < D_ANGLE) {
|
|
const tAngle = Math.abs(m.normAngle(Math.PI - cAngle)); // turn angle
|
|
|
|
if (tAngle === 0) {
|
|
continue;
|
|
}
|
|
|
|
if (cAngle < 0) {
|
|
backwardPoints.push(p2PC, paNC);
|
|
|
|
for (let theta = 0; theta <= tAngle; theta += tAngle / 4) {
|
|
forwardPoints.push(m.add(c, m.rot(m.smul(p1dirPC, cSize), theta)));
|
|
}
|
|
|
|
for (let theta = tAngle; theta >= 0; theta -= tAngle / 4) {
|
|
backwardPoints.push(m.add(c, m.rot(m.smul(p1dirPC, cSize), theta)));
|
|
}
|
|
|
|
backwardPoints.push(paNC, p1NC);
|
|
} else {
|
|
forwardPoints.push(p1PC, paPC);
|
|
|
|
for (let theta = 0; theta <= tAngle; theta += tAngle / 4) {
|
|
backwardPoints.push(
|
|
m.add(c, m.rot(m.smul(p1dirPC, -cSize), -theta)),
|
|
);
|
|
}
|
|
|
|
for (let theta = tAngle; theta >= 0; theta -= tAngle / 4) {
|
|
forwardPoints.push(
|
|
m.add(c, m.rot(m.smul(p1dirPC, -cSize), -theta)),
|
|
);
|
|
}
|
|
forwardPoints.push(paPC, p2NC);
|
|
}
|
|
} else {
|
|
forwardPoints.push(paPC);
|
|
backwardPoints.push(paNC);
|
|
}
|
|
|
|
prevSpeed = speed;
|
|
}
|
|
|
|
if (visibleStartIndex >= len - 2) {
|
|
if (this.options.keepHead) {
|
|
const c = points[len - 1];
|
|
|
|
const ps: Point[] = [];
|
|
|
|
for (let theta = 0; theta <= Math.PI * 2; theta += Math.PI / 16) {
|
|
ps.push(
|
|
m.add(
|
|
c,
|
|
m.smul(m.rot([1, 0, 0] as Point, theta), this.options.size),
|
|
),
|
|
);
|
|
}
|
|
|
|
ps.push(m.add(c, m.smul([1, 0, 0] as Point, this.options.size)));
|
|
|
|
return ps;
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
const first = points[visibleStartIndex];
|
|
const second = points[visibleStartIndex + 1];
|
|
const penultimate = points[len - 2];
|
|
const ultimate = points[len - 1];
|
|
|
|
const dirFS = m.norm(m.sub(second, first));
|
|
const dirPU = m.norm(m.sub(penultimate, ultimate));
|
|
|
|
const ppdirFS = m.rot(dirFS, -Math.PI / 2);
|
|
const ppdirPU = m.rot(dirPU, Math.PI / 2);
|
|
|
|
const startCapSize = this.getSize(sizeOverride, first[2], 0, len, 0);
|
|
const startCap: Point[] = [];
|
|
|
|
const endCapSize = this.options.keepHead
|
|
? this.options.size
|
|
: this.getSize(sizeOverride, penultimate[2], len - 2, len, runningLength);
|
|
|
|
const endCap: Point[] = [];
|
|
|
|
// Lowered threshold to 0.1,
|
|
// ensuring virtually all strokes get proper rounded caps for visual consistency.
|
|
if (startCapSize > 0.1) {
|
|
for (let theta = 0; theta <= Math.PI; theta += Math.PI / 16) {
|
|
startCap.unshift(
|
|
m.add(first, m.rot(m.smul(ppdirFS, startCapSize), -theta)),
|
|
);
|
|
}
|
|
|
|
startCap.unshift(m.add(first, m.smul(ppdirFS, -startCapSize)));
|
|
} else {
|
|
startCap.push(first);
|
|
}
|
|
|
|
for (let theta = 0; theta <= Math.PI * 3; theta += Math.PI / 16) {
|
|
endCap.push(m.add(ultimate, m.rot(m.smul(ppdirPU, -endCapSize), -theta)));
|
|
}
|
|
|
|
const strokeOutline = [
|
|
...startCap,
|
|
...forwardPoints,
|
|
...endCap.reverse(),
|
|
...backwardPoints.reverse(),
|
|
];
|
|
|
|
if (startCap.length > 0) {
|
|
strokeOutline.push(startCap[0]);
|
|
}
|
|
|
|
if (this.options.simplify > 0 && this.options.simplifyPhase === "output") {
|
|
return douglasPeucker(strokeOutline, this.options.simplify);
|
|
}
|
|
|
|
return strokeOutline;
|
|
}
|
|
}
|