Код: Выделить всё
export function moveElement(el: HTMLElement, options: {
toLeft?: string | undefined;
toTop?: string | undefined;
durationMs?: number | undefined;
fromLeft?: string | undefined;
fromTop?: string | undefined;
signal?: AbortSignal | undefined;
runInAnimationFrame?: boolean | undefined;
}): Promise {
if (runInAnimationFrame) {
return runInAnimationFrame(() => {
moveElementIntern(el, options);
})
} else {
return moveElementIntern(el, options);
}
}
< /code>
moveElementIntern()Код: Выделить всё
export function runInAnimationFrame(cb: (ms: number) => void, signal?: AbortSignal): Promise {
return new Promise((res, rej) => {
function wrappedCb(ms: number) {
try {
cb(ms);
res();
} catch (reason) {
rej(reason);
} finally {
signal?.removeEventListener('abort', onAbort);
}
}
const id = requestAnimationFrame(wrappedCb);
function onAbort() {
assert(signal?.aborted);
cancelAnimationFrame(id);
signal.removeEventListener('abort', onAbort);
rej(signal.reason);
}
signal?.addEventListener('abort', onAbort);
})
}
< /code>
Now, as I imagine for example 500 calls to moveElementКод: Выделить всё
function moveElements(tasks: {el: HTMLElement, options: { ... }}[]) { ... }
< /code>
This would mean that requestAnimationFrame is only called about 2 times instead of about 1000 times. But there would be much more logic needed in moveElementsПодробнее здесь: https://stackoverflow.com/questions/797 ... ationframe
Мобильная версия