mirror of
https://github.com/GuerillaStudio/souvenir.git
synced 2024-11-09 16:11:52 +00:00
refactor(capture): fix delay before completion
This commit is contained in:
parent
b403a78437
commit
b741779265
1 changed files with 58 additions and 56 deletions
|
@ -1,9 +1,6 @@
|
|||
import EventEmitter from 'eventemitter3'
|
||||
|
||||
import {
|
||||
makeRectangle,
|
||||
crop
|
||||
} from '/services/rectangle.js'
|
||||
import pEvent from 'p-event'
|
||||
import { makeRectangle, crop } from '/services/rectangle.js'
|
||||
|
||||
import {
|
||||
GIF_WIDTH,
|
||||
|
@ -11,41 +8,46 @@ import {
|
|||
GIF_FRAME_RATE
|
||||
} from '/constants.js'
|
||||
|
||||
|
||||
|
||||
export function capture (mediaStream, duration) {
|
||||
const emitter = new EventEmitter()
|
||||
|
||||
Promise.resolve().then(() => {
|
||||
const video = document.createElement('video')
|
||||
video.autoplay = true
|
||||
video.setAttribute('playsinline', '')
|
||||
video.setAttribute('webkit-playsinline', '')
|
||||
Promise.resolve().then(async () => {
|
||||
const delayTime = 1000 / GIF_FRAME_RATE
|
||||
const totalFrames = duration / 1000 * GIF_FRAME_RATE
|
||||
|
||||
// Well, this is a very low frame rate or very short duration clip
|
||||
if (totalFrames < 1) {
|
||||
emitter.emit('done', {
|
||||
imageWidth: GIF_WIDTH,
|
||||
imageHeight: GIF_HEIGHT,
|
||||
imageDataList: [],
|
||||
delayTime
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const imageDataList = []
|
||||
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = GIF_WIDTH
|
||||
canvas.height = GIF_HEIGHT
|
||||
|
||||
const canvasContext = canvas.getContext('2d')
|
||||
|
||||
const totalFrames = duration / 1000 * GIF_FRAME_RATE
|
||||
|
||||
if (totalFrames < 1) {
|
||||
resolve([])
|
||||
}
|
||||
|
||||
const delayTime = 1000 / GIF_FRAME_RATE
|
||||
|
||||
video.srcObject = mediaStream
|
||||
|
||||
video.addEventListener('canplay', () => {
|
||||
const soureRectangle = crop(makeRectangle(0, 0, video.videoWidth, video.videoHeight))
|
||||
const destinationRectangle = makeRectangle(0, 0, canvas.width, canvas.height)
|
||||
|
||||
const imageDataList = []
|
||||
const video = document.createElement('video')
|
||||
video.setAttribute('playsinline', '')
|
||||
video.setAttribute('webkit-playsinline', '')
|
||||
video.srcObject = mediaStream
|
||||
video.play()
|
||||
|
||||
const intervalId = setInterval(() => {
|
||||
if (imageDataList.length < totalFrames) {
|
||||
await pEvent(video, 'canplaythrough')
|
||||
const soureRectangle = crop(makeRectangle(0, 0, video.videoWidth, video.videoHeight))
|
||||
|
||||
step()
|
||||
|
||||
function step() {
|
||||
canvasContext.drawImage(
|
||||
video,
|
||||
soureRectangle.x,
|
||||
|
@ -68,9 +70,10 @@ export function capture (mediaStream, duration) {
|
|||
imageDataList.push(imageData)
|
||||
|
||||
emitter.emit('progress', imageDataList.length / totalFrames)
|
||||
} else {
|
||||
clearInterval(intervalId)
|
||||
|
||||
if (imageDataList.length < totalFrames) {
|
||||
setTimeout(step, delayTime)
|
||||
} else {
|
||||
emitter.emit('done', {
|
||||
imageDataList,
|
||||
imageWidth: GIF_WIDTH,
|
||||
|
@ -78,8 +81,7 @@ export function capture (mediaStream, duration) {
|
|||
delayTime
|
||||
})
|
||||
}
|
||||
}, delayTime)
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch(error => emitter.emit('error', error))
|
||||
|
||||
|
|
Loading…
Reference in a new issue