|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { logger } from "matrix-js-sdk/src/logger"; |
| 18 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 19 | + |
| 20 | +import SettingsStore from "../settings/SettingsStore"; |
| 21 | +import { useAnimation } from "./useAnimation"; |
| 22 | + |
| 23 | +const debuglog = (...args: any[]) => { |
| 24 | + if (SettingsStore.getValue("debug_animation")) { |
| 25 | + logger.log.call(console, "Animation debuglog:", ...args); |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Utility function to smoothly animate to a certain target value |
| 31 | + * @param initialValue Initial value to be used as initial starting point |
| 32 | + * @param targetValue Desired value to animate to (can be changed repeatedly to whatever is current at that time) |
| 33 | + * @param duration Duration that each animation should take |
| 34 | + * @param enabled Whether the animation should run or not |
| 35 | + */ |
| 36 | +export function useSmoothAnimation( |
| 37 | + initialValue: number, |
| 38 | + targetValue: number, |
| 39 | + duration: number, |
| 40 | + enabled: boolean, |
| 41 | +): number { |
| 42 | + const state = useRef<{ timestamp: DOMHighResTimeStamp | null, value: number }>({ |
| 43 | + timestamp: null, |
| 44 | + value: initialValue, |
| 45 | + }); |
| 46 | + const [currentValue, setCurrentValue] = useState<number>(initialValue); |
| 47 | + const [currentStepSize, setCurrentStepSize] = useState<number>(0); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + const totalDelta = targetValue - state.current.value; |
| 51 | + setCurrentStepSize(totalDelta / duration); |
| 52 | + state.current = { ...state.current, timestamp: null }; |
| 53 | + }, [duration, targetValue]); |
| 54 | + |
| 55 | + const update = useCallback( |
| 56 | + (timestamp: DOMHighResTimeStamp): boolean => { |
| 57 | + if (!state.current.timestamp) { |
| 58 | + state.current = { ...state.current, timestamp }; |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + if (Math.abs(currentStepSize) < Number.EPSILON) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + const timeDelta = timestamp - state.current.timestamp; |
| 67 | + const valueDelta = currentStepSize * timeDelta; |
| 68 | + const maxValueDelta = targetValue - state.current.value; |
| 69 | + const clampedValueDelta = Math.sign(valueDelta) * Math.min(Math.abs(maxValueDelta), Math.abs(valueDelta)); |
| 70 | + const value = state.current.value + clampedValueDelta; |
| 71 | + |
| 72 | + debuglog(`Animating to ${targetValue} at ${value} timeDelta=${timeDelta}, valueDelta=${valueDelta}`); |
| 73 | + |
| 74 | + setCurrentValue(value); |
| 75 | + state.current = { value, timestamp }; |
| 76 | + |
| 77 | + return Math.abs(maxValueDelta) > Number.EPSILON; |
| 78 | + }, |
| 79 | + [currentStepSize, targetValue], |
| 80 | + ); |
| 81 | + |
| 82 | + useAnimation(enabled, update); |
| 83 | + |
| 84 | + return currentValue; |
| 85 | +} |
0 commit comments