idlize

Форк
0
55 строк · 2.4 Кб
1
/*
2
 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15

16
import { float64, isFiniteNumber, lerp } from "@koalaui/common"
17

18
/**
19
 * Declares a function that can convert a state to a value.
20
 * Note that some easings may produce a state outside the `[0..1]` range.
21
 */
22
export type AnimationRange<Value> = (value: float64) => Value
23

24
/**
25
 * @param from - a first base array that corresponds to the `0` state
26
 * @param to - a second base array that corresponds to the `1` state
27
 * @returns a function to generate interpolated values
28
 * @see ColorAnimationRange
29
 * @see NumberAnimationRange
30
 */
31
export function ArrayAnimationRange<Value>(from: ReadonlyArray<float64>, to: ReadonlyArray<float64>, compute: (array: ReadonlyArray<float64>) => Value): AnimationRange<Value> {
32
    const length = from.length
33
    if (to.length != length) throw new Error("sizes of input arrays do not match")
34
    const array = new Array<float64>(length)
35
    return (weight: float64) => {
36
        if (from.length != length) throw new Error("size of the first input array is changed unexpectedly")
37
        if (to.length != length) throw new Error("size of the second input array is changed unexpectedly")
38
        for (let index = 0; index < length; index++) {
39
            array[index] = lerp(weight, from[index], to[index])
40
        }
41
        return compute(array)
42
    }
43
}
44

45
/**
46
 * @param from - a first base value that corresponds to the `0` state
47
 * @param to - a second base value that corresponds to the `1` state
48
 * @returns a function to generate interpolated numbers
49
 */
50
export function NumberAnimationRange(from: float64, to: float64): AnimationRange<float64> {
51
    if (from == 0 && to == 1) return (state: float64) => state
52
    if (!isFiniteNumber(from)) throw new Error("illegal start value: " + from)
53
    if (!isFiniteNumber(to)) throw new Error("illegal end value: " + to)
54
    return (weight: float64) => lerp(weight, from, to)
55
}
56

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.