From 90b3dee802c1b88a5980dfa46363e9f8c157a532 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Sun, 15 Nov 2026 18:51:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8A=A8=E7=94=BB=E5=92=8C=E6=B8=90?= =?UTF-8?q?=E5=8F=98=E7=9A=84=E9=9B=B6=E6=97=B6=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/animate/src/animater.ts | 42 ++++++++++++++++++------------ packages/animate/src/transition.ts | 27 ++++++++++++++----- packages/animate/src/utils.ts | 22 ++++++++-------- 3 files changed, 57 insertions(+), 34 deletions(-) diff --git a/packages/animate/src/animater.ts b/packages/animate/src/animater.ts index cbed2bc..95138de 100644 --- a/packages/animate/src/animater.ts +++ b/packages/animate/src/animater.ts @@ -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); + } } /** diff --git a/packages/animate/src/transition.ts b/packages/animate/src/transition.ts index adb722d..e1c668c 100644 --- a/packages/animate/src/transition.ts +++ b/packages/animate/src/transition.ts @@ -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(); 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); diff --git a/packages/animate/src/utils.ts b/packages/animate/src/utils.ts index 43e7a14..a5b81dd 100644 --- a/packages/animate/src/utils.ts +++ b/packages/animate/src/utils.ts @@ -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; } /**