Skip to content

Commit e2c3430

Browse files
Fix alpha interpolation in universalSoftIn/Off animations
Updated universalSoftIn and universalSoftOff animation logic to use the current alpha as the starting value instead of forcing a fixed initial alpha. Alpha transitions now use linear interpolation based on the eased progress, ensuring smoother and more accurate fade-in and fade-out effects.
1 parent c5f237c commit e2c3430

2 files changed

Lines changed: 25 additions & 26 deletions

File tree

packages/webgal/src/Core/controller/stage/pixi/animations/universalSoftIn.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@ export function generateUniversalSoftInAnimationObj(targetKey: string, duration:
44
const target = WebGAL.gameplay.pixiStage!.getStageObjByKey(targetKey);
55
let elapsedTime = 0;
66

7-
// 先设置一个通用的初态
7+
// 新增变量,用于存储动画开始时的初始透明度
8+
let startAlpha = 0;
9+
810
/**
911
* 在此书写为动画设置初态的操作
1012
*/
1113
function setStartState() {
1214
elapsedTime = 0; // Reset timer when animation starts
1315
if (target) {
14-
target.pixiContainer.alpha = 0;
16+
// 修正:不再强制设为 0,而是记录当前的透明度
17+
startAlpha = target.pixiContainer.alpha;
1518
}
1619
}
1720

18-
// TODO:通用终态设置
1921
/**
2022
* 在此书写为动画设置终态的操作
2123
*/
2224
function setEndState() {
2325
if (target) {
26+
// 终态是完全不透明,这保持不变
2427
target.pixiContainer.alpha = 1;
2528
}
2629
}
@@ -34,21 +37,19 @@ export function generateUniversalSoftInAnimationObj(targetKey: string, duration:
3437
const sprite = target.pixiContainer;
3538
const baseDuration = WebGAL.gameplay.pixiStage!.frameDuration;
3639

37-
// Increment the elapsed time by the duration of the last frame
3840
elapsedTime += baseDuration;
3941

40-
// Ensure elapsedTime does not exceed the total duration
4142
const realElapsedTime = Math.min(elapsedTime, duration);
42-
43-
// Calculate the progress of the animation as a value from 0 to 1
4443
const progress = realElapsedTime / duration;
4544

46-
// Apply the Cubic Ease-Out function
47-
// The formula is: 1 - (1 - progress)^3
45+
// 使用 Cubic Ease-Out 函数,这对于“进入”动画感觉更自然
4846
const easedProgress = 1 - Math.pow(1 - progress, 3);
4947

50-
// Set the sprite's alpha to the eased value
51-
sprite.alpha = easedProgress;
48+
// 修正:使用线性插值公式 (lerp)
49+
// 公式:最终值 = 初始值 + (目标值 - 初始值) * 进度
50+
// 在这里,目标值是 1,所以公式为:
51+
// alpha = startAlpha + (1 - startAlpha) * easedProgress
52+
sprite.alpha = startAlpha + (1 - startAlpha) * easedProgress;
5253
}
5354
}
5455

packages/webgal/src/Core/controller/stage/pixi/animations/universalSoftOff.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ export function generateUniversalSoftOffAnimationObj(targetKey: string, duration
44
const target = WebGAL.gameplay.pixiStage!.getStageObjByKey(targetKey);
55
let elapsedTime = 0;
66

7-
// 先设置一个通用的初态
7+
// 新增变量,用于存储动画开始时的初始透明度
8+
let startAlpha = 1;
89

910
/**
1011
* 在此书写为动画设置初态的操作
1112
*/
1213
function setStartState() {
13-
elapsedTime = 0; // Reset timer when animation starts
14+
elapsedTime = 0; // 重置计时器
1415
if (target) {
15-
// It's good practice to ensure the starting alpha is 1
16-
// in case the animation is chained or re-run.
17-
target.pixiContainer.alpha = 1;
16+
// 修正:不再强制设为1,而是记录当前的透明度
17+
startAlpha = target.pixiContainer.alpha;
1818
}
1919
}
2020

@@ -23,6 +23,7 @@ export function generateUniversalSoftOffAnimationObj(targetKey: string, duration
2323
*/
2424
function setEndState() {
2525
if (target) {
26+
// 终态是完全透明,这保持不变
2627
target.pixiContainer.alpha = 0;
2728
}
2829
}
@@ -36,23 +37,20 @@ export function generateUniversalSoftOffAnimationObj(targetKey: string, duration
3637
const targetContainer = target.pixiContainer;
3738
const baseDuration = WebGAL.gameplay.pixiStage!.frameDuration;
3839

39-
// Increment the elapsed time by the duration of the last frame
4040
elapsedTime += baseDuration;
4141

42-
// Ensure elapsedTime does not exceed the total duration
4342
const realElapsedTime = Math.min(elapsedTime, duration);
44-
45-
// Calculate the progress of the animation as a value from 0 to 1
4643
const progress = realElapsedTime / duration;
4744

48-
// Apply the Cubic Ease-In function
49-
// The formula is: progress^3
45+
// 使用 Cubic Ease-In 函数
5046
const easedProgress = Math.pow(progress, 3);
5147

52-
// To fade out, we subtract the eased progress from 1
53-
// As easedProgress goes from 0 to 1 (slowly then quickly),
54-
// alpha will go from 1 to 0 (slowly then quickly).
55-
targetContainer.alpha = 1 - easedProgress;
48+
// 修正:基于初始透明度 startAlpha 进行计算
49+
// 公式:最终值 = 初始值 + (目标值 - 初始值) * 进度
50+
// 在这里,目标值是 0,所以公式简化为:
51+
// alpha = startAlpha + (0 - startAlpha) * easedProgress
52+
// alpha = startAlpha * (1 - easedProgress)
53+
targetContainer.alpha = startAlpha * (1 - easedProgress);
5654
}
5755
}
5856

0 commit comments

Comments
 (0)