您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

utils.d.ts 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. import type React from 'react';
  2. import { TLBezierCurveSegment, TLBounds, TLBoundsCorner, TLBoundsEdge } from '../types';
  3. import './polyfills';
  4. import type { TLBoundsWithCenter } from '+index';
  5. export declare class Utils {
  6. static filterObject<T extends object>(obj: T, fn: (entry: Entry<T>, i?: number, arr?: Entry<T>[]) => boolean): Partial<T>;
  7. static deepMerge: <T>(target: T, patch: any) => T;
  8. /**
  9. * Linear interpolation betwen two numbers.
  10. * @param y1
  11. * @param y2
  12. * @param mu
  13. */
  14. static lerp(y1: number, y2: number, mu: number): number;
  15. /**
  16. * Linear interpolation between two colors.
  17. *
  18. * ### Example
  19. *
  20. *```ts
  21. * lerpColor("#000000", "#0099FF", .25)
  22. *```
  23. */
  24. static lerpColor(color1: string, color2: string, factor?: number): string;
  25. /**
  26. * Modulate a value between two ranges.
  27. * @param value
  28. * @param rangeA from [low, high]
  29. * @param rangeB to [low, high]
  30. * @param clamp
  31. */
  32. static modulate(value: number, rangeA: number[], rangeB: number[], clamp?: boolean): number;
  33. /**
  34. * Clamp a value into a range.
  35. * @param n
  36. * @param min
  37. */
  38. static clamp(n: number, min: number): number;
  39. static clamp(n: number, min: number, max: number): number;
  40. static compress(s: string): string;
  41. static decompress(s: string): string;
  42. /**
  43. * Recursively clone an object or array.
  44. * @param obj
  45. */
  46. static deepClone<T extends unknown>(obj: T): T;
  47. /**
  48. * Seeded random number generator, using [xorshift](https://en.wikipedia.org/wiki/Xorshift).
  49. * The result will always be betweeen -1 and 1.
  50. *
  51. * Adapted from [seedrandom](https://github.com/davidbau/seedrandom).
  52. */
  53. static rng(seed?: string): () => number;
  54. static getRectangleSides(point: number[], size: number[], rotation?: number): [string, number[][]][];
  55. static getBoundsSides(bounds: TLBounds): [string, number[][]][];
  56. static shallowEqual<T extends Record<string, unknown>>(objA: T, objB: T): boolean;
  57. /**
  58. * Get the outer of between a circle and a point.
  59. * @param C The circle's center.
  60. * @param r The circle's radius.
  61. * @param P The point.
  62. * @param side
  63. */
  64. static getCircleTangentToPoint(C: number[], r: number, P: number[], side: number): number[] | null;
  65. /**
  66. * Get outer tangents of two circles.
  67. * @param x0
  68. * @param y0
  69. * @param r0
  70. * @param x1
  71. * @param y1
  72. * @param r1
  73. * @returns [lx0, ly0, lx1, ly1, rx0, ry0, rx1, ry1]
  74. */
  75. static getOuterTangentsOfCircles(C0: number[], r0: number, C1: number[], r1: number): number[][] | null;
  76. /**
  77. * Get the closest point on the perimeter of a circle to a given point.
  78. * @param C The circle's center.
  79. * @param r The circle's radius.
  80. * @param P The point.
  81. */
  82. static getClosestPointOnCircle(C: number[], r: number, P: number[]): number[];
  83. /**
  84. * Get a circle from three points.
  85. * @param A
  86. * @param B
  87. * @param C
  88. * @returns [x, y, r]
  89. */
  90. static circleFromThreePoints(A: number[], B: number[], C: number[]): number[];
  91. /**
  92. * Find the approximate perimeter of an ellipse.
  93. * @param rx
  94. * @param ry
  95. */
  96. static perimeterOfEllipse(rx: number, ry: number): number;
  97. /**
  98. * Get the short angle distance between two angles.
  99. * @param a0
  100. * @param a1
  101. */
  102. static shortAngleDist(a0: number, a1: number): number;
  103. /**
  104. * Get the long angle distance between two angles.
  105. * @param a0
  106. * @param a1
  107. */
  108. static longAngleDist(a0: number, a1: number): number;
  109. /**
  110. * Interpolate an angle between two angles.
  111. * @param a0
  112. * @param a1
  113. * @param t
  114. */
  115. static lerpAngles(a0: number, a1: number, t: number): number;
  116. /**
  117. * Get the short distance between two angles.
  118. * @param a0
  119. * @param a1
  120. */
  121. static angleDelta(a0: number, a1: number): number;
  122. /**
  123. * Get the "sweep" or short distance between two points on a circle's perimeter.
  124. * @param C
  125. * @param A
  126. * @param B
  127. */
  128. static getSweep(C: number[], A: number[], B: number[]): number;
  129. /**
  130. * Rotate a point around a center.
  131. * @param x The x-axis coordinate of the point.
  132. * @param y The y-axis coordinate of the point.
  133. * @param cx The x-axis coordinate of the point to rotate round.
  134. * @param cy The y-axis coordinate of the point to rotate round.
  135. * @param angle The distance (in radians) to rotate.
  136. */
  137. static rotatePoint(A: number[], B: number[], angle: number): number[];
  138. /**
  139. * Clamp radians within 0 and 2PI
  140. * @param r
  141. */
  142. static clampRadians(r: number): number;
  143. /**
  144. * Clamp rotation to even segments.
  145. * @param r
  146. * @param segments
  147. */
  148. static snapAngleToSegments(r: number, segments: number): number;
  149. /**
  150. * Is angle c between angles a and b?
  151. * @param a
  152. * @param b
  153. * @param c
  154. */
  155. static isAngleBetween(a: number, b: number, c: number): boolean;
  156. /**
  157. * Convert degrees to radians.
  158. * @param d
  159. */
  160. static degreesToRadians(d: number): number;
  161. /**
  162. * Convert radians to degrees.
  163. * @param r
  164. */
  165. static radiansToDegrees(r: number): number;
  166. /**
  167. * Get the length of an arc between two points on a circle's perimeter.
  168. * @param C
  169. * @param r
  170. * @param A
  171. * @param B
  172. */
  173. static getArcLength(C: number[], r: number, A: number[], B: number[]): number;
  174. /**
  175. * Get a dash offset for an arc, based on its length.
  176. * @param C
  177. * @param r
  178. * @param A
  179. * @param B
  180. * @param step
  181. */
  182. static getArcDashOffset(C: number[], r: number, A: number[], B: number[], step: number): number;
  183. /**
  184. * Get a dash offset for an ellipse, based on its length.
  185. * @param A
  186. * @param step
  187. */
  188. static getEllipseDashOffset(A: number[], step: number): number;
  189. /**
  190. * Get bezier curve segments that pass through an array of points.
  191. * @param points
  192. * @param tension
  193. */
  194. static getTLBezierCurveSegments(points: number[][], tension?: number): TLBezierCurveSegment[];
  195. /**
  196. * Find a point along a curve segment, via pomax.
  197. * @param t
  198. * @param points [cpx1, cpy1, cpx2, cpy2, px, py][]
  199. */
  200. static computePointOnCurve(t: number, points: number[][]): number[];
  201. /**
  202. * Evaluate a 2d cubic bezier at a point t on the x axis.
  203. * @param tx
  204. * @param x1
  205. * @param y1
  206. * @param x2
  207. * @param y2
  208. */
  209. static cubicBezier(tx: number, x1: number, y1: number, x2: number, y2: number): number;
  210. /**
  211. * Get a bezier curve data for a spline that fits an array of points.
  212. * @param points An array of points formatted as [x, y]
  213. * @param k Tension
  214. */
  215. static getSpline(pts: number[][], k?: number): {
  216. cp1x: number;
  217. cp1y: number;
  218. cp2x: number;
  219. cp2y: number;
  220. px: number;
  221. py: number;
  222. }[];
  223. /**
  224. * Get a bezier curve data for a spline that fits an array of points.
  225. * @param pts
  226. * @param tension
  227. * @param isClosed
  228. * @param numOfSegments
  229. */
  230. static getCurvePoints(pts: number[][], tension?: number, isClosed?: boolean, numOfSegments?: number): number[][];
  231. /**
  232. * Simplify a line (using Ramer-Douglas-Peucker algorithm).
  233. * @param points An array of points as [x, y, ...][]
  234. * @param tolerance The minimum line distance (also called epsilon).
  235. * @returns Simplified array as [x, y, ...][]
  236. */
  237. static simplify(points: number[][], tolerance?: number): number[][];
  238. /**
  239. * Get whether a point is inside of a circle.
  240. * @param A
  241. * @param b
  242. * @returns
  243. */
  244. static pointInCircle(A: number[], C: number[], r: number): boolean;
  245. /**
  246. * Get whether a point is inside of an ellipse.
  247. * @param point
  248. * @param center
  249. * @param rx
  250. * @param ry
  251. * @param rotation
  252. * @returns
  253. */
  254. static pointInEllipse(A: number[], C: number[], rx: number, ry: number, rotation?: number): boolean;
  255. /**
  256. * Get whether a point is inside of a rectangle.
  257. * @param point
  258. * @param size
  259. */
  260. static pointInRect(point: number[], size: number[]): boolean;
  261. static pointInPolygon(p: number[], points: number[][]): boolean;
  262. /**
  263. * Expand a bounding box by a delta.
  264. *
  265. * ### Example
  266. *
  267. *```ts
  268. * expandBounds(myBounds, [100, 100])
  269. *```
  270. */
  271. static expandBounds(bounds: TLBounds, delta: number): TLBounds;
  272. /**
  273. * Get whether a point is inside of a bounds.
  274. * @param A
  275. * @param b
  276. * @returns
  277. */
  278. static pointInBounds(A: number[], b: TLBounds): boolean;
  279. /**
  280. * Get whether two bounds collide.
  281. * @param a Bounds
  282. * @param b Bounds
  283. * @returns
  284. */
  285. static boundsCollide(a: TLBounds, b: TLBounds): boolean;
  286. /**
  287. * Get whether the bounds of A contain the bounds of B. A perfect match will return true.
  288. * @param a Bounds
  289. * @param b Bounds
  290. * @returns
  291. */
  292. static boundsContain(a: TLBounds, b: TLBounds): boolean;
  293. /**
  294. * Get whether the bounds of A are contained by the bounds of B.
  295. * @param a Bounds
  296. * @param b Bounds
  297. * @returns
  298. */
  299. static boundsContained(a: TLBounds, b: TLBounds): boolean;
  300. /**
  301. * Get whether two bounds are identical.
  302. * @param a Bounds
  303. * @param b Bounds
  304. * @returns
  305. */
  306. static boundsAreEqual(a: TLBounds, b: TLBounds): boolean;
  307. /**
  308. * Find a bounding box from an array of points.
  309. * @param points
  310. * @param rotation (optional) The bounding box's rotation.
  311. */
  312. static getBoundsFromPoints(points: number[][], rotation?: number): TLBounds;
  313. /**
  314. * Center a bounding box around a given point.
  315. * @param bounds
  316. * @param center
  317. */
  318. static centerBounds(bounds: TLBounds, point: number[]): TLBounds;
  319. /**
  320. * Move a bounding box without recalculating it.
  321. * @param bounds
  322. * @param delta
  323. * @returns
  324. */
  325. static translateBounds(bounds: TLBounds, delta: number[]): TLBounds;
  326. /**
  327. * Rotate a bounding box.
  328. * @param bounds
  329. * @param center
  330. * @param rotation
  331. */
  332. static rotateBounds(bounds: TLBounds, center: number[], rotation: number): TLBounds;
  333. /**
  334. * Get the rotated bounds of an ellipse.
  335. * @param x
  336. * @param y
  337. * @param rx
  338. * @param ry
  339. * @param rotation
  340. */
  341. static getRotatedEllipseBounds(x: number, y: number, rx: number, ry: number, rotation?: number): TLBounds;
  342. /**
  343. * Get a bounding box that includes two bounding boxes.
  344. * @param a Bounding box
  345. * @param b Bounding box
  346. * @returns
  347. */
  348. static getExpandedBounds(a: TLBounds, b: TLBounds): TLBounds;
  349. /**
  350. * Get the common bounds of a group of bounds.
  351. * @returns
  352. */
  353. static getCommonBounds(bounds: TLBounds[]): TLBounds;
  354. static getRotatedCorners(b: TLBounds, rotation?: number): number[][];
  355. static getTransformedBoundingBox(bounds: TLBounds, handle: TLBoundsCorner | TLBoundsEdge | 'center', delta: number[], rotation?: number, isAspectRatioLocked?: boolean): TLBounds & {
  356. scaleX: number;
  357. scaleY: number;
  358. };
  359. static getTransformAnchor(type: TLBoundsEdge | TLBoundsCorner, isFlippedX: boolean, isFlippedY: boolean): TLBoundsCorner | TLBoundsEdge;
  360. /**
  361. * Get the relative bounds (usually a child) within a transformed bounding box.
  362. * @param bounds
  363. * @param initialBounds
  364. * @param initialShapeBounds
  365. * @param isFlippedX
  366. * @param isFlippedY
  367. */
  368. static getRelativeTransformedBoundingBox(bounds: TLBounds, initialBounds: TLBounds, initialShapeBounds: TLBounds, isFlippedX: boolean, isFlippedY: boolean): TLBounds;
  369. /**
  370. * Get the size of a rotated box.
  371. * @param size : ;
  372. * @param rotation
  373. */
  374. static getRotatedSize(size: number[], rotation: number): number[];
  375. /**
  376. * Get the center of a bounding box.
  377. * @param bounds
  378. */
  379. static getBoundsCenter(bounds: TLBounds): number[];
  380. /**
  381. * Get a bounding box with a midX and midY.
  382. * @param bounds
  383. */
  384. static getBoundsWithCenter(bounds: TLBounds): TLBounds & {
  385. midX: number;
  386. midY: number;
  387. };
  388. static getSnapPoints: (bounds: any, others: TLBoundsWithCenter[], snapDistance: number) => {
  389. offset: number[];
  390. snapLines: number[][][];
  391. };
  392. /**
  393. *
  394. *
  395. * ### Example
  396. *
  397. *```ts
  398. * example
  399. *```
  400. */
  401. static removeDuplicatePoints(points: number[][]): number[][];
  402. /**
  403. // points =
  404. /**
  405. * Get a value from a cache (a WeakMap), filling the value if it is not present.
  406. *
  407. * ### Example
  408. *
  409. *```ts
  410. * getFromCache(boundsCache, shape, (cache) => cache.set(shape, "value"))
  411. *```
  412. */
  413. static getFromCache<V, I extends object>(cache: WeakMap<I, V>, item: I, getNext: () => V): V;
  414. /**
  415. * Get a unique string id.
  416. */
  417. static uniqueId(a?: string): string;
  418. /**
  419. * Shuffle the contents of an array.
  420. * @param arr
  421. * @param offset
  422. */
  423. static rotateArray<T>(arr: T[], offset: number): T[];
  424. /**
  425. * Deep compare two arrays.
  426. * @param a
  427. * @param b
  428. */
  429. static deepCompareArrays<T>(a: T[], b: T[]): boolean;
  430. /**
  431. * Deep compare any values.
  432. * @param a
  433. * @param b
  434. */
  435. static deepCompare<T>(a: T, b: T): boolean;
  436. /**
  437. * Find whether two arrays intersect.
  438. * @param a
  439. * @param b
  440. * @param fn An optional function to apply to the items of a; will check if b includes the result.
  441. */
  442. static arrsIntersect<T, K>(a: T[], b: K[], fn?: (item: K) => T): boolean;
  443. static arrsIntersect<T>(a: T[], b: T[]): boolean;
  444. /**
  445. * Get the unique values from an array of strings or numbers.
  446. * @param items
  447. */
  448. static uniqueArray<T extends string | number>(...items: T[]): T[];
  449. /**
  450. * Convert a set to an array.
  451. * @param set
  452. */
  453. static setToArray<T>(set: Set<T>): T[];
  454. /**
  455. * Debounce a function.
  456. */
  457. static debounce<T extends (...args: any[]) => void>(fn: T, ms?: number): (...args: Parameters<T>) => void;
  458. static TRIM_NUMBERS: RegExp;
  459. /**
  460. * Turn an array of points into a path of quadradic curves.
  461. * @param stroke ;
  462. */
  463. static getSvgPathFromStroke(points: number[][], closed?: boolean): string;
  464. /**
  465. * Get balanced dash-strokearray and dash-strokeoffset properties for a path of a given length.
  466. * @param length The length of the path.
  467. * @param strokeWidth The shape's stroke-width property.
  468. * @param style The stroke's style: "dashed" or "dotted" (default "dashed").
  469. * @param snap An interval for dashes (e.g. 4 will produce arrays with 4, 8, 16, etc dashes).
  470. */
  471. static getPerfectDashProps(length: number, strokeWidth: number, style: 'dashed' | 'dotted' | string, snap?: number, outset?: boolean): {
  472. strokeDasharray: string;
  473. strokeDashoffset: string;
  474. };
  475. static isMobileSize(): boolean;
  476. static isMobileSafari(): boolean;
  477. static throttle<T extends (...args: any) => any>(func: T, limit: number): (...args: Parameters<T>) => ReturnType<T>;
  478. /**
  479. * Find whether the current display is a touch display.
  480. */
  481. /**
  482. * Find whether the current device is a Mac / iOS / iPadOS.
  483. */
  484. static isDarwin(): boolean;
  485. /**
  486. * Get whether an event is command (mac) or control (pc).
  487. * @param e
  488. */
  489. static metaKey(e: KeyboardEvent | React.KeyboardEvent): boolean;
  490. }
  491. export default Utils;
  492. declare type Entry<T> = {
  493. [K in keyof T]: [K, T[K]];
  494. }[keyof T];
  495. //# sourceMappingURL=utils.d.ts.map