Skip to content

Commit e1d6ca9

Browse files
Improve animation easing and duration handling
Updated universalSoftIn and universalSoftOff animations to use cubic easing for smoother transitions and track elapsed time for accurate progress. Increased default animation duration in getEnterExitAnimation for better visual effect.
1 parent 070e662 commit e1d6ca9

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

packages/webgal/src/Core/Modules/animationFunctions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function getEnterExitAnimation(
7676
}
7777
return { duration, animation };
7878
} else {
79-
let duration = 750;
79+
let duration = 1000;
8080
if (isBg) {
8181
duration = 1500;
8282
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { WebGAL } from '@/Core/WebGAL';
22

33
export function generateUniversalSoftInAnimationObj(targetKey: string, duration: number) {
44
const target = WebGAL.gameplay.pixiStage!.getStageObjByKey(targetKey);
5+
let elapsedTime = 0;
56

67
// 先设置一个通用的初态
7-
8-
// TODO:通用初态设置
98
/**
109
* 在此书写为动画设置初态的操作
1110
*/
1211
function setStartState() {
12+
elapsedTime = 0; // Reset timer when animation starts
1313
if (target) {
1414
target.pixiContainer.alpha = 0;
1515
}
@@ -33,12 +33,22 @@ export function generateUniversalSoftInAnimationObj(targetKey: string, duration:
3333
if (target) {
3434
const sprite = target.pixiContainer;
3535
const baseDuration = WebGAL.gameplay.pixiStage!.frameDuration;
36-
const currentAddOplityDelta = (duration / baseDuration) * delta;
37-
const increasement = 1 / currentAddOplityDelta;
38-
// const decreasement = 5 / currentAddOplityDelta;
39-
if (sprite.alpha < 1) {
40-
sprite.alpha += increasement;
41-
}
36+
37+
// Increment the elapsed time by the duration of the last frame
38+
elapsedTime += baseDuration;
39+
40+
// Ensure elapsedTime does not exceed the total duration
41+
const realElapsedTime = Math.min(elapsedTime, duration);
42+
43+
// Calculate the progress of the animation as a value from 0 to 1
44+
const progress = realElapsedTime / duration;
45+
46+
// Apply the Cubic Ease-Out function
47+
// The formula is: 1 - (1 - progress)^3
48+
const easedProgress = 1 - Math.pow(1 - progress, 3);
49+
50+
// Set the sprite's alpha to the eased value
51+
sprite.alpha = easedProgress;
4252
}
4353
}
4454

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@ import { WebGAL } from '@/Core/WebGAL';
22

33
export function generateUniversalSoftOffAnimationObj(targetKey: string, duration: number) {
44
const target = WebGAL.gameplay.pixiStage!.getStageObjByKey(targetKey);
5+
let elapsedTime = 0;
56

67
// 先设置一个通用的初态
78

89
/**
910
* 在此书写为动画设置初态的操作
1011
*/
11-
function setStartState() {}
12+
function setStartState() {
13+
elapsedTime = 0; // Reset timer when animation starts
14+
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;
18+
}
19+
}
1220

1321
/**
1422
* 在此书写为动画设置终态的操作
1523
*/
1624
function setEndState() {
17-
if (target) target.pixiContainer.alpha = 0;
25+
if (target) {
26+
target.pixiContainer.alpha = 0;
27+
}
1828
}
1929

2030
/**
@@ -25,11 +35,24 @@ export function generateUniversalSoftOffAnimationObj(targetKey: string, duration
2535
if (target) {
2636
const targetContainer = target.pixiContainer;
2737
const baseDuration = WebGAL.gameplay.pixiStage!.frameDuration;
28-
const currentAddOplityDelta = (duration / baseDuration) * delta;
29-
const decreasement = 1 / currentAddOplityDelta;
30-
if (targetContainer.alpha > 0) {
31-
targetContainer.alpha -= decreasement;
32-
}
38+
39+
// Increment the elapsed time by the duration of the last frame
40+
elapsedTime += baseDuration;
41+
42+
// Ensure elapsedTime does not exceed the total duration
43+
const realElapsedTime = Math.min(elapsedTime, duration);
44+
45+
// Calculate the progress of the animation as a value from 0 to 1
46+
const progress = realElapsedTime / duration;
47+
48+
// Apply the Cubic Ease-In function
49+
// The formula is: progress^3
50+
const easedProgress = Math.pow(progress, 3);
51+
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;
3356
}
3457
}
3558

0 commit comments

Comments
 (0)