123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907 |
- import * as GA from "../ga";
- import * as GAPoint from "../gapoints";
- import * as GADirection from "../gadirections";
- import * as GALine from "../galines";
- import * as GATransform from "../gatransforms";
- import {
- distance2d,
- rotatePoint,
- isPathALoop,
- isPointInPolygon,
- rotate,
- } from "../math";
- import { pointsOnBezierCurves } from "points-on-curve";
- import {
- NonDeletedExcalidrawElement,
- ExcalidrawBindableElement,
- ExcalidrawElement,
- ExcalidrawRectangleElement,
- ExcalidrawDiamondElement,
- ExcalidrawTextElement,
- ExcalidrawEllipseElement,
- NonDeleted,
- ExcalidrawFreeDrawElement,
- ExcalidrawImageElement,
- } from "./types";
- import { getElementAbsoluteCoords, getCurvePathOps, Bounds } from "./bounds";
- import { Point } from "../types";
- import { Drawable } from "roughjs/bin/core";
- import { AppState } from "../types";
- import { getShapeForElement } from "../renderer/renderElement";
- import { isImageElement } from "./typeChecks";
- const isElementDraggableFromInside = (
- element: NonDeletedExcalidrawElement,
- ): boolean => {
- if (element.type === "arrow") {
- return false;
- }
- if (element.type === "freedraw") {
- return true;
- }
- const isDraggableFromInside = element.backgroundColor !== "transparent";
- if (element.type === "line") {
- return isDraggableFromInside && isPathALoop(element.points);
- }
- return isDraggableFromInside || isImageElement(element);
- };
- export const hitTest = (
- element: NonDeletedExcalidrawElement,
- appState: AppState,
- x: number,
- y: number,
- ): boolean => {
-
- const threshold = 10 / appState.zoom.value;
- const point: Point = [x, y];
- if (isElementSelected(appState, element)) {
- return isPointHittingElementBoundingBox(element, point, threshold);
- }
- return isHittingElementNotConsideringBoundingBox(element, appState, point);
- };
- export const isHittingElementBoundingBoxWithoutHittingElement = (
- element: NonDeletedExcalidrawElement,
- appState: AppState,
- x: number,
- y: number,
- ): boolean => {
- const threshold = 10 / appState.zoom.value;
- return (
- !isHittingElementNotConsideringBoundingBox(element, appState, [x, y]) &&
- isPointHittingElementBoundingBox(element, [x, y], threshold)
- );
- };
- export const isHittingElementNotConsideringBoundingBox = (
- element: NonDeletedExcalidrawElement,
- appState: AppState,
- point: Point,
- ): boolean => {
- const threshold = 10 / appState.zoom.value;
- const check =
- element.type === "text"
- ? isStrictlyInside
- : isElementDraggableFromInside(element)
- ? isInsideCheck
- : isNearCheck;
- return hitTestPointAgainstElement({ element, point, threshold, check });
- };
- const isElementSelected = (
- appState: AppState,
- element: NonDeleted<ExcalidrawElement>,
- ) => appState.selectedElementIds[element.id];
- const isPointHittingElementBoundingBox = (
- element: NonDeleted<ExcalidrawElement>,
- [x, y]: Point,
- threshold: number,
- ) => {
- const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
- const elementCenterX = (x1 + x2) / 2;
- const elementCenterY = (y1 + y2) / 2;
-
- const [rotatedX, rotatedY] = rotate(
- x,
- y,
- elementCenterX,
- elementCenterY,
- -element.angle,
- );
- return (
- rotatedX > x1 - threshold &&
- rotatedX < x2 + threshold &&
- rotatedY > y1 - threshold &&
- rotatedY < y2 + threshold
- );
- };
- export const bindingBorderTest = (
- element: NonDeleted<ExcalidrawBindableElement>,
- { x, y }: { x: number; y: number },
- ): boolean => {
- const threshold = maxBindingGap(element, element.width, element.height);
- const check = isOutsideCheck;
- const point: Point = [x, y];
- return hitTestPointAgainstElement({ element, point, threshold, check });
- };
- export const maxBindingGap = (
- element: ExcalidrawElement,
- elementWidth: number,
- elementHeight: number,
- ): number => {
-
- const shapeRatio = element.type === "diamond" ? 1 / Math.sqrt(2) : 1;
- const smallerDimension = shapeRatio * Math.min(elementWidth, elementHeight);
-
- return Math.max(16, Math.min(0.25 * smallerDimension, 32));
- };
- type HitTestArgs = {
- element: NonDeletedExcalidrawElement;
- point: Point;
- threshold: number;
- check: (distance: number, threshold: number) => boolean;
- };
- const hitTestPointAgainstElement = (args: HitTestArgs): boolean => {
- switch (args.element.type) {
- case "rectangle":
- case "image":
- case "text":
- case "diamond":
- case "ellipse":
- const distance = distanceToBindableElement(args.element, args.point);
- return args.check(distance, args.threshold);
- case "freedraw": {
- if (
- !args.check(
- distanceToRectangle(args.element, args.point),
- args.threshold,
- )
- ) {
- return false;
- }
- return hitTestFreeDrawElement(args.element, args.point, args.threshold);
- }
- case "arrow":
- case "line":
- return hitTestLinear(args);
- case "selection":
- console.warn(
- "This should not happen, we need to investigate why it does.",
- );
- return false;
- }
- };
- export const distanceToBindableElement = (
- element: ExcalidrawBindableElement,
- point: Point,
- ): number => {
- switch (element.type) {
- case "rectangle":
- case "image":
- case "text":
- return distanceToRectangle(element, point);
- case "diamond":
- return distanceToDiamond(element, point);
- case "ellipse":
- return distanceToEllipse(element, point);
- }
- };
- const isStrictlyInside = (distance: number, threshold: number): boolean => {
- return distance < 0;
- };
- const isInsideCheck = (distance: number, threshold: number): boolean => {
- return distance < threshold;
- };
- const isNearCheck = (distance: number, threshold: number): boolean => {
- return Math.abs(distance) < threshold;
- };
- const isOutsideCheck = (distance: number, threshold: number): boolean => {
- return 0 <= distance && distance < threshold;
- };
- const distanceToRectangle = (
- element:
- | ExcalidrawRectangleElement
- | ExcalidrawTextElement
- | ExcalidrawFreeDrawElement
- | ExcalidrawImageElement,
- point: Point,
- ): number => {
- const [, pointRel, hwidth, hheight] = pointRelativeToElement(element, point);
- return Math.max(
- GAPoint.distanceToLine(pointRel, GALine.equation(0, 1, -hheight)),
- GAPoint.distanceToLine(pointRel, GALine.equation(1, 0, -hwidth)),
- );
- };
- const distanceToDiamond = (
- element: ExcalidrawDiamondElement,
- point: Point,
- ): number => {
- const [, pointRel, hwidth, hheight] = pointRelativeToElement(element, point);
- const side = GALine.equation(hheight, hwidth, -hheight * hwidth);
- return GAPoint.distanceToLine(pointRel, side);
- };
- const distanceToEllipse = (
- element: ExcalidrawEllipseElement,
- point: Point,
- ): number => {
- const [pointRel, tangent] = ellipseParamsForTest(element, point);
- return -GALine.sign(tangent) * GAPoint.distanceToLine(pointRel, tangent);
- };
- const ellipseParamsForTest = (
- element: ExcalidrawEllipseElement,
- point: Point,
- ): [GA.Point, GA.Line] => {
- const [, pointRel, hwidth, hheight] = pointRelativeToElement(element, point);
- const [px, py] = GAPoint.toTuple(pointRel);
-
- let tx = 0.707;
- let ty = 0.707;
- const a = hwidth;
- const b = hheight;
-
-
- [0, 1, 2, 3].forEach((_) => {
- const xx = a * tx;
- const yy = b * ty;
- const ex = ((a * a - b * b) * tx ** 3) / a;
- const ey = ((b * b - a * a) * ty ** 3) / b;
- const rx = xx - ex;
- const ry = yy - ey;
- const qx = px - ex;
- const qy = py - ey;
- const r = Math.hypot(ry, rx);
- const q = Math.hypot(qy, qx);
- tx = Math.min(1, Math.max(0, ((qx * r) / q + ex) / a));
- ty = Math.min(1, Math.max(0, ((qy * r) / q + ey) / b));
- const t = Math.hypot(ty, tx);
- tx /= t;
- ty /= t;
- });
- const closestPoint = GA.point(a * tx, b * ty);
- const tangent = GALine.orthogonalThrough(pointRel, closestPoint);
- return [pointRel, tangent];
- };
- const hitTestFreeDrawElement = (
- element: ExcalidrawFreeDrawElement,
- point: Point,
- threshold: number,
- ): boolean => {
-
-
-
-
- let x: number;
- let y: number;
- if (element.angle === 0) {
- x = point[0] - element.x;
- y = point[1] - element.y;
- } else {
-
- const [minX, minY, maxX, maxY] = getElementAbsoluteCoords(element);
- const rotatedPoint = rotatePoint(
- point,
- [minX + (maxX - minX) / 2, minY + (maxY - minY) / 2],
- -element.angle,
- );
- x = rotatedPoint[0] - element.x;
- y = rotatedPoint[1] - element.y;
- }
- let [A, B] = element.points;
- let P: readonly [number, number];
-
- if (
- distance2d(A[0], A[1], x, y) < threshold ||
- distance2d(B[0], B[1], x, y) < threshold
- ) {
- return true;
- }
-
- for (let i = 0; i < element.points.length; i++) {
- const delta = [B[0] - A[0], B[1] - A[1]];
- const length = Math.hypot(delta[1], delta[0]);
- const U = [delta[0] / length, delta[1] / length];
- const C = [x - A[0], y - A[1]];
- const d = (C[0] * U[0] + C[1] * U[1]) / Math.hypot(U[1], U[0]);
- P = [A[0] + U[0] * d, A[1] + U[1] * d];
- const da = distance2d(P[0], P[1], A[0], A[1]);
- const db = distance2d(P[0], P[1], B[0], B[1]);
- P = db < da && da > length ? B : da < db && db > length ? A : P;
- if (Math.hypot(y - P[1], x - P[0]) < threshold) {
- return true;
- }
- A = B;
- B = element.points[i + 1];
- }
- return false;
- };
- const hitTestLinear = (args: HitTestArgs): boolean => {
- const { element, threshold } = args;
- if (!getShapeForElement(element)) {
- return false;
- }
- const [point, pointAbs, hwidth, hheight] = pointRelativeToElement(
- args.element,
- args.point,
- );
- const side1 = GALine.equation(0, 1, -hheight);
- const side2 = GALine.equation(1, 0, -hwidth);
- if (
- !isInsideCheck(GAPoint.distanceToLine(pointAbs, side1), threshold) ||
- !isInsideCheck(GAPoint.distanceToLine(pointAbs, side2), threshold)
- ) {
- return false;
- }
- const [relX, relY] = GAPoint.toTuple(point);
- const shape = getShapeForElement(element) as Drawable[];
- if (args.check === isInsideCheck) {
- const hit = shape.some((subshape) =>
- hitTestCurveInside(subshape, relX, relY, element.strokeSharpness),
- );
- if (hit) {
- return true;
- }
- }
-
- return shape.some((subshape) =>
- hitTestRoughShape(subshape, relX, relY, threshold),
- );
- };
- const pointRelativeToElement = (
- element: ExcalidrawElement,
- pointTuple: Point,
- ): [GA.Point, GA.Point, number, number] => {
- const point = GAPoint.from(pointTuple);
- const elementCoords = getElementAbsoluteCoords(element);
- const center = coordsCenter(elementCoords);
-
- const rotate = GATransform.rotation(center, element.angle);
- const pointRotated = GATransform.apply(rotate, point);
- const pointRelToCenter = GA.sub(pointRotated, GADirection.from(center));
- const pointRelToCenterAbs = GAPoint.abs(pointRelToCenter);
- const elementPos = GA.offset(element.x, element.y);
- const pointRelToPos = GA.sub(pointRotated, elementPos);
- const [ax, ay, bx, by] = elementCoords;
- const halfWidth = (bx - ax) / 2;
- const halfHeight = (by - ay) / 2;
- return [pointRelToPos, pointRelToCenterAbs, halfWidth, halfHeight];
- };
- export const pointInAbsoluteCoords = (
- element: ExcalidrawElement,
-
- point: Point,
- ): Point => {
- const [x, y] = point;
- const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
- const cx = (x2 - x1) / 2;
- const cy = (y2 - y1) / 2;
- const [rotatedX, rotatedY] = rotate(x, y, cx, cy, element.angle);
- return [element.x + rotatedX, element.y + rotatedY];
- };
- const relativizationToElementCenter = (
- element: ExcalidrawElement,
- ): GA.Transform => {
- const elementCoords = getElementAbsoluteCoords(element);
- const center = coordsCenter(elementCoords);
-
- const rotate = GATransform.rotation(center, element.angle);
- const translate = GA.reverse(
- GATransform.translation(GADirection.from(center)),
- );
- return GATransform.compose(rotate, translate);
- };
- const coordsCenter = ([ax, ay, bx, by]: Bounds): GA.Point => {
- return GA.point((ax + bx) / 2, (ay + by) / 2);
- };
- export const determineFocusDistance = (
- element: ExcalidrawBindableElement,
-
- a: Point,
-
- b: Point,
- ): number => {
- const relateToCenter = relativizationToElementCenter(element);
- const aRel = GATransform.apply(relateToCenter, GAPoint.from(a));
- const bRel = GATransform.apply(relateToCenter, GAPoint.from(b));
- const line = GALine.through(aRel, bRel);
- const q = element.height / element.width;
- const hwidth = element.width / 2;
- const hheight = element.height / 2;
- const n = line[2];
- const m = line[3];
- const c = line[1];
- const mabs = Math.abs(m);
- const nabs = Math.abs(n);
- switch (element.type) {
- case "rectangle":
- case "image":
- case "text":
- return c / (hwidth * (nabs + q * mabs));
- case "diamond":
- return mabs < nabs ? c / (nabs * hwidth) : c / (mabs * hheight);
- case "ellipse":
- return c / (hwidth * Math.sqrt(n ** 2 + q ** 2 * m ** 2));
- }
- };
- export const determineFocusPoint = (
- element: ExcalidrawBindableElement,
-
-
- focus: number,
- adjecentPoint: Point,
- ): Point => {
- if (focus === 0) {
- const elementCoords = getElementAbsoluteCoords(element);
- const center = coordsCenter(elementCoords);
- return GAPoint.toTuple(center);
- }
- const relateToCenter = relativizationToElementCenter(element);
- const adjecentPointRel = GATransform.apply(
- relateToCenter,
- GAPoint.from(adjecentPoint),
- );
- const reverseRelateToCenter = GA.reverse(relateToCenter);
- let point;
- switch (element.type) {
- case "rectangle":
- case "image":
- case "text":
- case "diamond":
- point = findFocusPointForRectangulars(element, focus, adjecentPointRel);
- break;
- case "ellipse":
- point = findFocusPointForEllipse(element, focus, adjecentPointRel);
- break;
- }
- return GAPoint.toTuple(GATransform.apply(reverseRelateToCenter, point));
- };
- export const intersectElementWithLine = (
- element: ExcalidrawBindableElement,
-
- a: Point,
-
- b: Point,
-
- gap: number = 0,
- ): Point[] => {
- const relateToCenter = relativizationToElementCenter(element);
- const aRel = GATransform.apply(relateToCenter, GAPoint.from(a));
- const bRel = GATransform.apply(relateToCenter, GAPoint.from(b));
- const line = GALine.through(aRel, bRel);
- const reverseRelateToCenter = GA.reverse(relateToCenter);
- const intersections = getSortedElementLineIntersections(
- element,
- line,
- aRel,
- gap,
- );
- return intersections.map((point) =>
- GAPoint.toTuple(GATransform.apply(reverseRelateToCenter, point)),
- );
- };
- const getSortedElementLineIntersections = (
- element: ExcalidrawBindableElement,
-
- line: GA.Line,
-
- nearPoint: GA.Point,
- gap: number = 0,
- ): GA.Point[] => {
- let intersections: GA.Point[];
- switch (element.type) {
- case "rectangle":
- case "image":
- case "text":
- case "diamond":
- const corners = getCorners(element);
- intersections = corners
- .flatMap((point, i) => {
- const edge: [GA.Point, GA.Point] = [point, corners[(i + 1) % 4]];
- return intersectSegment(line, offsetSegment(edge, gap));
- })
- .concat(
- corners.flatMap((point) => getCircleIntersections(point, gap, line)),
- );
- break;
- case "ellipse":
- intersections = getEllipseIntersections(element, gap, line);
- break;
- }
- if (intersections.length < 2) {
-
- return [];
- }
- const sortedIntersections = intersections.sort(
- (i1, i2) =>
- GAPoint.distance(i1, nearPoint) - GAPoint.distance(i2, nearPoint),
- );
- return [
- sortedIntersections[0],
- sortedIntersections[sortedIntersections.length - 1],
- ];
- };
- const getCorners = (
- element:
- | ExcalidrawRectangleElement
- | ExcalidrawImageElement
- | ExcalidrawDiamondElement
- | ExcalidrawTextElement,
- scale: number = 1,
- ): GA.Point[] => {
- const hx = (scale * element.width) / 2;
- const hy = (scale * element.height) / 2;
- switch (element.type) {
- case "rectangle":
- case "image":
- case "text":
- return [
- GA.point(hx, hy),
- GA.point(hx, -hy),
- GA.point(-hx, -hy),
- GA.point(-hx, hy),
- ];
- case "diamond":
- return [
- GA.point(0, hy),
- GA.point(hx, 0),
- GA.point(0, -hy),
- GA.point(-hx, 0),
- ];
- }
- };
- const intersectSegment = (
- line: GA.Line,
- segment: [GA.Point, GA.Point],
- ): GA.Point[] => {
- const [a, b] = segment;
- const aDist = GAPoint.distanceToLine(a, line);
- const bDist = GAPoint.distanceToLine(b, line);
- if (aDist * bDist >= 0) {
-
- return [];
- }
- return [GAPoint.intersect(line, GALine.through(a, b))];
- };
- const offsetSegment = (
- segment: [GA.Point, GA.Point],
- distance: number,
- ): [GA.Point, GA.Point] => {
- const [a, b] = segment;
- const offset = GATransform.translationOrthogonal(
- GADirection.fromTo(a, b),
- distance,
- );
- return [GATransform.apply(offset, a), GATransform.apply(offset, b)];
- };
- const getEllipseIntersections = (
- element: ExcalidrawEllipseElement,
- gap: number,
- line: GA.Line,
- ): GA.Point[] => {
- const a = element.width / 2 + gap;
- const b = element.height / 2 + gap;
- const m = line[2];
- const n = line[3];
- const c = line[1];
- const squares = a * a * m * m + b * b * n * n;
- const discr = squares - c * c;
- if (squares === 0 || discr <= 0) {
- return [];
- }
- const discrRoot = Math.sqrt(discr);
- const xn = -a * a * m * c;
- const yn = -b * b * n * c;
- return [
- GA.point(
- (xn + a * b * n * discrRoot) / squares,
- (yn - a * b * m * discrRoot) / squares,
- ),
- GA.point(
- (xn - a * b * n * discrRoot) / squares,
- (yn + a * b * m * discrRoot) / squares,
- ),
- ];
- };
- export const getCircleIntersections = (
- center: GA.Point,
- radius: number,
- line: GA.Line,
- ): GA.Point[] => {
- if (radius === 0) {
- return GAPoint.distanceToLine(line, center) === 0 ? [center] : [];
- }
- const m = line[2];
- const n = line[3];
- const c = line[1];
- const [a, b] = GAPoint.toTuple(center);
- const r = radius;
- const squares = m * m + n * n;
- const discr = r * r * squares - (m * a + n * b + c) ** 2;
- if (squares === 0 || discr <= 0) {
- return [];
- }
- const discrRoot = Math.sqrt(discr);
- const xn = a * n * n - b * m * n - m * c;
- const yn = b * m * m - a * m * n - n * c;
- return [
- GA.point((xn + n * discrRoot) / squares, (yn - m * discrRoot) / squares),
- GA.point((xn - n * discrRoot) / squares, (yn + m * discrRoot) / squares),
- ];
- };
- export const findFocusPointForEllipse = (
- ellipse: ExcalidrawEllipseElement,
-
-
- relativeDistance: number,
-
-
- point: GA.Point,
- ): GA.Point => {
- const relativeDistanceAbs = Math.abs(relativeDistance);
- const a = (ellipse.width * relativeDistanceAbs) / 2;
- const b = (ellipse.height * relativeDistanceAbs) / 2;
- const orientation = Math.sign(relativeDistance);
- const [px, pyo] = GAPoint.toTuple(point);
-
- const py = pyo === 0 ? 0.0001 : pyo;
- const squares = px ** 2 * b ** 2 + py ** 2 * a ** 2;
-
- const m =
- (-px * b ** 2 +
- orientation * py * Math.sqrt(Math.max(0, squares - a ** 2 * b ** 2))) /
- squares;
- const n = (-m * px - 1) / py;
- const x = -(a ** 2 * m) / (n ** 2 * b ** 2 + m ** 2 * a ** 2);
- return GA.point(x, (-m * x - 1) / n);
- };
- export const findFocusPointForRectangulars = (
- element:
- | ExcalidrawRectangleElement
- | ExcalidrawImageElement
- | ExcalidrawDiamondElement
- | ExcalidrawTextElement,
-
-
- relativeDistance: number,
-
-
- point: GA.Point,
- ): GA.Point => {
- const relativeDistanceAbs = Math.abs(relativeDistance);
- const orientation = Math.sign(relativeDistance);
- const corners = getCorners(element, relativeDistanceAbs);
- let maxDistance = 0;
- let tangentPoint: null | GA.Point = null;
- corners.forEach((corner) => {
- const distance = orientation * GALine.through(point, corner)[1];
- if (distance > maxDistance) {
- maxDistance = distance;
- tangentPoint = corner;
- }
- });
- return tangentPoint!;
- };
- const pointInBezierEquation = (
- p0: Point,
- p1: Point,
- p2: Point,
- p3: Point,
- [mx, my]: Point,
- lineThreshold: number,
- ) => {
-
- const equation = (t: number, idx: number) =>
- Math.pow(1 - t, 3) * p3[idx] +
- 3 * t * Math.pow(1 - t, 2) * p2[idx] +
- 3 * Math.pow(t, 2) * (1 - t) * p1[idx] +
- p0[idx] * Math.pow(t, 3);
-
- let t = 0;
- while (t <= 1.0) {
- const tx = equation(t, 0);
- const ty = equation(t, 1);
- const diff = Math.sqrt(Math.pow(tx - mx, 2) + Math.pow(ty - my, 2));
- if (diff < lineThreshold) {
- return true;
- }
- t += 0.01;
- }
- return false;
- };
- const hitTestCurveInside = (
- drawable: Drawable,
- x: number,
- y: number,
- sharpness: ExcalidrawElement["strokeSharpness"],
- ) => {
- const ops = getCurvePathOps(drawable);
- const points: Point[] = [];
- let odd = false;
- for (const operation of ops) {
- if (operation.op === "move") {
- odd = !odd;
- if (odd) {
- points.push([operation.data[0], operation.data[1]]);
- }
- } else if (operation.op === "bcurveTo") {
- if (odd) {
- points.push([operation.data[0], operation.data[1]]);
- points.push([operation.data[2], operation.data[3]]);
- points.push([operation.data[4], operation.data[5]]);
- }
- }
- }
- if (points.length >= 4) {
- if (sharpness === "sharp") {
- return isPointInPolygon(points, x, y);
- }
- const polygonPoints = pointsOnBezierCurves(points as any, 10, 5);
- return isPointInPolygon(polygonPoints, x, y);
- }
- return false;
- };
- const hitTestRoughShape = (
- drawable: Drawable,
- x: number,
- y: number,
- lineThreshold: number,
- ) => {
-
- const ops = getCurvePathOps(drawable);
-
-
- let currentP: Point = [0, 0];
- return ops.some(({ op, data }, idx) => {
-
-
- if (op === "move") {
-
- currentP = data as unknown as Point;
-
-
- } else if (op === "bcurveTo") {
-
-
-
- const p1 = [data[0], data[1]] as Point;
- const p2 = [data[2], data[3]] as Point;
- const p3 = [data[4], data[5]] as Point;
- const p0 = currentP;
- currentP = p3;
-
-
-
- const retVal = pointInBezierEquation(
- p0,
- p1,
- p2,
- p3,
- [x, y],
- lineThreshold,
- );
-
-
-
- return retVal;
- } else if (op === "lineTo") {
-
- } else if (op === "qcurveTo") {
-
- }
- return false;
- });
- };
|