[FEAT] Clean code
This commit is contained in:
@@ -9,7 +9,6 @@ export interface AnimationFramesOptions {
|
||||
export function useAnimationFrames(options: AnimationFramesOptions) {
|
||||
const { onDraw } = options;
|
||||
|
||||
// Convert sprites to a computed ref for reactivity
|
||||
const spritesRef = computed(() => {
|
||||
if (typeof options.sprites === 'function') {
|
||||
return options.sprites();
|
||||
@@ -20,20 +19,16 @@ export function useAnimationFrames(options: AnimationFramesOptions) {
|
||||
return options.sprites;
|
||||
});
|
||||
|
||||
// Helper to get sprites array
|
||||
const getSprites = () => spritesRef.value;
|
||||
|
||||
// State
|
||||
const currentFrameIndex = ref(0);
|
||||
const isPlaying = ref(false);
|
||||
const fps = ref(12);
|
||||
const hiddenFrames = ref<number[]>([]);
|
||||
|
||||
// Animation internals
|
||||
const animationFrameId = ref<number | null>(null);
|
||||
const lastFrameTime = ref(0);
|
||||
|
||||
// Computed properties for visible frames
|
||||
const visibleFrames = computed(() => getSprites().filter((_, index) => !hiddenFrames.value.includes(index)));
|
||||
|
||||
const visibleFramesCount = computed(() => visibleFrames.value.length);
|
||||
@@ -46,7 +41,6 @@ export function useAnimationFrames(options: AnimationFramesOptions) {
|
||||
|
||||
const visibleFrameNumber = computed(() => visibleFrameIndex.value + 1);
|
||||
|
||||
// Animation control
|
||||
const animateFrame = () => {
|
||||
const now = performance.now();
|
||||
const elapsed = now - lastFrameTime.value;
|
||||
@@ -109,7 +103,6 @@ export function useAnimationFrames(options: AnimationFramesOptions) {
|
||||
currentFrameIndex.value = sprites.indexOf(visibleFrames.value[index]);
|
||||
};
|
||||
|
||||
// Frame visibility management
|
||||
const toggleHiddenFrame = (index: number) => {
|
||||
const sprites = getSprites();
|
||||
const currentIndex = hiddenFrames.value.indexOf(index);
|
||||
@@ -117,7 +110,6 @@ export function useAnimationFrames(options: AnimationFramesOptions) {
|
||||
if (currentIndex === -1) {
|
||||
hiddenFrames.value.push(index);
|
||||
|
||||
// If hiding current frame, switch to next visible
|
||||
if (index === currentFrameIndex.value) {
|
||||
const nextVisible = sprites.findIndex((_, i) => i !== index && !hiddenFrames.value.includes(i));
|
||||
if (nextVisible !== -1) {
|
||||
@@ -140,32 +132,27 @@ export function useAnimationFrames(options: AnimationFramesOptions) {
|
||||
const sprites = getSprites();
|
||||
hiddenFrames.value = sprites.map((_, index) => index);
|
||||
|
||||
// Keep at least one frame visible
|
||||
if (hiddenFrames.value.length > 0) {
|
||||
hiddenFrames.value.splice(currentFrameIndex.value, 1);
|
||||
}
|
||||
onDraw();
|
||||
};
|
||||
|
||||
// Cleanup on unmount
|
||||
onUnmounted(() => {
|
||||
stopAnimation();
|
||||
});
|
||||
|
||||
return {
|
||||
// State
|
||||
currentFrameIndex,
|
||||
isPlaying,
|
||||
fps,
|
||||
hiddenFrames,
|
||||
|
||||
// Computed
|
||||
visibleFrames,
|
||||
visibleFramesCount,
|
||||
visibleFrameIndex,
|
||||
visibleFrameNumber,
|
||||
|
||||
// Methods
|
||||
togglePlayback,
|
||||
nextFrame,
|
||||
previousFrame,
|
||||
|
||||
Reference in New Issue
Block a user