[Perf] Animation: Allow to start immediately

This commit is contained in:
Alexander Zinchuk 2022-01-24 04:42:05 +01:00
parent a317e6be15
commit 43e4492102
4 changed files with 35 additions and 26 deletions

View File

@ -16,19 +16,19 @@ export function animateSingle(tick: Function, instance?: AnimationInstance) {
currentInstance = instance; currentInstance = instance;
} }
fastRaf(() => { if (!instance!.isCancelled && tick()) {
if (!instance!.isCancelled && tick()) { fastRaf(() => {
animateSingle(tick, instance); animateSingle(tick, instance);
} });
}); }
} }
export function animate(tick: Function) { export function animate(tick: Function) {
fastRaf(() => { if (tick()) {
if (tick()) { fastRaf(() => {
animate(tick); animate(tick);
} });
}); }
} }
export type TimingFn = (t: number) => number; export type TimingFn = (t: number) => number;

View File

@ -10,6 +10,7 @@ import {
import { IS_ANDROID } from './environment'; import { IS_ANDROID } from './environment';
import { dispatchHeavyAnimationEvent } from '../hooks/useHeavyAnimationCheck'; import { dispatchHeavyAnimationEvent } from '../hooks/useHeavyAnimationCheck';
import { animateSingle } from './animation'; import { animateSingle } from './animation';
import { fastRaf } from './schedulers';
let isAnimating = false; let isAnimating = false;
@ -135,19 +136,21 @@ function scrollWithJs(
const startAt = Date.now(); const startAt = Date.now();
const onHeavyAnimationStop = dispatchHeavyAnimationEvent(); const onHeavyAnimationStop = dispatchHeavyAnimationEvent();
animateSingle(() => { fastRaf(() => {
const t = Math.min((Date.now() - startAt) / duration, 1); animateSingle(() => {
const currentPath = path * (1 - transition(t)); const t = Math.min((Date.now() - startAt) / duration, 1);
const currentPath = path * (1 - transition(t));
container.scrollTop = Math.round(target - currentPath); container.scrollTop = Math.round(target - currentPath);
isAnimating = t < 1; isAnimating = t < 1;
if (!isAnimating) { if (!isAnimating) {
onHeavyAnimationStop(); onHeavyAnimationStop();
} }
return isAnimating; return isAnimating;
});
}); });
} }

View File

@ -2,6 +2,7 @@ import { getGlobal } from '../lib/teact/teactn';
import { ANIMATION_LEVEL_MIN } from '../config'; import { ANIMATION_LEVEL_MIN } from '../config';
import { animate } from './animation'; import { animate } from './animation';
import { fastRaf } from './schedulers';
const DEFAULT_DURATION = 300; const DEFAULT_DURATION = 300;
@ -39,13 +40,15 @@ function scrollWithJs(container: HTMLElement, left: number, duration: number) {
const startAt = Date.now(); const startAt = Date.now();
animate(() => { fastRaf(() => {
const t = Math.min((Date.now() - startAt) / duration, 1); animate(() => {
const t = Math.min((Date.now() - startAt) / duration, 1);
const currentPath = path * (1 - transition(t)); const currentPath = path * (1 - transition(t));
container.scrollLeft = Math.round(target - currentPath); container.scrollLeft = Math.round(target - currentPath);
return t < 1; return t < 1;
});
}); });
} }

View File

@ -1,6 +1,7 @@
import { ISettings } from '../types'; import { ISettings } from '../types';
import { animateSingle } from './animation'; import { animateSingle } from './animation';
import { fastRaf } from './schedulers';
import themeColors from '../styles/themes.json'; import themeColors from '../styles/themes.json';
@ -54,12 +55,14 @@ export default (theme: ISettings['theme'], withAnimation: boolean) => {
isInitialized = true; isInitialized = true;
if (shouldAnimate) { if (shouldAnimate) {
animateSingle(() => { fastRaf(() => {
const t = Math.min((Date.now() - startAt) / DURATION_MS, 1); animateSingle(() => {
const t = Math.min((Date.now() - startAt) / DURATION_MS, 1);
applyColorAnimationStep(startIndex, endIndex, transition(t)); applyColorAnimationStep(startIndex, endIndex, transition(t));
return t < 1; return t < 1;
});
}); });
} else { } else {
applyColorAnimationStep(startIndex, endIndex); applyColorAnimationStep(startIndex, endIndex);