-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLottieLooper.vue
More file actions
170 lines (130 loc) · 4.54 KB
/
Copy pathLottieLooper.vue
File metadata and controls
170 lines (130 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<script setup lang="ts">
import { ref, computed, onMounted, onBeforeUnmount, watch, nextTick } from 'vue';
import { useTool } from '@/tools/use-tool';
import lottie, { type AnimationItem } from 'lottie-web';
import ToolWindow from '@/components/ToolWindow.vue';
import DropZone from '@/components/DropZone.vue';
const { title, about } = useTool();
const currentLottie = ref(false);
const animation = ref<AnimationItem>();
const isPaused = ref(false);
const totalFrames = ref(0);
const currentFrame = ref(0);
const loopStart = ref(0);
const loopEnd = ref(0);
const animationFrame = ref(0);
const lottieContainer = ref<HTMLDivElement>();
onMounted(() => tick());
onBeforeUnmount(() => cancelAnimationFrame(animationFrame.value));
watch(() => loopStart.value, () => {
if (!animation.value) return;
animation.value.pause();
isPaused.value = true;
animation.value.goToAndStop(parseInt(loopStart.value as any), true);
});
watch(() => loopEnd.value, () => {
if (!animation.value) return;
animation.value.pause();
isPaused.value = true;
animation.value.goToAndStop(parseInt(loopEnd.value as any), true);
});
function tick() {
if (animation.value) {
if (animation.value.currentFrame >= loopEnd.value) {
playFromLoopPoint();
}
currentFrame.value = animation.value.currentFrame;
}
animationFrame.value = requestAnimationFrame(tick);
}
function playFromLoopPoint() {
if (!animation.value) return;
animation.value.goToAndPlay(loopStart.value, true);
isPaused.value = false;
}
async function handleDrop(item: DataTransferItem) {
if (item.kind === 'file' && item.type === 'application/json') {
const file = item.getAsFile();
if (file) {
const text = await file.text();
try {
currentLottie.value = JSON.parse(text);
await nextTick() // Wait for DOM to insert lottie container
loadLottie();
} catch (e) {}
}
}
}
function reset() {
animation.value = undefined;
currentLottie.value = false;
isPaused.value = false;
totalFrames.value = 0;
currentFrame.value = 0;
loopStart.value = 0;
loopEnd.value = 0;
}
function togglePlayback() {
if (!animation.value) return;
if (animation.value.isPaused) {
animation.value.play();
isPaused.value = false;
} else {
animation.value.pause();
isPaused.value = true;
}
}
async function loadLottie() {
if (!lottieContainer.value) return;
animation.value = lottie.loadAnimation({
container: lottieContainer.value,
loop: false,
autoplay: false,
animationData: currentLottie.value
});
totalFrames.value = animation.value.totalFrames - 1;
loopStart.value = 0;
loopEnd.value = totalFrames.value;
isPaused.value = false;
await nextTick();
animation.value.goToAndPlay(0);
animation.value.addEventListener('complete', () => {
if (animation.value) animation.value.goToAndPlay(loopStart.value, true);
});
}
const currentFrameRounded = computed(() => Math.round(currentFrame.value));
</script>
<template>
<ToolWindow :title="title" :aboutLink="about" backRoute="/">
<div v-if="!currentLottie" class="p-4">
<DropZone @drop="handleDrop" text="Drop a lottie .json file here!" />
</div>
<div v-if="currentLottie" class="p-4">
<div ref="lottieContainer" class="lottie-container"></div>
<input type="range" @pointerup="playFromLoopPoint" class="range-start form-range" min="0" :max="totalFrames" step="1" v-model="loopStart">
<input type="range" class="form-range pe-none" min="0" :max="totalFrames" step="1" :value="currentFrame">
<input type="range" @pointerup="playFromLoopPoint" class="range-end form-range" min="0" :max="totalFrames" step="1" v-model="loopEnd">
<p class="text-center text-light"><span class="text-white-50 user-select-none">Loop start frame:</span> {{loopStart}}</p>
<p class="text-center text-light"><span class="text-white-50 user-select-none">Current frame:</span> {{currentFrameRounded}}</p>
<p class="text-center text-light"><span class="text-white-50 user-select-none">Loop end frame:</span> {{loopEnd}}</p>
<div class="d-flex justify-content-center">
<button @click="togglePlayback" class="btn btn-primary mx-3">{{isPaused ? 'Play' : 'Pause'}}</button>
<button @click="reset" class="btn btn-primary mx-3">Reset</button>
</div>
</div>
</ToolWindow>
</template>
<style scoped>
.lottie-container {
height: 300px;
}
.form-range.pe-none {
filter: invert(1) hue-rotate(180deg);
}
.range-start {
filter: hue-rotate(280deg);
}
.range-end {
filter: hue-rotate(150deg);
}
</style>