fix: 动画和渐变的零时检测

This commit is contained in:
AncTe committed 2026-11-15 18:51:04 +08:00
1 parent 5db2cefb39
commit 90b3dee802
3 files changed
+57 -34

No files matched your search

+26 -16
View File
@@ -10,6 +10,7 @@ import {
EndRelation
} from './types';
import { linear } from './utils';
import { clamp } from 'lodash-es';
interface IAnimaterTimedInfo extends IAnimatePlanIdentifier {
/** 等待时长 */
@@ -337,6 +338,21 @@ export class Animater implements IAnimater {
//#region 动画执行
private endAnimate(anim: IAnimatingContent) {
if (!this.excitation) return;
const content = anim.identifier.content;
if (anim.end === EndRelation.Target) {
content.value = anim.targetValue;
} else if (anim.end === EndRelation.Curve) {
content.value = anim.startValue + anim.curve(1) * anim.diff;
} else {
const elapsed = this.excitation.payload() - anim.startTime;
const progress = clamp(elapsed / anim.time, 0, 1);
const curveValue = anim.curve(progress);
content.value = anim.startValue + anim.diff * curveValue;
}
}
excited(payload: number): void {
if (this.planning) {
if (!this.planErrored) {
@@ -396,11 +412,7 @@ export class Animater implements IAnimater {
const content = anim.identifier.content;
if (progress >= 1) {
// 动画结束
if (anim.end === EndRelation.Target) {
content.value = anim.targetValue;
} else {
content.value = anim.startValue + anim.curve(1) * anim.diff;
}
this.endAnimate(anim);
anim.resolve();
endedAnimate.add(anim);
anim.after.forEach(v => afters.add(v));
@@ -452,18 +464,9 @@ export class Animater implements IAnimater {
}
// 终止前一个动画
curr.resolve();
// 这里也需要
const anim = curr.identifier.content;
if (curr.end === EndRelation.Target) {
anim.value = curr.targetValue;
} else if (curr.end === EndRelation.Curve) {
anim.value = curr.startValue + curr.curve(1) * curr.diff;
} else {
const elapsed = this.excitation.payload() - curr.startTime;
const curveValue = curr.curve(elapsed / curr.time);
anim.value = curr.startValue + curr.diff * curveValue;
}
this.endAnimate(curr);
this.executing.delete(curr);
this.executingMap.delete(identifier.content);
}
// 记录动画初始值和开始时间
const startValue = identifier.content.value;
@@ -480,6 +483,13 @@ export class Animater implements IAnimater {
for (const when of plan.when) {
this.startWhen({ ...when, arousedTime: startTime });
}
// 零时检测
if (anim.time === 0) {
this.endAnimate(anim);
this.executing.delete(anim);
this.executingMap.delete(identifier.content);
}
}
/**
+20 -7
View File
@@ -69,6 +69,19 @@ export class Transition implements ITransition {
return this;
}
/**
* 结束某一渐变操作
* @param anim 可动画对象
* @param data 渐变数据对象
*/
private endTransition(anim: IAnimatable, data: ITransitionData) {
if (data.end === EndRelation.Target) {
anim.value = data.targetValue;
} else {
anim.value = data.startValue + data.diff * data.curve(1);
}
}
to(
value: number,
time: number,
@@ -79,7 +92,7 @@ export class Transition implements ITransition {
const now = this.excitation.payload();
const data = this.transitionData.getOrInsertComputed(
this.animatableStatus,
_ => {
() => {
const { promise, resolve } = Promise.withResolvers<void>();
return {
animatable,
@@ -102,6 +115,11 @@ export class Transition implements ITransition {
data.targetValue = value;
data.diff = value - animatable.value;
data.end = end;
if (time === 0) {
this.endTransition(animatable, data);
data.resolve();
this.transitionData.delete(animatable);
}
return this;
}
@@ -132,14 +150,9 @@ export class Transition implements ITransition {
const progress = elapsed / data.time;
if (progress >= 1) {
// 动画结束
if (data.end === EndRelation.Target) {
anim.value = data.targetValue;
} else {
anim.value = data.startValue + data.diff * data.curve(1);
}
this.endTransition(anim, data);
data.resolve();
toDelete.add(anim);
this.transitionData.delete(anim);
} else {
// 动画进行中
const curveValue = data.curve(progress);
+11 -11
View File
@@ -336,7 +336,7 @@ const sinfunc: ExcitationCurve = p => 1 - Math.cos((p * Math.PI) / 2);
/**
* 正弦速率曲线,EaseIn: `f(x) = 1 - cos(x * pi/2), x∈[0,1], f(x)∈[0,1]`
* @param mode 曲线模式
* @param mode 曲线模式,默认为缓入
*/
export function sin(mode: CurveMode = CurveMode.EaseIn): ExcitationCurve {
return applyCurveMode(sinfunc, mode);
@@ -347,7 +347,7 @@ const tanfunc: ExcitationCurve = p => Math.tan((p * Math.PI) / 4);
/**
* 正切速率曲线,EaseIn: `f(x) = tan(x * pi/4), x∈[0,1], f(x)∈[0,1]`
* @param mode 曲线模式
* @param mode 曲线模式,默认为缓入
*/
export function tan(mode: CurveMode = CurveMode.EaseIn): ExcitationCurve {
return applyCurveMode(tanfunc, mode);
@@ -358,7 +358,7 @@ const secfunc: ExcitationCurve = p => 1 / Math.cos((p * Math.PI) / 3) - 1;
/**
* 正割速率曲线,EaseIn: `f(x) = sec(x * pi/3)-1, x∈[0,1], f(x)∈[0,1]`
* @param mode 曲线模式
* @param mode 曲线模式,默认为缓入
*/
export function sec(mode: CurveMode = CurveMode.EaseIn): ExcitationCurve {
return applyCurveMode(secfunc, mode);
@@ -367,7 +367,7 @@ export function sec(mode: CurveMode = CurveMode.EaseIn): ExcitationCurve {
/**
* 幂函数速率曲线,EaseIn: `f(x) = x ** n, x∈[0,1], f(x)∈[0,1]`
* @param exp 指数
* @param mode 曲线模式
* @param mode 曲线模式,默认为缓入
*/
export function pow(
exp: number,
@@ -380,8 +380,8 @@ export function pow(
/**
* 双曲余弦速率曲线,EaseIn: `f(x) = (cosh(x * k) - 1) / (cosh(k) - 1), x∈[0,1], f(x)∈[0,1]`
* @param k 比例参数
* @param mode 曲线模式
* @param k 比例参数,默认为 2
* @param mode 曲线模式,默认为缓入
*/
export function cosh(
k: number = 2,
@@ -395,8 +395,8 @@ export function cosh(
/**
* 双曲正切速率曲线,EaseIn: `f(x) = 1 + tanh((x - 1) * k) / tanh(k), x∈[0,1], f(x)∈[0,1]`
* @param k 比例参数
* @param mode 曲线模式
* @param k 比例参数,默认为 2
* @param mode 曲线模式,默认为缓入
*/
export function tanh(
k: number = 2,
@@ -410,8 +410,8 @@ export function tanh(
/**
* 双曲正割速率曲线,EaseIn: `f(x) = 1 - sech(x * k) / sech(k), x∈[0,1], f(x)∈[0,1]`
* @param k 比例参数
* @param mode 曲线模式
* @param k 比例参数,默认为 2
* @param mode 曲线模式,默认为缓入
*/
export function sech(
k: number = 2,
@@ -429,7 +429,7 @@ export function sech(
* @param k 常数值
*/
export function constant(k: number): ExcitationCurve {
return _ => k;
return () => k;
}
/**