feat(encoding): add boomerang effect to encoder

This commit is contained in:
wryk 2019-03-29 17:17:42 +01:00 committed by Tixie
parent 69ccbc780d
commit 0ab14263ac
4 changed files with 12 additions and 7 deletions

View file

@ -22,7 +22,7 @@ class OutputStream {
}
}
export function write (imageWidth, imageHeight, delay, indexedColorImages) {
export function write (imageWidth, imageHeight, delay, indexedColorImages, boomerangEffect = false) {
const emitter = new EventEmitter()
// yup, this is the browser nextTick implementation we are waiting for :facepalm:
@ -41,7 +41,11 @@ export function write (imageWidth, imageHeight, delay, indexedColorImages) {
writer.writeLoopControlInfo(0)
indexedColorImages.forEach((indexedColorImage, index, { length }) => {
const frames = boomerangEffect
? [...indexedColorImages, ...indexedColorImages.slice(1, indexedColorImages.length - 1).reverse()]
: indexedColorImages
frames.forEach((indexedColorImage, index, { length }) => {
writer.writeTableBasedImageWithGraphicControl(indexedColorImage, { delayTimeInMS: delay })
emitter.emit('progress', calcProgress(0, 0.99, (index + 1) / length))
})

View file

@ -4,7 +4,7 @@ import { promisesProgress, calcProgress } from '/services/util.js'
import { GIF_PALETTE_SIZE } from '/constants.js'
export function encode ({ imageDataList, imageWidth, imageHeight, delayTime }) {
export function encode ({ imageDataList, imageWidth, imageHeight, delayTime }, { boomerangEffect }) {
const emitter = new EventEmitter()
const quantizeColorWorkerPool = genericPool.createPool({
@ -74,7 +74,8 @@ export function encode ({ imageDataList, imageWidth, imageHeight, delayTime }) {
imageWidth,
imageHeight,
indexedColorImages,
delayTime
delayTime,
boomerangEffect
})
})
.catch(error => emitter.emit('error', error))

View file

@ -3,9 +3,9 @@ import { write } from '/services/encode-core.js'
onmessage = handleMessage
async function handleMessage (event) {
const { imageWidth, imageHeight, delayTime, indexedColorImages } = event.data
const { imageWidth, imageHeight, delayTime, indexedColorImages, boomerangEffect } = event.data
const writing = write(imageWidth, imageHeight, delayTime, indexedColorImages)
const writing = write(imageWidth, imageHeight, delayTime, indexedColorImages, boomerangEffect)
writing.on('progress', value => postMessage({
type: 'progress',

View file

@ -76,7 +76,7 @@ export default {
},
startEncoding () {
this.encoding = true
const encoding = encode(this.capture)
const encoding = encode(this.capture, { boomerangEffect: false })
encoding.once('error', error => {
console.error(error)