feat: 添加循环渐变动画功能方法

This commit is contained in:
AncTe committed 2026-11-15 17:05:41 +08:00
1 parent 35a5056b07
commit 2744a76e07
2 files changed
+117 -10

No files matched your search

+21 -8
View File
@@ -11,17 +11,12 @@ export type DefaultProps<
//#region 功能接口
export interface ITransitionedController<T> {
export interface ITransitionedBase<T> {
/** 响应式对象,可以直接写作元素 prop */
readonly ref: Readonly<Ref<T>>;
/** 当前值 */
readonly value: T;
/**
* 执行动画,使当前值缓慢变化至目标值
* @param value 目标值
* @param time 动画时长
*/
set(value: T, time?: number): void;
/**
* 设置动画的速率曲线
* @param timing 速率曲线
@@ -35,4 +30,22 @@ export interface ITransitionedController<T> {
setTime(time: number): void;
}
export interface ITransitionedController<T> extends ITransitionedBase<T> {
/**
* 执行动画,使当前值缓慢变化至目标值
* @param value 目标值
* @param time 动画时长
*/
set(value: T, time?: number): void;
}
export interface ICycledTransitionController extends ITransitionedBase<number> {
/**
* 设置循环渐变的范围
* @param min 要设置为的最小值
* @param max 要设置为的最大值
*/
setRange(min: number, max: number): void;
}
//#endregion
+96 -2
View File
@@ -28,7 +28,7 @@ import {
ExcitationCurve,
IAnimatable
} from '@motajs/animate';
import { ITransitionedController } from './types';
import { ICycledTransitionController, ITransitionedController } from './types';
//#region 函数功能接口
@@ -276,7 +276,8 @@ export function transitioned(
}
/**
* 创建一个渐变颜色,当颜色发生变化时会缓慢变化值目标值而非突变,自动判断颜色字符串类型,只可用于组件内,不可用于组件外
* 创建一个渐变颜色,当颜色发生变化时会缓慢变化值目标值而非突变,自动判断颜色字符串类型,只可用于组件内,不可用于组件外。
* 目前支持 `rgba` `rgb` 颜色字符串和十六进制颜色字符串
* ```tsx
* // 创建渐变,初始值为 '#fff'(白色),渐变时长 300ms,线性变化
* const color = transitionedColor('#fff', 300, linear());
@@ -299,6 +300,41 @@ export function transitionedColor(
return new RenderColorTransition(color, getGlobalTransition(), time, curve);
}
/**
* 创建一个渐变循环,渐变值会在最大值和最小值之间反复变换,常用于播放循环动画,只可用于组件内,不可用于组件外
*
* ```tsx
* // 创建渐变循环,alpha 会在 [0.2, 0.8] 的区间内来回变化
* const alpha = cycledTransition(0.2, 0.8, 1000, linear());
*
* // 直接在元素上使用
* <text text='文本' alpha={alpha.ref.value} />
* ```
* @param min 渐变循环的最小值
* @param max 渐变循环的最大值
* @param time 渐变循环的单程耗时
* @param curve 渐变循环使用的速率曲线
* @param maxStart 是否从 `max` 开始渐变循环,默认会从 `min` 开始渐变循环
*/
export function cycledTransition(
min: number,
max: number,
time: number,
curve: ExcitationCurve,
maxStart: boolean = false
): ICycledTransitionController | null {
const renderer = useRendererRoot();
if (!renderer) return null;
return new CycledTransition(
min,
max,
getGlobalTransition(),
time,
curve,
maxStart
);
}
//#endregion
//#region 渐变
@@ -325,6 +361,7 @@ class RenderTransition implements ITransitionedController<number> {
public curve: ExcitationCurve
) {
this.ref = ref(value);
this.pendingValue = value;
watch(transition, (value, old) => {
if (value && !old) {
@@ -391,6 +428,7 @@ class RenderColorTransition implements ITransitionedController<string> {
this.bValue = ref(b);
this.aValue = ref(a);
this.ref = computed(() => this.encodeColor());
this.pendingValue = [r, g, b, a];
watch(transition, (value, old) => {
if (value && !old) {
@@ -519,4 +557,60 @@ class RenderColorTransition implements ITransitionedController<string> {
}
}
class CycledTransition
extends RenderTransition
implements ICycledTransitionController
{
private stop: boolean = false;
constructor(
private min: number,
private max: number,
transition: ComputedRef<ITransition | null>,
time: number,
curve: ExcitationCurve,
maxStart: boolean
) {
super(maxStart ? max : min, transition, time, curve);
watch(transition, value => {
if (value) {
this.startCycle();
this.check();
}
});
}
/**
* 检查当前状态
*/
private check() {
if (this.ref.value <= this.min) {
this.set(this.max);
} else if (this.ref.value >= this.max) {
this.set(this.min);
} else {
this.set(this.min);
}
}
setRange(min: number, max: number): void {
this.min = min;
this.max = max;
this.check();
}
/**
* 开始动画循环
*/
private async startCycle() {
while (true) {
if (this.stop || !this.transition.value) break;
const tran = this.transition.value;
await tran.wait(this.ref);
this.check();
}
}
}
//#endregion