[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,6 +136,7 @@ function scrollWithJs(
const startAt = Date.now(); const startAt = Date.now();
const onHeavyAnimationStop = dispatchHeavyAnimationEvent(); const onHeavyAnimationStop = dispatchHeavyAnimationEvent();
fastRaf(() => {
animateSingle(() => { animateSingle(() => {
const t = Math.min((Date.now() - startAt) / duration, 1); const t = Math.min((Date.now() - startAt) / duration, 1);
const currentPath = path * (1 - transition(t)); const currentPath = path * (1 - transition(t));
@ -149,6 +151,7 @@ function scrollWithJs(
return isAnimating; return isAnimating;
}); });
});
} }
function longTransition(t: number) { function longTransition(t: number) {

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,6 +40,7 @@ function scrollWithJs(container: HTMLElement, left: number, duration: number) {
const startAt = Date.now(); const startAt = Date.now();
fastRaf(() => {
animate(() => { animate(() => {
const t = Math.min((Date.now() - startAt) / duration, 1); const t = Math.min((Date.now() - startAt) / duration, 1);
@ -47,6 +49,7 @@ function scrollWithJs(container: HTMLElement, left: number, duration: number) {
return t < 1; return t < 1;
}); });
});
} }
function transition(t: number) { function transition(t: number) {

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,6 +55,7 @@ export default (theme: ISettings['theme'], withAnimation: boolean) => {
isInitialized = true; isInitialized = true;
if (shouldAnimate) { if (shouldAnimate) {
fastRaf(() => {
animateSingle(() => { animateSingle(() => {
const t = Math.min((Date.now() - startAt) / DURATION_MS, 1); const t = Math.min((Date.now() - startAt) / DURATION_MS, 1);
@ -61,6 +63,7 @@ export default (theme: ISettings['theme'], withAnimation: boolean) => {
return t < 1; return t < 1;
}); });
});
} else { } else {
applyColorAnimationStep(startIndex, endIndex); applyColorAnimationStep(startIndex, endIndex);
} }