-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No longer use animation state to implement clip motion
- Loading branch information
1 parent
0943cec
commit 20755f9
Showing
4 changed files
with
203 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { WrapMode, WrapModeMask, WrappedInfo } from './types'; | ||
|
||
export function wrap ( | ||
elapsedTime: number, | ||
duration: number, | ||
wrapMode: WrapMode, | ||
repeatCount: number, | ||
negativeSpeed: boolean, | ||
info: WrappedInfo, | ||
): WrappedInfo { | ||
let stopped = false; | ||
|
||
let currentIterations = elapsedTime > 0 ? (elapsedTime / duration) : -(elapsedTime / duration); | ||
if (currentIterations >= repeatCount) { | ||
currentIterations = repeatCount; | ||
|
||
stopped = true; | ||
let tempRatio = repeatCount - (repeatCount | 0); | ||
if (tempRatio === 0) { | ||
tempRatio = 1; // 如果播放过,动画不复位 | ||
} | ||
elapsedTime = tempRatio * duration * (elapsedTime > 0 ? 1 : -1); | ||
} | ||
|
||
if (elapsedTime > duration) { | ||
const tempTime = elapsedTime % duration; | ||
elapsedTime = tempTime === 0 ? duration : tempTime; | ||
} else if (elapsedTime < 0) { | ||
elapsedTime %= duration; | ||
if (elapsedTime !== 0) { elapsedTime += duration; } | ||
} | ||
|
||
let needReverse = false; | ||
const shouldWrap = wrapMode & WrapModeMask.ShouldWrap; | ||
if (shouldWrap) { | ||
needReverse = isReverseIteration(wrapMode, currentIterations); | ||
} | ||
|
||
let direction = needReverse ? -1 : 1; | ||
if (negativeSpeed) { | ||
direction *= -1; | ||
} | ||
|
||
// calculate wrapped time | ||
if (shouldWrap && needReverse) { | ||
elapsedTime = duration - elapsedTime; | ||
} | ||
|
||
info.time = elapsedTime; | ||
info.ratio = info.time / duration; | ||
info.direction = direction; | ||
info.stopped = stopped; | ||
info.iterations = currentIterations; | ||
|
||
return info; | ||
} | ||
|
||
function isReverseIteration (wrapMode: WrapMode, currentIterations: number) { | ||
let needReverse = false; | ||
if ((wrapMode & WrapModeMask.PingPong) === WrapModeMask.PingPong) { | ||
const isEnd = currentIterations - (currentIterations | 0) === 0; | ||
if (isEnd && (currentIterations > 0)) { | ||
currentIterations -= 1; | ||
} | ||
const isOddIteration = currentIterations & 1; | ||
if (isOddIteration) { | ||
needReverse = !needReverse; | ||
} | ||
} | ||
if ((wrapMode & WrapModeMask.Reverse) === WrapModeMask.Reverse) { | ||
needReverse = !needReverse; | ||
} | ||
return needReverse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters