Skip to content

Commit 652eec8

Browse files
committed
Animation performance improvements
1 parent 1c7847b commit 652eec8

File tree

3 files changed

+11
-35
lines changed

3 files changed

+11
-35
lines changed

src/Animation.js

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getRect, css, isScrolledPast, matrix, isRectEqual, indexOfObject } from './utils.js';
1+
import { getRect, css, matrix, isRectEqual, indexOfObject } from './utils.js';
22
import Sortable from './Sortable.js';
33

44
export default function AnimationStateManager() {
@@ -17,7 +17,7 @@ export default function AnimationStateManager() {
1717
target: child,
1818
rect: getRect(child)
1919
});
20-
let fromRect = getRect(child);
20+
let fromRect = { ...animationStates[animationStates.length - 1].rect };
2121

2222
// If animating: compensate for current animation
2323
if (child.thisAnimationDuration) {
@@ -70,29 +70,6 @@ export default function AnimationStateManager() {
7070

7171
target.toRect = toRect;
7272

73-
// If element is scrolled out of view: Do not animate
74-
if (
75-
(
76-
isScrolledPast(target, toRect, 'bottom', 'top') ||
77-
isScrolledPast(target, toRect, 'top', 'bottom') ||
78-
isScrolledPast(target, toRect, 'right', 'left') ||
79-
isScrolledPast(target, toRect, 'left', 'right')
80-
) &&
81-
(
82-
isScrolledPast(target, animatingRect, 'bottom', 'top') ||
83-
isScrolledPast(target, animatingRect, 'top', 'bottom') ||
84-
isScrolledPast(target, animatingRect, 'right', 'left') ||
85-
isScrolledPast(target, animatingRect, 'left', 'right')
86-
) &&
87-
(
88-
isScrolledPast(target, fromRect, 'bottom', 'top') ||
89-
isScrolledPast(target, fromRect, 'top', 'bottom') ||
90-
isScrolledPast(target, fromRect, 'right', 'left') ||
91-
isScrolledPast(target, fromRect, 'left', 'right')
92-
)
93-
) return;
94-
95-
9673
if (target.thisAnimationDuration) {
9774
// Could also check if animatingRect is between fromRect and toRect
9875
if (
@@ -120,6 +97,7 @@ export default function AnimationStateManager() {
12097
this.animate(
12198
target,
12299
animatingRect,
100+
toRect,
123101
time
124102
);
125103
}
@@ -151,16 +129,15 @@ export default function AnimationStateManager() {
151129
animationStates = [];
152130
},
153131

154-
animate(target, prev, duration) {
132+
animate(target, currentRect, toRect, duration) {
155133
if (duration) {
156134
css(target, 'transition', '');
157135
css(target, 'transform', '');
158-
let currentRect = getRect(target),
159-
elMatrix = matrix(this.el),
136+
let elMatrix = matrix(this.el),
160137
scaleX = elMatrix && elMatrix.a,
161138
scaleY = elMatrix && elMatrix.d,
162-
translateX = (prev.left - currentRect.left) / (scaleX || 1),
163-
translateY = (prev.top - currentRect.top) / (scaleY || 1);
139+
translateX = (currentRect.left - toRect.left) / (scaleX || 1),
140+
translateY = (currentRect.top - toRect.top) / (scaleY || 1);
164141

165142
target.animatingX = !!translateX;
166143
target.animatingY = !!translateY;

src/Sortable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,10 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
552552
el = _this.el,
553553
options = _this.options,
554554
ownerDocument = el.ownerDocument,
555-
dragRect = getRect(target),
556555
dragStartFn;
557556

558557
if (target && !dragEl && (target.parentNode === el)) {
558+
let dragRect = getRect(target);
559559
rootEl = el;
560560
dragEl = target;
561561
parentEl = dragEl.parentNode;
@@ -1194,7 +1194,7 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
11941194
differentLevel = dragEl.parentNode !== el,
11951195
differentRowCol = !_dragElInRowColumn(dragEl.animated && dragEl.toRect || dragRect, target.animated && target.toRect || targetRect, vertical),
11961196
side1 = vertical ? 'top' : 'left',
1197-
scrolledPastTop = isScrolledPast(target, null, 'top', 'top') || isScrolledPast(dragEl, null, 'top', 'top'),
1197+
scrolledPastTop = isScrolledPast(target, 'top', 'top') || isScrolledPast(dragEl, 'top', 'top'),
11981198
scrollBefore = scrolledPastTop ? scrolledPastTop.scrollTop : void 0;
11991199

12001200

src/utils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,13 @@ function getRect(el, relativeToContainingBlock, relativeToNonStaticParent, undoS
255255
/**
256256
* Checks if a side of an element is scrolled past a side of its parents
257257
* @param {HTMLElement} el The element who's side being scrolled out of view is in question
258-
* @param {[DOMRect]} rect Optional rect of `el` to use
259258
* @param {String} elSide Side of the element in question ('top', 'left', 'right', 'bottom')
260259
* @param {String} parentSide Side of the parent in question ('top', 'left', 'right', 'bottom')
261260
* @return {HTMLElement} The parent scroll element that the el's side is scrolled past, or null if there is no such element
262261
*/
263-
function isScrolledPast(el, rect, elSide, parentSide) {
262+
function isScrolledPast(el, elSide, parentSide) {
264263
let parent = getParentAutoScrollElement(el, true),
265-
elSideVal = (rect ? rect : getRect(el))[elSide];
264+
elSideVal = getRect(el)[elSide];
266265

267266
/* jshint boss:true */
268267
while (parent) {

0 commit comments

Comments
 (0)