Browse Source

demo: image selection with no padding

Ryan Di 10 months ago
parent
commit
81e1d61d42

+ 7 - 3
packages/excalidraw/element/resizeTest.ts

@@ -20,7 +20,7 @@ import type { AppState, Device, Zoom } from "../types";
 import type { Bounds } from "./bounds";
 import type { Bounds } from "./bounds";
 import { getElementAbsoluteCoords } from "./bounds";
 import { getElementAbsoluteCoords } from "./bounds";
 import { SIDE_RESIZING_THRESHOLD } from "../constants";
 import { SIDE_RESIZING_THRESHOLD } from "../constants";
-import { isLinearElement } from "./typeChecks";
+import { isImageElement, isLinearElement } from "./typeChecks";
 import type { GlobalPoint, LineSegment, LocalPoint } from "../../math";
 import type { GlobalPoint, LineSegment, LocalPoint } from "../../math";
 import {
 import {
   pointFrom,
   pointFrom,
@@ -90,7 +90,11 @@ export const resizeTest = <Point extends GlobalPoint | LocalPoint>(
 
 
     // do not resize from the sides for linear elements with only two points
     // do not resize from the sides for linear elements with only two points
     if (!(isLinearElement(element) && element.points.length <= 2)) {
     if (!(isLinearElement(element) && element.points.length <= 2)) {
-      const SPACING = SIDE_RESIZING_THRESHOLD / zoom.value;
+      const SPACING = isImageElement(element)
+        ? 0
+        : SIDE_RESIZING_THRESHOLD / zoom.value;
+      const ZOOMED_SIDE_RESIZING_THRESHOLD =
+        SIDE_RESIZING_THRESHOLD / zoom.value;
       const sides = getSelectionBorders(
       const sides = getSelectionBorders(
         pointFrom(x1 - SPACING, y1 - SPACING),
         pointFrom(x1 - SPACING, y1 - SPACING),
         pointFrom(x2 + SPACING, y2 + SPACING),
         pointFrom(x2 + SPACING, y2 + SPACING),
@@ -104,7 +108,7 @@ export const resizeTest = <Point extends GlobalPoint | LocalPoint>(
           pointOnLineSegment(
           pointOnLineSegment(
             pointFrom(x, y),
             pointFrom(x, y),
             side as LineSegment<Point>,
             side as LineSegment<Point>,
-            SPACING,
+            ZOOMED_SIDE_RESIZING_THRESHOLD,
           )
           )
         ) {
         ) {
           return dir as TransformHandleType;
           return dir as TransformHandleType;

+ 8 - 4
packages/excalidraw/element/transformHandles.ts

@@ -11,6 +11,7 @@ import type { Device, InteractiveCanvasAppState, Zoom } from "../types";
 import {
 import {
   isElbowArrow,
   isElbowArrow,
   isFrameLikeElement,
   isFrameLikeElement,
+  isImageElement,
   isLinearElement,
   isLinearElement,
 } from "./typeChecks";
 } from "./typeChecks";
 import {
 import {
@@ -129,6 +130,7 @@ export const getTransformHandlesFromCoords = (
   pointerType: PointerType,
   pointerType: PointerType,
   omitSides: { [T in TransformHandleType]?: boolean } = {},
   omitSides: { [T in TransformHandleType]?: boolean } = {},
   margin = 4,
   margin = 4,
+  spacing = DEFAULT_TRANSFORM_HANDLE_SPACING,
 ): TransformHandles => {
 ): TransformHandles => {
   const size = transformHandleSizes[pointerType];
   const size = transformHandleSizes[pointerType];
   const handleWidth = size / zoom.value;
   const handleWidth = size / zoom.value;
@@ -140,8 +142,7 @@ export const getTransformHandlesFromCoords = (
   const width = x2 - x1;
   const width = x2 - x1;
   const height = y2 - y1;
   const height = y2 - y1;
   const dashedLineMargin = margin / zoom.value;
   const dashedLineMargin = margin / zoom.value;
-  const centeringOffset =
-    (size - DEFAULT_TRANSFORM_HANDLE_SPACING * 2) / (2 * zoom.value);
+  const centeringOffset = (size - spacing * 2) / (2 * zoom.value);
 
 
   const transformHandles: TransformHandles = {
   const transformHandles: TransformHandles = {
     nw: omitSides.nw
     nw: omitSides.nw
@@ -301,8 +302,10 @@ export const getTransformHandles = (
       rotation: true,
       rotation: true,
     };
     };
   }
   }
-  const dashedLineMargin = isLinearElement(element)
+  const margin = isLinearElement(element)
     ? DEFAULT_TRANSFORM_HANDLE_SPACING + 8
     ? DEFAULT_TRANSFORM_HANDLE_SPACING + 8
+    : isImageElement(element)
+    ? 0
     : DEFAULT_TRANSFORM_HANDLE_SPACING;
     : DEFAULT_TRANSFORM_HANDLE_SPACING;
   return getTransformHandlesFromCoords(
   return getTransformHandlesFromCoords(
     getElementAbsoluteCoords(element, elementsMap, true),
     getElementAbsoluteCoords(element, elementsMap, true),
@@ -310,7 +313,8 @@ export const getTransformHandles = (
     zoom,
     zoom,
     pointerType,
     pointerType,
     omitSides,
     omitSides,
-    dashedLineMargin,
+    margin,
+    isImageElement(element) ? 0 : undefined,
   );
   );
 };
 };
 
 

+ 5 - 1
packages/excalidraw/renderer/interactiveScene.ts

@@ -950,7 +950,11 @@ const _renderInteractiveScene = ({
             activeEmbeddable:
             activeEmbeddable:
               appState.activeEmbeddable?.element === element &&
               appState.activeEmbeddable?.element === element &&
               appState.activeEmbeddable.state === "active",
               appState.activeEmbeddable.state === "active",
-            padding: element.id === appState.croppingElementId ? 0 : undefined,
+            padding:
+              element.id === appState.croppingElementId ||
+              isImageElement(element)
+                ? 0
+                : undefined,
           });
           });
         }
         }
       }
       }