impl controller play, pause and speed
This commit is contained in:
@@ -7,7 +7,14 @@
|
|||||||
</div>
|
</div>
|
||||||
<span class="rr-timeline__time">{formatTime(meta.totalTime)}</span>
|
<span class="rr-timeline__time">{formatTime(meta.totalTime)}</span>
|
||||||
</div>
|
</div>
|
||||||
<button>pause</button>
|
<div class="rr-controller__btns">
|
||||||
|
<button on:click="toggle()">
|
||||||
|
{isPlaying ? 'pause' : 'play'}
|
||||||
|
</button>
|
||||||
|
{#each [1, 2, 4, 8] as s}
|
||||||
|
<button class:active="s === speed" on:click="set({ speed: s })">{s}x</button>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
@@ -17,6 +24,8 @@
|
|||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
currentTime: 0,
|
currentTime: 0,
|
||||||
|
isPlaying: false,
|
||||||
|
speed: 1,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
@@ -24,7 +33,8 @@
|
|||||||
return replayer.getMetaData();
|
return replayer.getMetaData();
|
||||||
},
|
},
|
||||||
percentage({ currentTime, meta }) {
|
percentage({ currentTime, meta }) {
|
||||||
return `${100 * currentTime / meta.totalTime}%`;
|
const percent = Math.min(1, currentTime / meta.totalTime);
|
||||||
|
return `${100 * percent}%`;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
helpers: {
|
helpers: {
|
||||||
@@ -33,25 +43,55 @@
|
|||||||
methods: {
|
methods: {
|
||||||
loopTimer() {
|
loopTimer() {
|
||||||
const now = performance.now();
|
const now = performance.now();
|
||||||
|
let lastStep = now;
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
function update(step) {
|
function update(step) {
|
||||||
let { currentTime, meta } = self.get();
|
let { currentTime, meta, isPlaying, speed } = self.get();
|
||||||
currentTime = Math.floor(step - now);
|
if (!isPlaying) {
|
||||||
self.set({ currentTime });
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const stepDiff = Math.floor(step - lastStep);
|
||||||
|
lastStep = step;
|
||||||
|
currentTime += speed * stepDiff;
|
||||||
|
self.set({
|
||||||
|
currentTime: Math.min(currentTime, meta.totalTime),
|
||||||
|
});
|
||||||
|
|
||||||
if (currentTime < meta.totalTime) {
|
if (currentTime < meta.totalTime) {
|
||||||
requestAnimationFrame(update);
|
requestAnimationFrame(update);
|
||||||
|
} else {
|
||||||
|
self.set({ isPlaying: false, currentTime: 0 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
requestAnimationFrame(update);
|
requestAnimationFrame(update);
|
||||||
},
|
},
|
||||||
},
|
play() {
|
||||||
onupdate({ changed, current }) {
|
const { replayer } = this.get();
|
||||||
if (changed.replayer) {
|
// replayer.play();
|
||||||
this.loopTimer();
|
this.loopTimer();
|
||||||
current.replayer.play();
|
},
|
||||||
|
pause() {
|
||||||
|
const { replayer } = this.get();
|
||||||
|
replayer.pause();
|
||||||
|
},
|
||||||
|
toggle() {
|
||||||
|
const { isPlaying } = this.get();
|
||||||
|
this.set({ isPlaying: !isPlaying });
|
||||||
|
if (isPlaying) {
|
||||||
|
this.pause();
|
||||||
|
} else {
|
||||||
|
this.play();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
onupdate({ changed, current, previous }) {
|
||||||
|
if (current.replayer && !previous) {
|
||||||
|
// auto play
|
||||||
|
this.set({ isPlaying: true });
|
||||||
|
this.play();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -105,4 +145,8 @@
|
|||||||
transform: translate(-50%, -50%);
|
transform: translate(-50%, -50%);
|
||||||
background: orange;
|
background: orange;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.rr-controller__btns button.active {
|
||||||
|
color: orange;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user