Compare commits

..

1 Commits

Author SHA1 Message Date
dwelle
c1b903395a wip: hitbox test visualizer 2023-02-15 16:40:10 +01:00
2 changed files with 55 additions and 15 deletions

View File

@@ -744,19 +744,18 @@ export const isHittingContainerStroke = (
); );
const strokeWidth = container.strokeWidth; const strokeWidth = container.strokeWidth;
const threshold = 10 / zoom;
if (container.type === "ellipse") { if (container.type === "ellipse") {
const threshold = 10 * zoom;
const h = (topLeft[0] + topRight[0]) / 2; const h = (topLeft[0] + topRight[0]) / 2;
const k = (topLeft[1] + bottomLeft[1]) / 2; const k = (topLeft[1] + bottomLeft[1]) / 2;
let a = container.width / 2 + strokeWidth / 2 + threshold; let a = container.width / 2 + threshold;
let b = container.height / 2 + strokeWidth / 2 + threshold; let b = container.height / 2 + threshold;
const checkPointOnOuterEllipse = const checkPointOnOuterEllipse =
Math.pow(counterRotateX - h, 2) / Math.pow(a, 2) + Math.pow(counterRotateX - h, 2) / Math.pow(a, 2) +
Math.pow(counterRotateY - k, 2) / Math.pow(b, 2); Math.pow(counterRotateY - k, 2) / Math.pow(b, 2);
a = container.width / 2 - strokeWidth / 2 - threshold; a = container.width / 2 - strokeWidth - threshold;
b = container.height / 2 - strokeWidth / 2 - threshold; b = container.height / 2 - strokeWidth - threshold;
const checkPointOnInnerEllipse = const checkPointOnInnerEllipse =
Math.pow(counterRotateX - h, 2) / Math.pow(a, 2) + Math.pow(counterRotateX - h, 2) / Math.pow(a, 2) +
@@ -773,11 +772,12 @@ export const isHittingContainerStroke = (
} }
return false; return false;
} }
const threshold = 10 / zoom;
// Left Stroke // Left Stroke
if ( if (
counterRotateX >= topLeft[0] - strokeWidth / 2 - threshold && counterRotateX >= topLeft[0] - threshold &&
counterRotateX <= topLeft[0] + strokeWidth / 2 + threshold && counterRotateX <= topLeft[0] + strokeWidth + threshold &&
counterRotateY >= topLeft[1] - threshold && counterRotateY >= topLeft[1] - threshold &&
counterRotateY <= bottomRight[1] + threshold counterRotateY <= bottomRight[1] + threshold
) { ) {
@@ -787,16 +787,16 @@ export const isHittingContainerStroke = (
if ( if (
counterRotateX >= topLeft[0] - threshold && counterRotateX >= topLeft[0] - threshold &&
counterRotateX <= topRight[0] + threshold && counterRotateX <= topRight[0] + threshold &&
counterRotateY >= topLeft[1] - threshold - strokeWidth / 2 && counterRotateY >= topLeft[1] - threshold &&
counterRotateY <= topLeft[1] + threshold + strokeWidth / 2 counterRotateY <= topLeft[1] + threshold + strokeWidth
) { ) {
return true; return true;
} }
// Right stroke // Right stroke
if ( if (
counterRotateX >= topRight[0] - threshold - strokeWidth / 2 && counterRotateX >= topRight[0] - threshold - strokeWidth &&
counterRotateX <= topRight[0] + threshold + strokeWidth / 2 && counterRotateX <= topRight[0] + threshold &&
counterRotateY >= topRight[1] - threshold && counterRotateY >= topRight[1] - threshold &&
counterRotateY <= bottomRight[1] + threshold counterRotateY <= bottomRight[1] + threshold
) { ) {
@@ -807,8 +807,8 @@ export const isHittingContainerStroke = (
if ( if (
counterRotateX >= bottomLeft[0] - threshold && counterRotateX >= bottomLeft[0] - threshold &&
counterRotateX <= bottomRight[0] + threshold && counterRotateX <= bottomRight[0] + threshold &&
counterRotateY >= bottomLeft[1] - threshold - strokeWidth / 2 && counterRotateY >= bottomLeft[1] - threshold - strokeWidth &&
counterRotateY <= bottomLeft[1] + threshold + strokeWidth / 2 counterRotateY <= bottomLeft[1] + threshold
) { ) {
return true; return true;
} }

View File

@@ -37,7 +37,10 @@ import {
getSelectedGroupIds, getSelectedGroupIds,
getElementsInGroup, getElementsInGroup,
} from "../groups"; } from "../groups";
import { maxBindingGap } from "../element/collision"; import {
isHittingElementNotConsideringBoundingBox,
maxBindingGap,
} from "../element/collision";
import { import {
SuggestedBinding, SuggestedBinding,
SuggestedPointBinding, SuggestedPointBinding,
@@ -60,6 +63,8 @@ import {
getLinkHandleFromCoords, getLinkHandleFromCoords,
} from "../element/Hyperlink"; } from "../element/Hyperlink";
import { isLinearElement } from "../element/typeChecks"; import { isLinearElement } from "../element/typeChecks";
import { rotatePoint } from "../math";
import { isHittingContainerStroke } from "../element/textElement";
const hasEmojiSupport = supportsEmoji(); const hasEmojiSupport = supportsEmoji();
export const DEFAULT_SPACING = 2; export const DEFAULT_SPACING = 2;
@@ -407,9 +412,44 @@ export const _renderScene = ({
let editingLinearElement: NonDeleted<ExcalidrawLinearElement> | undefined = let editingLinearElement: NonDeleted<ExcalidrawLinearElement> | undefined =
undefined; undefined;
let idx = -1;
visibleElements.forEach((element) => { visibleElements.forEach((element) => {
idx++;
try { try {
const useProdAlgo = idx % 2 === 0;
context.fillStyle = useProdAlgo ? "lime" : "red";
const padding = 40 / renderConfig.zoom.value;
const bounds = getCommonBounds([element]);
const box = [
bounds[0] + renderConfig.scrollX,
bounds[1] + renderConfig.scrollY,
bounds[2] + renderConfig.scrollX,
bounds[3] + renderConfig.scrollY,
];
for (let x = box[0] - padding; x < box[2] + padding; x++) {
for (let y = box[1] - padding; y < box[3] + padding; y++) {
const sceneX = x - renderConfig.scrollX;
const sceneY = y - renderConfig.scrollY;
if (
useProdAlgo
? isHittingElementNotConsideringBoundingBox(element, appState, [
sceneX,
sceneY,
])
: isHittingContainerStroke(
sceneX,
sceneY,
// @ts-ignore
element,
renderConfig.zoom.value,
)
) {
context.fillRect(x, y, 1, 1);
}
}
}
renderElement(element, rc, context, renderConfig, appState); renderElement(element, rc, context, renderConfig, appState);
// Getting the element using LinearElementEditor during collab mismatches version - being one head of visible elements due to // Getting the element using LinearElementEditor during collab mismatches version - being one head of visible elements due to
// ShapeCache returns empty hence making sure that we get the // ShapeCache returns empty hence making sure that we get the
// correct element from visible elements // correct element from visible elements