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
7
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
import { float64, isFiniteNumber, lerp } from "@koalaui/common"
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.
22
export type AnimationRange<Value> = (value: float64) => Value
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
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])
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
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)