bounds.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { ExcalidrawElement } from "./types";
  2. import { rotate } from "../math";
  3. import { Drawable } from "roughjs/bin/core";
  4. import { Point } from "roughjs/bin/geometry";
  5. // If the element is created from right to left, the width is going to be negative
  6. // This set of functions retrieves the absolute position of the 4 points.
  7. export function getElementAbsoluteCoords(element: ExcalidrawElement) {
  8. if (element.type === "arrow" || element.type === "line") {
  9. return getLinearElementAbsoluteBounds(element);
  10. }
  11. return [
  12. element.x,
  13. element.y,
  14. element.x + element.width,
  15. element.y + element.height,
  16. ];
  17. }
  18. export function getDiamondPoints(element: ExcalidrawElement) {
  19. // Here we add +1 to avoid these numbers to be 0
  20. // otherwise rough.js will throw an error complaining about it
  21. const topX = Math.floor(element.width / 2) + 1;
  22. const topY = 0;
  23. const rightX = element.width;
  24. const rightY = Math.floor(element.height / 2) + 1;
  25. const bottomX = topX;
  26. const bottomY = element.height;
  27. const leftX = topY;
  28. const leftY = rightY;
  29. return [topX, topY, rightX, rightY, bottomX, bottomY, leftX, leftY];
  30. }
  31. export function getLinearElementAbsoluteBounds(element: ExcalidrawElement) {
  32. if (element.points.length < 2 || !element.shape) {
  33. const { minX, minY, maxX, maxY } = element.points.reduce(
  34. (limits, [x, y]) => {
  35. limits.minY = Math.min(limits.minY, y);
  36. limits.minX = Math.min(limits.minX, x);
  37. limits.maxX = Math.max(limits.maxX, x);
  38. limits.maxY = Math.max(limits.maxY, y);
  39. return limits;
  40. },
  41. { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity },
  42. );
  43. return [
  44. minX + element.x,
  45. minY + element.y,
  46. maxX + element.x,
  47. maxY + element.y,
  48. ];
  49. }
  50. const shape = element.shape as Drawable[];
  51. // first element is always the curve
  52. const ops = shape[0].sets[0].ops;
  53. let currentP: Point = [0, 0];
  54. const { minX, minY, maxX, maxY } = ops.reduce(
  55. (limits, { op, data }) => {
  56. // There are only four operation types:
  57. // move, bcurveTo, lineTo, and curveTo
  58. if (op === "move") {
  59. // change starting point
  60. currentP = data as Point;
  61. // move operation does not draw anything; so, it always
  62. // returns false
  63. } else if (op === "bcurveTo") {
  64. // create points from bezier curve
  65. // bezier curve stores data as a flattened array of three positions
  66. // [x1, y1, x2, y2, x3, y3]
  67. const p1 = [data[0], data[1]] as Point;
  68. const p2 = [data[2], data[3]] as Point;
  69. const p3 = [data[4], data[5]] as Point;
  70. const p0 = currentP;
  71. currentP = p3;
  72. const equation = (t: number, idx: number) =>
  73. Math.pow(1 - t, 3) * p3[idx] +
  74. 3 * t * Math.pow(1 - t, 2) * p2[idx] +
  75. 3 * Math.pow(t, 2) * (1 - t) * p1[idx] +
  76. p0[idx] * Math.pow(t, 3);
  77. let t = 0;
  78. while (t <= 1.0) {
  79. const x = equation(t, 0);
  80. const y = equation(t, 1);
  81. limits.minY = Math.min(limits.minY, y);
  82. limits.minX = Math.min(limits.minX, x);
  83. limits.maxX = Math.max(limits.maxX, x);
  84. limits.maxY = Math.max(limits.maxY, y);
  85. t += 0.1;
  86. }
  87. } else if (op === "lineTo") {
  88. // TODO: Implement this
  89. } else if (op === "qcurveTo") {
  90. // TODO: Implement this
  91. }
  92. return limits;
  93. },
  94. { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity },
  95. );
  96. return [
  97. minX + element.x,
  98. minY + element.y,
  99. maxX + element.x,
  100. maxY + element.y,
  101. ];
  102. }
  103. export function getArrowPoints(element: ExcalidrawElement) {
  104. const points = element.points;
  105. const [x1, y1] = points.length >= 2 ? points[points.length - 2] : [0, 0];
  106. const [x2, y2] = points[points.length - 1];
  107. const size = 30; // pixels
  108. const distance = Math.hypot(x2 - x1, y2 - y1);
  109. const arrowLength = element.points.reduce((total, [cx, cy], idx, points) => {
  110. const [px, py] = idx > 0 ? points[idx - 1] : [0, 0];
  111. return total + Math.hypot(cx - px, cy - py);
  112. }, 0);
  113. // Scale down the arrow until we hit a certain size so that it doesn't look weird
  114. // This value is selected by minizing a minmum size with the whole length of the arrow
  115. // intead of last segment of the arrow
  116. const minSize = Math.min(size, arrowLength / 2);
  117. const xs = x2 - ((x2 - x1) / distance) * minSize;
  118. const ys = y2 - ((y2 - y1) / distance) * minSize;
  119. const angle = 20; // degrees
  120. const [x3, y3] = rotate(xs, ys, x2, y2, (-angle * Math.PI) / 180);
  121. const [x4, y4] = rotate(xs, ys, x2, y2, (angle * Math.PI) / 180);
  122. return [x2, y2, x3, y3, x4, y4];
  123. }
  124. export function getCommonBounds(elements: readonly ExcalidrawElement[]) {
  125. let minX = Infinity;
  126. let maxX = -Infinity;
  127. let minY = Infinity;
  128. let maxY = -Infinity;
  129. elements.forEach(element => {
  130. const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
  131. minX = Math.min(minX, x1);
  132. minY = Math.min(minY, y1);
  133. maxX = Math.max(maxX, x2);
  134. maxY = Math.max(maxY, y2);
  135. });
  136. return [minX, minY, maxX, maxY];
  137. }