2019-03-15 18:25:36 +00:00
|
|
|
import EventEmitter from 'eventemitter3'
|
|
|
|
|
|
|
|
import {
|
|
|
|
makeRectangle,
|
|
|
|
crop
|
|
|
|
} from '/services/rectangle.js'
|
|
|
|
|
2019-03-15 18:07:26 +00:00
|
|
|
import {
|
|
|
|
GIF_WIDTH,
|
|
|
|
GIF_HEIGHT,
|
|
|
|
GIF_FRAME_RATE
|
|
|
|
} from '/constants.js'
|
|
|
|
|
2019-03-10 03:13:25 +00:00
|
|
|
|
2019-03-15 18:25:36 +00:00
|
|
|
|
|
|
|
export function capture (mediaStream, duration) {
|
|
|
|
const emitter = new EventEmitter()
|
|
|
|
|
|
|
|
Promise.resolve().then(() => {
|
2019-03-10 04:37:22 +00:00
|
|
|
const video = document.createElement('video')
|
|
|
|
video.autoplay = true
|
2019-03-12 20:54:17 +00:00
|
|
|
video.setAttribute('playsinline', '')
|
|
|
|
video.setAttribute('webkit-playsinline', '')
|
2019-03-10 04:37:22 +00:00
|
|
|
|
|
|
|
const canvas = document.createElement('canvas')
|
2019-03-15 18:07:26 +00:00
|
|
|
canvas.width = GIF_WIDTH
|
|
|
|
canvas.height = GIF_HEIGHT
|
2019-03-10 04:37:22 +00:00
|
|
|
|
|
|
|
const canvasContext = canvas.getContext('2d')
|
|
|
|
|
2019-03-15 18:07:26 +00:00
|
|
|
const totalFrames = duration / 1000 * GIF_FRAME_RATE
|
2019-03-10 03:13:25 +00:00
|
|
|
|
|
|
|
if (totalFrames < 1) {
|
|
|
|
resolve([])
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:07:26 +00:00
|
|
|
const delayTime = 1000 / GIF_FRAME_RATE
|
2019-03-10 03:13:25 +00:00
|
|
|
|
|
|
|
video.srcObject = mediaStream
|
|
|
|
|
2019-03-10 04:37:22 +00:00
|
|
|
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 intervalId = setInterval(() => {
|
|
|
|
if (imageDataList.length < totalFrames) {
|
|
|
|
canvasContext.drawImage(
|
|
|
|
video,
|
|
|
|
soureRectangle.x,
|
|
|
|
soureRectangle.y,
|
|
|
|
soureRectangle.width,
|
|
|
|
soureRectangle.height,
|
|
|
|
destinationRectangle.x,
|
|
|
|
destinationRectangle.y,
|
|
|
|
destinationRectangle.width,
|
|
|
|
destinationRectangle.height
|
|
|
|
)
|
|
|
|
|
|
|
|
const imageData = canvasContext.getImageData(
|
|
|
|
destinationRectangle.x,
|
|
|
|
destinationRectangle.y,
|
|
|
|
destinationRectangle.width,
|
|
|
|
destinationRectangle.height
|
|
|
|
)
|
|
|
|
|
|
|
|
imageDataList.push(imageData)
|
|
|
|
|
2019-03-15 18:25:36 +00:00
|
|
|
emitter.emit('progress', imageDataList.length / totalFrames)
|
2019-03-10 04:37:22 +00:00
|
|
|
} else {
|
|
|
|
clearInterval(intervalId)
|
|
|
|
|
2019-03-15 18:25:36 +00:00
|
|
|
emitter.emit('done', {
|
2019-03-10 04:37:22 +00:00
|
|
|
imageDataList,
|
2019-03-15 18:07:26 +00:00
|
|
|
imageWidth: GIF_WIDTH,
|
|
|
|
imageHeight: GIF_HEIGHT,
|
2019-03-10 04:37:22 +00:00
|
|
|
delayTime
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, delayTime)
|
|
|
|
})
|
2019-03-10 03:13:25 +00:00
|
|
|
})
|
2019-03-15 18:25:36 +00:00
|
|
|
.catch(error => emitter.emit('error', error))
|
|
|
|
|
|
|
|
return emitter;
|
2019-03-10 03:13:25 +00:00
|
|
|
}
|