Pārlūkot izejas kodu

perf: Improve arrow head sizing (#3480)

Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
vanilla_orig
Lily Ge 4 gadus atpakaļ
vecāks
revīzija
044614dcf3
Revīzijas autora e-pasta adrese nav piesaistīta nevienam kontam
1 mainītis faili ar 20 papildinājumiem un 6 dzēšanām
  1. 20
    6
      src/element/bounds.ts

+ 20
- 6
src/element/bounds.ts Parādīt failu

@@ -260,14 +260,28 @@ export const getArrowheadPoints = (
260 260
     dot: 15,
261 261
   }[arrowhead]; // pixels (will differ for each arrowhead)
262 262
 
263
-  const length = element.points.reduce((total, [cx, cy], idx, points) => {
264
-    const [px, py] = idx > 0 ? points[idx - 1] : [0, 0];
265
-    return total + Math.hypot(cx - px, cy - py);
266
-  }, 0);
263
+  let length = 0;
264
+
265
+  if (arrowhead === "arrow") {
266
+    // Length for -> arrows is based on the length of the last section
267
+    const [cx, cy] = element.points[element.points.length - 1];
268
+    const [px, py] =
269
+      element.points.length > 1
270
+        ? element.points[element.points.length - 2]
271
+        : [0, 0];
272
+
273
+    length = Math.hypot(cx - px, cy - py);
274
+  } else {
275
+    // Length for other arrowhead types is based on the total length of the line
276
+    for (let i = 0; i < element.points.length; i++) {
277
+      const [px, py] = element.points[i - 1] || [0, 0];
278
+      const [cx, cy] = element.points[i];
279
+      length += Math.hypot(cx - px, cy - py);
280
+    }
281
+  }
267 282
 
268 283
   // Scale down the arrowhead until we hit a certain size so that it doesn't look weird.
269
-  // This value is selected by minimizing a minimum size with the whole length of the
270
-  // arrowhead instead of last segment of the arrowhead.
284
+  // This value is selected by minimizing a minimum size with the last segment of the arrowhead
271 285
   const minSize = Math.min(size, length / 2);
272 286
   const xs = x2 - nx * minSize;
273 287
   const ys = y2 - ny * minSize;

Notiek ielāde…
Atcelt
Saglabāt