bounds.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { ExcalidrawElement, ExcalidrawLinearElement } from "./types";
  2. import { rotate } from "../math";
  3. import { Drawable } from "roughjs/bin/core";
  4. import { Point } from "../types";
  5. import { getShapeForElement } from "../renderer/renderElement";
  6. import { isLinearElement } from "./typeChecks";
  7. // If the element is created from right to left, the width is going to be negative
  8. // This set of functions retrieves the absolute position of the 4 points.
  9. export function getElementAbsoluteCoords(
  10. element: ExcalidrawElement,
  11. ): [number, number, number, number] {
  12. if (isLinearElement(element)) {
  13. return getLinearElementAbsoluteBounds(element);
  14. }
  15. return [
  16. element.x,
  17. element.y,
  18. element.x + element.width,
  19. element.y + element.height,
  20. ];
  21. }
  22. export function getDiamondPoints(element: ExcalidrawElement) {
  23. // Here we add +1 to avoid these numbers to be 0
  24. // otherwise rough.js will throw an error complaining about it
  25. const topX = Math.floor(element.width / 2) + 1;
  26. const topY = 0;
  27. const rightX = element.width;
  28. const rightY = Math.floor(element.height / 2) + 1;
  29. const bottomX = topX;
  30. const bottomY = element.height;
  31. const leftX = topY;
  32. const leftY = rightY;
  33. return [topX, topY, rightX, rightY, bottomX, bottomY, leftX, leftY];
  34. }
  35. export function getLinearElementAbsoluteBounds(
  36. element: ExcalidrawLinearElement,
  37. ): [number, number, number, number] {
  38. if (element.points.length < 2 || !getShapeForElement(element)) {
  39. const { minX, minY, maxX, maxY } = element.points.reduce(
  40. (limits, [x, y]) => {
  41. limits.minY = Math.min(limits.minY, y);
  42. limits.minX = Math.min(limits.minX, x);
  43. limits.maxX = Math.max(limits.maxX, x);
  44. limits.maxY = Math.max(limits.maxY, y);
  45. return limits;
  46. },
  47. { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity },
  48. );
  49. return [
  50. minX + element.x,
  51. minY + element.y,
  52. maxX + element.x,
  53. maxY + element.y,
  54. ];
  55. }
  56. const shape = getShapeForElement(element) as Drawable[];
  57. // first element is always the curve
  58. const ops = shape[0].sets[0].ops;
  59. let currentP: Point = [0, 0];
  60. const { minX, minY, maxX, maxY } = ops.reduce(
  61. (limits, { op, data }) => {
  62. // There are only four operation types:
  63. // move, bcurveTo, lineTo, and curveTo
  64. if (op === "move") {
  65. // change starting point
  66. currentP = (data as unknown) as Point;
  67. // move operation does not draw anything; so, it always
  68. // returns false
  69. } else if (op === "bcurveTo") {
  70. // create points from bezier curve
  71. // bezier curve stores data as a flattened array of three positions
  72. // [x1, y1, x2, y2, x3, y3]
  73. const p1 = [data[0], data[1]] as Point;
  74. const p2 = [data[2], data[3]] as Point;
  75. const p3 = [data[4], data[5]] as Point;
  76. const p0 = currentP;
  77. currentP = p3;
  78. const equation = (t: number, idx: number) =>
  79. Math.pow(1 - t, 3) * p3[idx] +
  80. 3 * t * Math.pow(1 - t, 2) * p2[idx] +
  81. 3 * Math.pow(t, 2) * (1 - t) * p1[idx] +
  82. p0[idx] * Math.pow(t, 3);
  83. let t = 0;
  84. while (t <= 1.0) {
  85. const x = equation(t, 0);
  86. const y = equation(t, 1);
  87. limits.minY = Math.min(limits.minY, y);
  88. limits.minX = Math.min(limits.minX, x);
  89. limits.maxX = Math.max(limits.maxX, x);
  90. limits.maxY = Math.max(limits.maxY, y);
  91. t += 0.1;
  92. }
  93. } else if (op === "lineTo") {
  94. // TODO: Implement this
  95. } else if (op === "qcurveTo") {
  96. // TODO: Implement this
  97. }
  98. return limits;
  99. },
  100. { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity },
  101. );
  102. return [
  103. minX + element.x,
  104. minY + element.y,
  105. maxX + element.x,
  106. maxY + element.y,
  107. ];
  108. }
  109. export function getArrowPoints(
  110. element: ExcalidrawLinearElement,
  111. shape: Drawable[],
  112. ) {
  113. const ops = shape[0].sets[0].ops;
  114. const data = ops[ops.length - 1].data;
  115. const p3 = [data[4], data[5]] as Point;
  116. const p2 = [data[2], data[3]] as Point;
  117. const p1 = [data[0], data[1]] as Point;
  118. // we need to find p0 of the bezier curve
  119. // it is typically the last point of the previous
  120. // curve; it can also be the position of moveTo operation
  121. const prevOp = ops[ops.length - 2];
  122. let p0: Point = [0, 0];
  123. if (prevOp.op === "move") {
  124. p0 = (prevOp.data as unknown) as Point;
  125. } else if (prevOp.op === "bcurveTo") {
  126. p0 = [prevOp.data[4], prevOp.data[5]];
  127. }
  128. // B(t) = p0 * (1-t)^3 + 3p1 * t * (1-t)^2 + 3p2 * t^2 * (1-t) + p3 * t^3
  129. const equation = (t: number, idx: number) =>
  130. Math.pow(1 - t, 3) * p3[idx] +
  131. 3 * t * Math.pow(1 - t, 2) * p2[idx] +
  132. 3 * Math.pow(t, 2) * (1 - t) * p1[idx] +
  133. p0[idx] * Math.pow(t, 3);
  134. // we know the last point of the arrow
  135. const [x2, y2] = p3;
  136. // by using cubic bezier equation (B(t)) and the given parameters,
  137. // we calculate a point that is closer to the last point
  138. // The value 0.3 is chosen arbitrarily and it works best for all
  139. // the tested cases
  140. const [x1, y1] = [equation(0.3, 0), equation(0.3, 1)];
  141. // find the normalized direction vector based on the
  142. // previously calculated points
  143. const distance = Math.hypot(x2 - x1, y2 - y1);
  144. const nx = (x2 - x1) / distance;
  145. const ny = (y2 - y1) / distance;
  146. const size = 30; // pixels
  147. const arrowLength = element.points.reduce((total, [cx, cy], idx, points) => {
  148. const [px, py] = idx > 0 ? points[idx - 1] : [0, 0];
  149. return total + Math.hypot(cx - px, cy - py);
  150. }, 0);
  151. // Scale down the arrow until we hit a certain size so that it doesn't look weird
  152. // This value is selected by minizing a minmum size with the whole length of the arrow
  153. // intead of last segment of the arrow
  154. const minSize = Math.min(size, arrowLength / 2);
  155. const xs = x2 - nx * minSize;
  156. const ys = y2 - ny * minSize;
  157. const angle = 20; // degrees
  158. const [x3, y3] = rotate(xs, ys, x2, y2, (-angle * Math.PI) / 180);
  159. const [x4, y4] = rotate(xs, ys, x2, y2, (angle * Math.PI) / 180);
  160. return [x2, y2, x3, y3, x4, y4];
  161. }
  162. export function getCommonBounds(
  163. elements: readonly ExcalidrawElement[],
  164. ): [number, number, number, number] {
  165. if (!elements.length) {
  166. return [0, 0, 0, 0];
  167. }
  168. let minX = Infinity;
  169. let maxX = -Infinity;
  170. let minY = Infinity;
  171. let maxY = -Infinity;
  172. elements.forEach((element) => {
  173. const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
  174. const angle = element.angle;
  175. const cx = (x1 + x2) / 2;
  176. const cy = (y1 + y2) / 2;
  177. const [x11, y11] = rotate(x1, y1, cx, cy, angle);
  178. const [x12, y12] = rotate(x1, y2, cx, cy, angle);
  179. const [x22, y22] = rotate(x2, y2, cx, cy, angle);
  180. const [x21, y21] = rotate(x2, y1, cx, cy, angle);
  181. minX = Math.min(minX, x11, x12, x22, x21);
  182. minY = Math.min(minY, y11, y12, y22, y21);
  183. maxX = Math.max(maxX, x11, x12, x22, x21);
  184. maxY = Math.max(maxY, y11, y12, y22, y21);
  185. });
  186. return [minX, minY, maxX, maxY];
  187. }