Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 | 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 74x 74x 74x 74x 74x 714x 714x 714x 714x 714x 714x 714x 714x 714x 714x 714x 714x 714x 714x 716x 1276x 714x 714x 714x 714x 714x 714x 714x 714x 865x 714x 74x 110x 109x 110x 234x 78x 78x 74x 714x 74x 90x 734x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 33x 33x 367x 367x 1x 74x 330x 330x 330x 330x 330x 330x 74x 2x 74x 74x 155x 74x 74x 36x 36x 36x 86x 43x 43x 43x 43x 43x 52x 52x 52x 43x 43x 74x 76x 76x 76x 36x 36x 26x 26x 36x 36x 36x 36x 36x 36x 36x 74x 36x 36x 74x 74x 35x 32x 32x 714x 32x 714x 74x 74x 118x 118x 118x 40x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 74x 205x 104x 74x 83x 51x 51x 51x 51x 51x 51x 4x 4x 4x 74x 78x 78x 72x 72x 72x 72x 72x 72x 74x 78x 78x 78x 93x 186x 93x 78x 74x 74x 74x 74x 747x 74x 74x 74x 714x 74x 714x 74x 74x 74x 4x 4x 4x 714x 74x 74x 74x 74x 74x 714x 74x 74x 275x 275x 74x 1608x 1608x 74x 74x 609x 609x 74x 74x 74x 83x 83x 39x 39x 39x 156x 156x 156x 156x 39x 2283x 1608x 1608x 1608x 1608x 1608x 1608x 1608x 1608x 1608x 1608x 1608x 1608x 74x 83x 74x 74x 83x 83x 83x 83x 83x 83x 83x 83x 83x 37x 37x 37x 37x 32x 32x 32x 32x 32x 32x 32x 32x 156x 156x 78x 78x | import sync, { cancelSync, flushSync, Process } from "framesync" import { mix } from "popmotion" import { animate, AnimationOptions, AnimationPlaybackControls, } from "../../animation/animate" import { ResolvedValues } from "../../render/types" import { SubscriptionManager } from "../../utils/subscription-manager" import { mixValues } from "../animation/mix-values" import { copyBoxInto } from "../geometry/copy" import { applyBoxDelta, applyTreeDeltas } from "../geometry/delta-apply" import { calcBoxDelta, calcLength, calcRelativeBox, calcRelativePosition, } from "../geometry/delta-calc" import { removeBoxTransforms } from "../geometry/delta-remove" import { createBox, createDelta } from "../geometry/models" import { transformBox, translateAxis } from "../geometry/delta-apply" import { Axis, AxisDelta, Box, Delta, Point } from "../geometry/types" import { getValueTransition } from "../../animation/utils/transitions" import { boxEquals, isDeltaZero } from "../geometry/utils" import { NodeStack } from "../shared/stack" import { scaleCorrectors } from "../styles/scale-correction" import { buildProjectionTransform } from "../styles/transform" import { eachAxis } from "../utils/each-axis" import { hasScale, hasTransform } from "../utils/has-transform" import { IProjectionNode, Layout, LayoutEvents, LayoutUpdateData, ProjectionNodeConfig, ProjectionNodeOptions, Snapshot, } from "./types" import { transformAxes } from "../../render/html/utils/transform" import { FlatTree } from "../../render/utils/flat-tree" import { Transition } from "../../types" import { resolveMotionValue } from "../../value/utils/resolve-motion-value" import { MotionStyle } from "../../motion/types" /** * We use 1000 as the animation target as 0-1000 maps better to pixels than 0-1 * which has a noticeable difference in spring animations */ const animationTarget = 1000 /** * This should only ever be modified on the client otherwise it'll * persist through server requests. If we need instanced states we * could lazy-init via root. */ export const globalProjectionState = { /** * Global flag as to whether the tree has animated since the last time * we resized the window */ hasAnimatedSinceResize: true, /** * We set this to true once, on the first update. Any nodes added to the tree beyond that * update will be given a `data-projection-id` attribute. */ hasEverUpdated: false, } export function createProjectionNode<I>({ attachResizeListener, defaultParent, measureScroll, resetTransform, }: ProjectionNodeConfig<I>) { return class ProjectionNode implements IProjectionNode<I> { /** * A unique ID generated for every projection node. * * The projection tree's `didUpdate` function will be triggered by the first element * in the tree to run its layout effects. However, if there are elements entering the tree * these might not be mounted yet. When React renders a `motion` component we * give it a unique selector and register it as a potential projection node (not all * rendered components will be committed by React). In `didUpdate`, we search the DOM for * these potential nodes with this id and hydrate the projetion node of the ones that were commited. */ id: number | undefined /** * A reference to the platform-native node (currently this will be a HTMLElement). */ instance: I /** * A reference to the root projection node. There'll only ever be one tree and one root. */ root: IProjectionNode /** * A reference to this node's parent. */ parent?: IProjectionNode /** * A path from this node to the root node. This provides a fast way to iterate * back up the tree. */ path: IProjectionNode[] /** * A Set containing all this component's children. This is used to iterate * through the children. * * TODO: This could be faster to iterate as a flat array stored on the root node. */ children = new Set<IProjectionNode>() /** * Options for the node. We use this to configure what kind of layout animations * we should perform (if any). */ options: ProjectionNodeOptions = {} /** * A snapshot of the element's state just before the current update. This is * hydrated when this node's `willUpdate` method is called and scrubbed at the * end of the tree's `didUpdate` method. */ snapshot: Snapshot | undefined /** * A box defining the element's layout relative to the page. This will have been * captured with all parent scrolls and projection transforms unset. */ layout: Layout | undefined /** * The layout used to calculate the previous layout animation. We use this to compare * layouts between renders and decide whether we need to trigger a new layout animation * or just let the current one play out. */ targetLayout?: Box /** * A mutable data structure we use to apply all parent transforms currently * acting on the element's layout. It's from here we can calculate the projectionDelta * required to get the element from its layout into its calculated target box. */ layoutCorrected: Box /** * An ideal projection transform we want to apply to the element. This is calculated, * usually when an element's layout has changed, and we want the element to look as though * its in its previous layout on the next frame. From there, we animated it down to 0 * to animate the element to its new layout. */ targetDelta?: Delta /** * A mutable structure representing the visual bounding box on the page where we want * and element to appear. This can be set directly but is currently derived once a frame * from apply targetDelta to layout. */ target?: Box /** * A mutable structure describing a visual bounding box relative to the element's * projected parent. If defined, target will be derived from this rather than targetDelta. * If not defined, we'll attempt to calculate on the first layout animation frame * based on the targets calculated from targetDelta. This will transfer a layout animation * from viewport-relative to parent-relative. */ relativeTarget?: Box relativeTargetOrigin?: Box relativeParent?: IProjectionNode /** * We use this to detect when its safe to shut down part of a projection tree. * We have to keep projecting children for scale correction and relative projection * until all their parents stop performing layout animations. */ isTreeAnimating = false isAnimationBlocked = false /** * If true, attempt to resolve relativeTarget. */ attemptToResolveRelativeTarget?: boolean /** * A mutable structure that represents the target as transformed by the element's * latest user-set transforms (ie scale, x) */ targetWithTransforms?: Box /** * A calculated transform that will project an element from its layoutCorrected * into the target. This will be used by children to calculate their own layoutCorrect boxes. */ projectionDelta?: Delta /** * A calculated transform that will project an element from its layoutCorrected * into the targetWithTransforms. */ projectionDeltaWithTransform?: Delta /** * If we're tracking the scroll of this element, we store it here. */ scroll?: Point /** * Flag to true if we think this layout has been changed. We can't always know this, * currently we set it to true every time a component renders, or if it has a layoutDependency * if that has changed between renders. Additionally, components can be grouped by LayoutGroup * and if one node is dirtied, they all are. */ isLayoutDirty = false /** * Block layout updates for instant layout transitions throughout the tree. */ updateManuallyBlocked = false updateBlockedByResize = false /** * Set to true between the start of the first `willUpdate` call and the end of the `didUpdate` * call. */ isUpdating = false /** * If this is an SVG element we currently disable projection transforms */ isSVG = false /** * Flag to true (during promotion) if a node doing an instant layout transition needs to reset * its projection styles. */ needsReset = false /** * Flags whether this node should have its transform reset prior to measuring. */ shouldResetTransform = false /** * An object representing the calculated contextual/accumulated/tree scale. * This will be used to scale calculcated projection transforms, as these are * calculated in screen-space but need to be scaled for elements to actually * make it to their calculated destinations. * * TODO: Lazy-init */ treeScale: Point = { x: 1, y: 1 } /** * Is hydrated with a projection node if an element is animating from another. */ resumeFrom?: IProjectionNode /** * Is hydrated with a projection node if an element is animating from another. */ resumingFrom?: IProjectionNode /** * A reference to the element's latest animated values. This is a reference shared * between the element's VisualElement and the ProjectionNode. */ latestValues: ResolvedValues /** * */ eventHandlers = new Map<LayoutEvents, SubscriptionManager<any>>() nodes?: FlatTree depth: number /** * When we update the projection transform, we also build it into a string. * If the string changes between frames, we trigger a render. */ projectionTransform: string /** * If transformTemplate generates a different value before/after the * update, we need to reset the transform. */ prevTransformTemplateValue: string | undefined preserveOpacity?: boolean constructor( id: number | undefined, latestValues: ResolvedValues = {}, parent: IProjectionNode | undefined = defaultParent?.() ) { this.id = id this.latestValues = latestValues this.root = parent ? parent.root || parent : this this.path = parent ? [...parent.path, parent] : [] this.parent = parent this.depth = parent ? parent.depth + 1 : 0 id && this.root.registerPotentialNode(id, this) for (let i = 0; i < this.path.length; i++) { this.path[i].shouldResetTransform = true } if (this.root === this) this.nodes = new FlatTree() } addEventListener(name: LayoutEvents, handler: any) { if (!this.eventHandlers.has(name)) { this.eventHandlers.set(name, new SubscriptionManager()) } return this.eventHandlers.get(name)!.add(handler) } notifyListeners(name: LayoutEvents, ...args: any) { const subscriptionManager = this.eventHandlers.get(name) subscriptionManager?.notify(...args) } hasListeners(name: LayoutEvents) { return this.eventHandlers.has(name) } // Note: Currently only running on root node potentialNodes = new Map<number, IProjectionNode>() registerPotentialNode(id: number, node: IProjectionNode) { this.potentialNodes.set(id, node) } /** * Lifecycles */ mount(instance: I, EisLayoutDirty = false) { Iif (this.instance) return this.isSVG = instance instanceof SVGElement && instance.tagName !== "svg" this.instance = instance const { layoutId, layout, visualElement } = this.options Iif (visualElement && !visualElement.getInstance()) { visualElement.mount(instance) } this.root.nodes!.add(this) this.parent?.children.add(this) this.id && this.root.potentialNodes.delete(this.id) Iif (isLayoutDirty && (layout || layoutId)) { this.isLayoutDirty = true } if (attachResizeListener) { let unblockTimeout: number const resizeUnblockUpdate = () => (this.root.updateBlockedByResize = false) attachResizeListener(instance, () => { this.root.updateBlockedByResize = true clearTimeout(unblockTimeout) unblockTimeout = window.setTimeout(resizeUnblockUpdate, 250) if (globalProjectionState.hasAnimatedSinceResize) { globalProjectionState.hasAnimatedSinceResize = false this.nodes!.forEach(finishAnimation) } }) } Iif (layoutId) { this.root.registerSharedNode(layoutId, this) } // Only register the handler if it requires layout animation if ( this.options.animate !== false && visualElement && (layoutId || layout) ) { this.addEventListener( "didUpdate", ({ delta, hasLayoutChanged, hasRelativeTargetChanged, layout: newLayout, }: LayoutUpdateData) => { if (this.isTreeAnimationBlocked()) { this.target = undefined this.relativeTarget = undefined return } // TODO: Check here if an animation exists const layoutTransition = this.options.transition ?? visualElement.getDefaultTransition() ?? defaultLayoutTransition const { onLayoutAnimationComplete } = visualElement.getProps() /** * The target layout of the element might stay the same, * but its position relative to its parent has changed. */ const targetChanged = !this.targetLayout || !boxEquals(this.targetLayout, newLayout) || hasRelativeTargetChanged /** * If the layout hasn't seemed to have changed, it might be that the * element is visually in the same place in the document but its position * relative to its parent has indeed changed. So here we check for that. */ const hasOnlyRelativeTargetChanged = !hasLayoutChanged && hasRelativeTargetChanged if ( this.resumeFrom?.instance || hasOnlyRelativeTargetChanged || (hasLayoutChanged && (targetChanged || !this.currentAnimation)) ) { if (this.resumeFrom) { this.resumingFrom = this.resumeFrom this.resumingFrom.resumingFrom = undefined } this.setAnimationOrigin( delta, hasOnlyRelativeTargetChanged ) this.startAnimation({ ...getValueTransition( layoutTransition, "layout" ), onComplete: onLayoutAnimationComplete, }) } else { this.isLead() && this.options.onExitComplete?.() } this.targetLayout = newLayout } ) } } unmount() { this.options.layoutId && this.willUpdate() this.root.nodes!.remove(this) this.getStack()?.remove(this) this.parent?.children.delete(this) ;(this.instance as any) = undefined cancelSync.preRender(this.updateProjection) } // only on the root blockUpdate() { this.updateManuallyBlocked = true } unblockUpdate() { this.updateManuallyBlocked = false } isUpdateBlocked() { return this.updateManuallyBlocked || this.updateBlockedByResize } isTreeAnimationBlocked() { return ( this.isAnimationBlocked || this.parent?.isTreeAnimationBlocked() || false ) } // Note: currently only running on root node startUpdate() { Iif (this.isUpdateBlocked()) return this.isUpdating = true this.nodes?.forEach(resetRotation) } willUpdate(EshouldNotifyListeners = true) { Iif (this.root.isUpdateBlocked()) { this.options.onExitComplete?.() return } !this.root.isUpdating && this.root.startUpdate() Iif (this.isLayoutDirty) return this.isLayoutDirty = true for (let i = 0; i < this.path.length; i++) { const node = this.path[i] node.shouldResetTransform = true /** * TODO: Check we haven't updated the scroll * since the last didUpdate */ node.updateScroll() } const { layoutId, layout } = this.options Eif (layoutId === undefined && !layout) return const transformTemplate = this.options.visualElement?.getProps().transformTemplate this.prevTransformTemplateValue = transformTemplate?.( this.latestValues, "" ) this.updateSnapshot() shouldNotifyListeners && this.notifyListeners("willUpdate") } // Note: Currently only running on root node didUpdate() { const updateWasBlocked = this.isUpdateBlocked() // When doing an instant transition, we skip the layout update, // but should still clean up the measurements so that the next // snapshot could be taken correctly. Iif (updateWasBlocked) { this.unblockUpdate() this.clearAllSnapshots() this.nodes!.forEach(clearMeasurements) return } if (!this.isUpdating) return this.isUpdating = false /** * Search for and mount newly-added projection elements. * * TODO: Every time a new component is rendered we could search up the tree for * the closest mounted node and query from there rather than document. */ if (this.potentialNodes.size) { this.potentialNodes.forEach(mountNodeEarly) this.potentialNodes.clear() } /** * Write */ this.nodes!.forEach(resetTransformStyle) /** * Read ================== */ // Update layout measurements of updated children this.nodes!.forEach(updateLayout) /** * Write */ // Notify listeners that the layout is updated this.nodes!.forEach(notifyLayoutUpdate) this.clearAllSnapshots() // Flush any scheduled updates flushSync.update() flushSync.preRender() flushSync.render() } clearAllSnapshots() { this.nodes!.forEach(clearSnapshot) this.sharedNodes.forEach(removeLeadSnapshots) } scheduleUpdateProjection() { sync.preRender(this.updateProjection, false, true) } scheduleCheckAfterUnmount() { /** * If the unmounting node is in a layoutGroup and did trigger a willUpdate, * we manually call didUpdate to give a chance to the siblings to animate. * Otherwise, cleanup all snapshots to prevents future nodes from reusing them. */ sync.postRender(() => { Iif (this.isLayoutDirty) { this.root.didUpdate() } else { this.root.checkUpdateFailed() } }) } checkUpdateFailed = () => { Iif (this.isUpdating) { this.isUpdating = false this.clearAllSnapshots() } } updateProjection = () => { this.nodes!.forEach(resolveTargetDelta) this.nodes!.forEach(calcProjection) } /** * Update measurements */ updateSnapshot() { if (this.snapshot || !this.instance) return const measured = this.measure()! const layout = this.removeTransform( this.removeElementScroll(measured) ) roundBox(layout) this.snapshot = { measured, layout, latestValues: {}, } } updateLayout() { Iif (!this.instance) return // TODO: Incorporate into a forwarded scroll offset this.updateScroll() if ( !(this.options.alwaysMeasureLayout && this.isLead()) && !this.isLayoutDirty ) { return } /** * When a node is mounted, it simply resumes from the prevLead's * snapshot instead of taking a new one, but the ancestors scroll * might have updated while the prevLead is unmounted. We need to * update the scroll again to make sure the layout we measure is * up to date. */ Iif (this.resumeFrom && !this.resumeFrom.instance) { for (let i = 0; i < this.path.length; i++) { const node = this.path[i] node.updateScroll() } } const measured = this.measure() roundBox(measured) const prevLayout = this.layout this.layout = { measured, actual: this.removeElementScroll(measured), } this.layoutCorrected = createBox() this.isLayoutDirty = false this.projectionDelta = undefined this.notifyListeners("measure", this.layout.actual) this.options.visualElement?.notifyLayoutMeasure( this.layout.actual, prevLayout?.actual ) } updateScroll() { if (this.options.layoutScroll && this.instance) { this.scroll = measureScroll(this.instance) } } resetTransform() { if (!resetTransform) return const isResetRequested = this.isLayoutDirty || this.shouldResetTransform const hasProjection = this.projectionDelta && !isDeltaZero(this.projectionDelta) const transformTemplate = this.options.visualElement?.getProps().transformTemplate const transformTemplateValue = transformTemplate?.( this.latestValues, "" ) const transformTemplateHasChanged = transformTemplateValue !== this.prevTransformTemplateValue if ( isResetRequested && (hasProjection || hasTransform(this.latestValues) || transformTemplateHasChanged) ) { resetTransform(this.instance, transformTemplateValue) this.shouldResetTransform = false this.scheduleRender() } } measure() { const { visualElement } = this.options if (!visualElement) return createBox() const box = visualElement.measureViewportBox() // Remove viewport scroll to give page-relative coordinates const { scroll } = this.root Eif (scroll) { translateAxis(box.x, scroll.x) translateAxis(box.y, scroll.y) } return box } removeElementScroll(box: Box): Box { const boxWithoutScroll = createBox() copyBoxInto(boxWithoutScroll, box) /** * Performance TODO: Keep a cumulative scroll offset down the tree * rather than loop back up the path. */ for (let i = 0; i < this.path.length; i++) { const node = this.path[i] const { scroll, options } = node Iif (node !== this.root && scroll && options.layoutScroll) { translateAxis(boxWithoutScroll.x, scroll.x) translateAxis(boxWithoutScroll.y, scroll.y) } } return boxWithoutScroll } applyTransform(box: Box, transformOnly = false): Box { const withTransforms = createBox() copyBoxInto(withTransforms, box) for (let i = 0; i < this.path.length; i++) { const node = this.path[i] if ( !transformOnly && node.options.layoutScroll && node.scroll && node !== node.root ) { transformBox(withTransforms, { x: -node.scroll.x, y: -node.scroll.y, }) } if (!hasTransform(node.latestValues)) continue transformBox(withTransforms, node.latestValues) } if (hasTransform(this.latestValues)) { transformBox(withTransforms, this.latestValues) } return withTransforms } removeTransform(box: Box): Box { const boxWithoutTransform = createBox() copyBoxInto(boxWithoutTransform, box) for (let i = 0; i < this.path.length; i++) { const node = this.path[i] if (!node.instance) continue if (!hasTransform(node.latestValues)) continue hasScale(node.latestValues) && node.updateSnapshot() const sourceBox = createBox() const nodeBox = node.measure() copyBoxInto(sourceBox, nodeBox) removeBoxTransforms( boxWithoutTransform, node.latestValues, node.snapshot?.layout, sourceBox ) } if (hasTransform(this.latestValues)) { removeBoxTransforms(boxWithoutTransform, this.latestValues) } return boxWithoutTransform } /** * */ setTargetDelta(delta: Delta) { this.targetDelta = delta this.root.scheduleUpdateProjection() } setOptions(options: ProjectionNodeOptions) { this.options = { ...this.options, ...options, crossfade: options.crossfade ?? true, } } clearMeasurements() { this.scroll = undefined this.layout = undefined this.snapshot = undefined this.prevTransformTemplateValue = undefined this.targetDelta = undefined this.target = undefined this.isLayoutDirty = false } /** * Frame calculations */ resolveTargetDelta() { const { layout, layoutId } = this.options /** * If we have no layout, we can't perform projection, so early return */ if (!this.layout || !(layout || layoutId)) return /** * If we don't have a targetDelta but do have a layout, we can attempt to resolve * a relativeParent. This will allow a component to perform scale correction * even if no animation has started. */ // TODO If this is unsuccessful this currently happens every frame if (!this.targetDelta && !this.relativeTarget) { // TODO: This is a semi-repetition of further down this function, make DRY this.relativeParent = this.getClosestProjectingParent() if (this.relativeParent && this.relativeParent.layout) { this.relativeTarget = createBox() this.relativeTargetOrigin = createBox() calcRelativePosition( this.relativeTargetOrigin, this.layout.actual, this.relativeParent.layout.actual ) copyBoxInto(this.relativeTarget, this.relativeTargetOrigin) } } /** * If we have no relative target or no target delta our target isn't valid * for this frame. */ if (!this.relativeTarget && !this.targetDelta) return /** * Lazy-init target data structure */ if (!this.target) { this.target = createBox() this.targetWithTransforms = createBox() } /** * If we've got a relative box for this component, resolve it into a target relative to the parent. */ if ( this.relativeTarget && this.relativeTargetOrigin && this.relativeParent?.target ) { calcRelativeBox( this.target, this.relativeTarget, this.relativeParent.target ) /** * If we've only got a targetDelta, resolve it into a target */ } else if (this.targetDelta) { if (Boolean(this.resumingFrom)) { // TODO: This is creating a new object every frame this.target = this.applyTransform(this.layout.actual) } else { copyBoxInto(this.target, this.layout.actual) } applyBoxDelta(this.target, this.targetDelta) } else { /** * If no target, use own layout as target */ copyBoxInto(this.target, this.layout.actual) } /** * If we've been told to attempt to resolve a relative target, do so. */ if (this.attemptToResolveRelativeTarget) { this.attemptToResolveRelativeTarget = false this.relativeParent = this.getClosestProjectingParent() if ( this.relativeParent && Boolean(this.relativeParent.resumingFrom) === Boolean(this.resumingFrom) && !this.relativeParent.options.layoutScroll && this.relativeParent.target ) { this.relativeTarget = createBox() this.relativeTargetOrigin = createBox() calcRelativePosition( this.relativeTargetOrigin, this.target, this.relativeParent.target ) copyBoxInto(this.relativeTarget, this.relativeTargetOrigin) } } } getClosestProjectingParent() { if (!this.parent || hasTransform(this.parent.latestValues)) return undefined if ( (this.parent.relativeTarget || this.parent.targetDelta) && this.parent.layout ) { return this.parent } else { return this.parent.getClosestProjectingParent() } } hasProjected: boolean = false calcProjection() { const { layout, layoutId } = this.options /** * If this section of the tree isn't animating we can * delete our target sources for the following frame. */ this.isTreeAnimating = Boolean( this.parent?.isTreeAnimating || this.currentAnimation || this.pendingAnimation ) if (!this.isTreeAnimating) { this.targetDelta = this.relativeTarget = undefined } if (!this.layout || !(layout || layoutId)) return const lead = this.getLead() /** * Reset the corrected box with the latest values from box, as we're then going * to perform mutative operations on it. */ copyBoxInto(this.layoutCorrected, this.layout.actual) /** * Apply all the parent deltas to this box to produce the corrected box. This * is the layout box, as it will appear on screen as a result of the transforms of its parents. */ applyTreeDeltas( this.layoutCorrected, this.treeScale, this.path, Boolean(this.resumingFrom) || this !== lead ) const { target } = lead if (!target) return if (!this.projectionDelta) { this.projectionDelta = createDelta() this.projectionDeltaWithTransform = createDelta() } const prevTreeScaleX = this.treeScale.x const prevTreeScaleY = this.treeScale.y const prevProjectionTransform = this.projectionTransform /** * Update the delta between the corrected box and the target box before user-set transforms were applied. * This will allow us to calculate the corrected borderRadius and boxShadow to compensate * for our layout reprojection, but still allow them to be scaled correctly by the user. * It might be that to simplify this we may want to accept that user-set scale is also corrected * and we wouldn't have to keep and calc both deltas, OR we could support a user setting * to allow people to choose whether these styles are corrected based on just the * layout reprojection or the final bounding box. */ calcBoxDelta( this.projectionDelta, this.layoutCorrected, target, this.latestValues ) this.projectionTransform = buildProjectionTransform( this.projectionDelta!, this.treeScale ) if ( this.projectionTransform !== prevProjectionTransform || this.treeScale.x !== prevTreeScaleX || this.treeScale.y !== prevTreeScaleY ) { this.hasProjected = true this.scheduleRender() this.notifyListeners("projectionUpdate", target) } } isVisible = true hide() { this.isVisible = false // TODO: Schedule render } show() { this.isVisible = true // TODO: Schedule render } scheduleRender(EnotifyAll = true) { this.options.scheduleRender?.() notifyAll && this.getStack()?.scheduleRender() Iif (this.resumingFrom && !this.resumingFrom.instance) { this.resumingFrom = undefined } } /** * Animation */ animationProgress = 0 animationValues?: ResolvedValues pendingAnimation?: Process currentAnimation?: AnimationPlaybackControls mixTargetDelta: (progress: number) => void setAnimationOrigin( delta: Delta, hasOnlyRelativeTargetChanged: boolean = false ) { const snapshot = this.snapshot const snapshotLatestValues = snapshot?.latestValues || {} const mixedValues = { ...this.latestValues } const targetDelta = createDelta() this.relativeTarget = this.relativeTargetOrigin = undefined this.attemptToResolveRelativeTarget = !hasOnlyRelativeTargetChanged const relativeLayout = createBox() const isSharedLayoutAnimation = snapshot?.isShared const isOnlyMember = (this.getStack()?.members.length || 0) <= 1 const shouldCrossfadeOpacity = Boolean( isSharedLayoutAnimation && !isOnlyMember && this.options.crossfade === true && !this.path.some(hasOpacityCrossfade) ) this.mixTargetDelta = (latest: number) => { const progress = latest / 1000 mixAxisDelta(targetDelta.x, delta.x, progress) mixAxisDelta(targetDelta.y, delta.y, progress) this.setTargetDelta(targetDelta) if ( this.relativeTarget && this.relativeTargetOrigin && this.layout && this.relativeParent?.layout ) { calcRelativePosition( relativeLayout, this.layout.actual, this.relativeParent.layout.actual ) mixBox( this.relativeTarget, this.relativeTargetOrigin, relativeLayout, progress ) } if (isSharedLayoutAnimation) { this.animationValues = mixedValues mixValues( mixedValues, snapshotLatestValues, this.latestValues, progress, shouldCrossfadeOpacity, isOnlyMember ) } this.root.scheduleUpdateProjection() this.scheduleRender() } this.mixTargetDelta(0) } startAnimation(options: AnimationOptions<number>) { this.currentAnimation?.stop() if (this.resumingFrom) { this.resumingFrom.currentAnimation?.stop() } if (this.pendingAnimation) { cancelSync.update(this.pendingAnimation) this.pendingAnimation = undefined } /** * Start the animation in the next frame to have a frame with progress 0, * where the target is the same as when the animation started, so we can * calculate the relative positions correctly for instant transitions. */ this.pendingAnimation = sync.update(() => { globalProjectionState.hasAnimatedSinceResize = true this.currentAnimation = animate(0, animationTarget, { ...(options as any), onUpdate: (latest: number) => { this.mixTargetDelta(latest) options.onUpdate?.(latest) }, onComplete: () => { options.onComplete?.() this.completeAnimation() }, }) if (this.resumingFrom) { this.resumingFrom.currentAnimation = this.currentAnimation } this.pendingAnimation = undefined }) } completeAnimation() { if (this.resumingFrom) { this.resumingFrom.currentAnimation = undefined this.resumingFrom.preserveOpacity = undefined } this.getStack()?.exitAnimationComplete() this.resumingFrom = this.currentAnimation = this.animationValues = undefined this.notifyListeners("animationComplete") } finishAnimation() { if (this.currentAnimation) { this.mixTargetDelta?.(animationTarget) this.currentAnimation.stop() } this.completeAnimation() } applyTransformsToTarget() { const { targetWithTransforms, target, layout, latestValues } = this.getLead() if (!targetWithTransforms || !target || !layout) return copyBoxInto(targetWithTransforms, target) /** * Apply the latest user-set transforms to the targetBox to produce the targetBoxFinal. * This is the final box that we will then project into by calculating a transform delta and * applying it to the corrected box. */ transformBox(targetWithTransforms, latestValues) /** * Update the delta between the corrected box and the final target box, after * user-set transforms are applied to it. This will be used by the renderer to * create a transform style that will reproject the element from its actual layout * into the desired bounding box. */ calcBoxDelta( this.projectionDeltaWithTransform!, this.layoutCorrected, targetWithTransforms!, latestValues ) } /** * Shared layout */ // TODO Only running on root node sharedNodes: Map<string, NodeStack> = new Map() registerSharedNode(layoutId: string, node: IProjectionNode) { if (!this.sharedNodes.has(layoutId)) { this.sharedNodes.set(layoutId, new NodeStack()) } const stack = this.sharedNodes.get(layoutId)! stack.add(node) node.promote({ transition: node.options.initialPromotionConfig?.transition, preserveFollowOpacity: node.options.initialPromotionConfig?.shouldPreserveFollowOpacity?.( node ), }) } isLead(): boolean { const stack = this.getStack() return stack ? stack.lead === this : true } getLead() { const { layoutId } = this.options return layoutId ? this.getStack()?.lead || this : this } getPrevLead() { const { layoutId } = this.options return layoutId ? this.getStack()?.prevLead : undefined } getStack() { const { layoutId } = this.options Iif (layoutId) return this.root.sharedNodes.get(layoutId) } promote({ needsReset, transition, preserveFollowOpacity, }: { needsReset?: boolean transition?: Transition preserveFollowOpacity?: boolean } = {}) { const stack = this.getStack() if (stack) stack.promote(this, preserveFollowOpacity) if (needsReset) { this.projectionDelta = undefined this.needsReset = true } if (transition) this.setOptions({ transition }) } relegate(): boolean { const stack = this.getStack() if (stack) { return stack.relegate(this) } else { return false } } resetRotation() { const { visualElement } = this.options if (!visualElement) return // If there's no detected rotation values, we can early return without a forced render. let hasRotate = false // Keep a record of all the values we've reset const resetValues = {} // Check the rotate value of all axes and reset to 0 for (let i = 0; i < transformAxes.length; i++) { const axis = transformAxes[i] const key = "rotate" + axis // If this rotation doesn't exist as a motion value, then we don't // need to reset it Eif (!visualElement.getStaticValue(key)) { continue } hasRotate = true // Record the rotation and then temporarily set it to 0 resetValues[key] = visualElement.getStaticValue(key) visualElement.setStaticValue(key, 0) } // If there's no rotation values, we don't need to do any more. Eif (!hasRotate) return // Force a render of this element to apply the transform with all rotations // set to 0. visualElement?.syncRender() // Put back all the values we reset for (const key in resetValues) { visualElement.setStaticValue(key, resetValues[key]) } // Schedule a render for the next frame. This ensures we won't visually // see the element with the reset rotate value applied. visualElement.scheduleRender() } getProjectionStyles(styleProp: MotionStyle = {}) { // TODO: Return lifecycle-persistent object const styles: ResolvedValues = {} Iif (!this.instance || this.isSVG) return styles Iif (!this.isVisible) { return { visibility: "hidden" } } else { styles.visibility = "" } const transformTemplate = this.options.visualElement?.getProps().transformTemplate Iif (this.needsReset) { this.needsReset = false styles.opacity = "" styles.pointerEvents = resolveMotionValue(styleProp.pointerEvents) || "" styles.transform = transformTemplate ? transformTemplate(this.latestValues, "") : "none" return styles } const lead = this.getLead() Eif (!this.projectionDelta || !this.layout || !lead.target) { const emptyStyles: ResolvedValues = {} Iif (this.options.layoutId) { emptyStyles.opacity = this.latestValues.opacity ?? 1 emptyStyles.pointerEvents = resolveMotionValue(styleProp.pointerEvents) || "" } Iif (this.hasProjected && !hasTransform(this.latestValues)) { emptyStyles.transform = transformTemplate ? transformTemplate({}, "") : "none" this.hasProjected = false } return emptyStyles } const valuesToRender = lead.animationValues || lead.latestValues this.applyTransformsToTarget() styles.transform = buildProjectionTransform( this.projectionDeltaWithTransform!, this.treeScale, valuesToRender ) if (transformTemplate) { styles.transform = transformTemplate( valuesToRender, styles.transform ) } const { x, y } = this.projectionDelta styles.transformOrigin = `${x.origin * 100}% ${y.origin * 100}% 0` if (lead.animationValues) { /** * If the lead component is animating, assign this either the entering/leaving * opacity */ styles.opacity = lead === this ? valuesToRender.opacity ?? this.latestValues.opacity ?? 1 : this.preserveOpacity ? this.latestValues.opacity : valuesToRender.opacityExit } else { /** * Or we're not animating at all, set the lead component to its actual * opacity and other components to hidden. */ styles.opacity = lead === this ? valuesToRender.opacity ?? "" : valuesToRender.opacityExit ?? 0 } /** * Apply scale correction */ for (const key in scaleCorrectors) { if (valuesToRender[key] === undefined) continue const { correct, applyTo } = scaleCorrectors[key] const corrected = correct(valuesToRender[key], lead) if (applyTo) { const num = applyTo.length for (let i = 0; i < num; i++) { styles[applyTo[i]] = corrected } } else { styles[key] = corrected } } /** * Disable pointer events on follow components. This is to ensure * that if a follow component covers a lead component it doesn't block * pointer events on the lead. */ if (this.options.layoutId) { styles.pointerEvents = lead === this ? resolveMotionValue(styleProp.pointerEvents) || "" : "none" } return styles } clearSnapshot() { this.resumeFrom = this.snapshot = undefined } // Only run on root resetTree() { this.root.nodes!.forEach((node: IProjectionNode) => node.currentAnimation?.stop() ) this.root.nodes!.forEach(clearMeasurements) this.root.sharedNodes.clear() } } } function updateLayout(node: IProjectionNode) { node.updateLayout() } function notifyLayoutUpdate(node: IProjectionNode) { const snapshot = node.resumeFrom?.snapshot ?? node.snapshot Iif ( node.isLead() && node.layout && snapshot && node.hasListeners("didUpdate") ) { const { actual: layout, measured: measuredLayout } = node.layout // TODO Maybe we want to also resize the layout snapshot so we don't trigger // animations for instance if layout="size" and an element has only changed position if (node.options.animationType === "size") { eachAxis((axis) => { const axisSnapshot = snapshot.isShared ? snapshot.measured[axis] : snapshot.layout[axis] const length = calcLength(axisSnapshot) axisSnapshot.min = layout[axis].min axisSnapshot.max = axisSnapshot.min + length }) } else if (node.options.animationType === "position") { eachAxis((axis) => { const axisSnapshot = snapshot.isShared ? snapshot.measured[axis] : snapshot.layout[axis] const length = calcLength(layout[axis]) axisSnapshot.max = axisSnapshot.min + length }) } const layoutDelta = createDelta() calcBoxDelta(layoutDelta, layout, snapshot.layout) const visualDelta = createDelta() if (snapshot.isShared) { calcBoxDelta( visualDelta, node.applyTransform(measuredLayout, true), snapshot.measured ) } else { calcBoxDelta(visualDelta, layout, snapshot.layout) } const hasLayoutChanged = !isDeltaZero(layoutDelta) let hasRelativeTargetChanged = false if (!node.resumeFrom) { node.relativeParent = node.getClosestProjectingParent() /** * If the relativeParent is itself resuming from a different element then * the relative snapshot is not relavent */ if (node.relativeParent && !node.relativeParent.resumeFrom) { const { snapshot: parentSnapshot, layout: parentLayout } = node.relativeParent if (parentSnapshot && parentLayout) { const relativeSnapshot = createBox() calcRelativePosition( relativeSnapshot, snapshot.layout, parentSnapshot.layout ) const relativeLayout = createBox() calcRelativePosition( relativeLayout, layout, parentLayout.actual ) if (!boxEquals(relativeSnapshot, relativeLayout)) { hasRelativeTargetChanged = true } } } } node.notifyListeners("didUpdate", { layout, snapshot, delta: visualDelta, layoutDelta, hasLayoutChanged, hasRelativeTargetChanged, }) } else Eif (node.isLead()) { node.options.onExitComplete?.() } /** * Clearing transition * TODO: Investigate why this transition is being passed in as {type: false } from Framer * and why we need it at all */ node.options.transition = undefined } function clearSnapshot(node: IProjectionNode) { node.clearSnapshot() } function clearMeasurements(node: IProjectionNode) { node.clearMeasurements() } function resetTransformStyle(node: IProjectionNode) { node.resetTransform() } function finishAnimation(node: IProjectionNode) { node.finishAnimation() node.targetDelta = node.relativeTarget = node.target = undefined } function resolveTargetDelta(node: IProjectionNode) { node.resolveTargetDelta() } function calcProjection(node: IProjectionNode) { node.calcProjection() } function resetRotation(node: IProjectionNode) { node.resetRotation() } function removeLeadSnapshots(stack: NodeStack) { stack.removeLeadSnapshot() } export function mixAxisDelta(output: AxisDelta, delta: AxisDelta, p: number) { output.translate = mix(delta.translate, 0, p) output.scale = mix(delta.scale, 1, p) output.origin = delta.origin output.originPoint = delta.originPoint } export function mixAxis(output: Axis, from: Axis, to: Axis, p: number) { output.min = mix(from.min, to.min, p) output.max = mix(from.max, to.max, p) } export function mixBox(output: Box, from: Box, to: Box, p: number) { mixAxis(output.x, from.x, to.x, p) mixAxis(output.y, from.y, to.y, p) } function hasOpacityCrossfade(node: IProjectionNode) { return ( node.animationValues && node.animationValues.opacityExit !== undefined ) } const defaultLayoutTransition = { duration: 0.45, ease: [0.4, 0, 0.1, 1], } function mountNodeEarly(node: IProjectionNode, id: number) { /** * Rather than searching the DOM from document we can search the * path for the deepest mounted ancestor and search from there */ let searchNode = node.root for (let i = node.path.length - 1; i >= 0; i--) { Eif (Boolean(node.path[i].instance)) { searchNode = node.path[i] break } } const searchElement = searchNode && searchNode !== node.root ? searchNode.instance : document const element = (searchElement as Element).querySelector( `[data-projection-id="${id}"]` ) Iif (element) node.mount(element, true) } function roundAxis(axis: Axis): void { axis.min = Math.round(axis.min) axis.max = Math.round(axis.max) } function roundBox(box: Box): void { roundAxis(box.x) roundAxis(box.y) } |