Sfoglia il codice sorgente

perf: Improve arrow head sizing (#3480)

Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
vanilla_orig
Lily Ge 4 anni fa
parent
commit
044614dcf3
Nessun account collegato all'indirizzo email del committer
1 ha cambiato i file con 20 aggiunte e 6 eliminazioni
  1. 20
    6
      src/element/bounds.ts

+ 20
- 6
src/element/bounds.ts Vedi File

260
     dot: 15,
260
     dot: 15,
261
   }[arrowhead]; // pixels (will differ for each arrowhead)
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
   // Scale down the arrowhead until we hit a certain size so that it doesn't look weird.
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
   const minSize = Math.min(size, length / 2);
285
   const minSize = Math.min(size, length / 2);
272
   const xs = x2 - nx * minSize;
286
   const xs = x2 - nx * minSize;
273
   const ys = y2 - ny * minSize;
287
   const ys = y2 - ny * minSize;

Loading…
Annulla
Salva