mirror of
https://github.com/GuerillaStudio/souvenir.git
synced 2025-02-01 14:49:40 +00:00
refactor(preview): use iterator for image loop
This commit is contained in:
parent
318c4ef7e1
commit
74f0344c6a
3 changed files with 23 additions and 24 deletions
|
@ -1,11 +1,3 @@
|
||||||
export function delay(duration) {
|
|
||||||
return new Promise((resolve, _) => {
|
|
||||||
setTimeout(() => {
|
|
||||||
resolve()
|
|
||||||
}, duration)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function * cycle (xs) {
|
export function * cycle (xs) {
|
||||||
while (true) {
|
while (true) {
|
||||||
yield * xs
|
yield * xs
|
||||||
|
|
|
@ -76,7 +76,6 @@ export default {
|
||||||
this.capturingProgress = 0
|
this.capturingProgress = 0
|
||||||
this.$store.commit('updateCapture', captureData)
|
this.$store.commit('updateCapture', captureData)
|
||||||
this.$router.push({ name: 'preview' })
|
this.$router.push({ name: 'preview' })
|
||||||
// this.startEncoding()
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
async ensureCamera () {
|
async ensureCamera () {
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
<script>
|
<script>
|
||||||
import { encode } from '/services/encode.js'
|
import { encode } from '/services/encode.js'
|
||||||
import encodingOverlay from '/views/components/encoding'
|
import encodingOverlay from '/views/components/encoding'
|
||||||
|
import { cycle } from '/services/util.js'
|
||||||
|
|
||||||
import { mapState } from 'vuex'
|
import { mapState } from 'vuex'
|
||||||
|
|
||||||
|
@ -29,7 +30,7 @@ export default {
|
||||||
data: () => ({
|
data: () => ({
|
||||||
encoding: false,
|
encoding: false,
|
||||||
encodingProgress: 0,
|
encodingProgress: 0,
|
||||||
previewTimeout: null
|
previewInterval: null
|
||||||
}),
|
}),
|
||||||
computed: {
|
computed: {
|
||||||
...mapState([
|
...mapState([
|
||||||
|
@ -44,16 +45,6 @@ export default {
|
||||||
backHome () {
|
backHome () {
|
||||||
this.$router.push({ name: 'home' })
|
this.$router.push({ name: 'home' })
|
||||||
},
|
},
|
||||||
printPreview (context, frameNumber) {
|
|
||||||
this.previewTimeout = setTimeout(() => {
|
|
||||||
// Looper
|
|
||||||
frameNumber < (this.capture.imageDataList.length - 1) ? frameNumber++ : frameNumber = 0
|
|
||||||
// Printer
|
|
||||||
const image = this.capture.imageDataList[frameNumber]
|
|
||||||
context.putImageData(image, 0, 0)
|
|
||||||
this.printPreview(context, frameNumber)
|
|
||||||
}, this.capture.delayTime)
|
|
||||||
},
|
|
||||||
startPreview () {
|
startPreview () {
|
||||||
if (!this.capture) {
|
if (!this.capture) {
|
||||||
this.backHome()
|
this.backHome()
|
||||||
|
@ -63,7 +54,24 @@ export default {
|
||||||
canvas.height = this.capture.imageHeight
|
canvas.height = this.capture.imageHeight
|
||||||
const canvasContext = canvas.getContext('2d')
|
const canvasContext = canvas.getContext('2d')
|
||||||
|
|
||||||
this.printPreview(canvasContext, 0)
|
const imagesIterator = cycle(this.capture.imageDataList)
|
||||||
|
const delay = this.capture.delayTime
|
||||||
|
|
||||||
|
this.previewInterval = setInterval(() => {
|
||||||
|
const {
|
||||||
|
value: image,
|
||||||
|
done
|
||||||
|
} = imagesIterator.next()
|
||||||
|
|
||||||
|
if (image) {
|
||||||
|
canvasContext.putImageData(image, 0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
clearInterval(this.previewInterval)
|
||||||
|
this.previewInterval = null
|
||||||
|
}
|
||||||
|
}, delay)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
startEncoding () {
|
startEncoding () {
|
||||||
|
@ -97,7 +105,7 @@ export default {
|
||||||
this.startPreview()
|
this.startPreview()
|
||||||
},
|
},
|
||||||
destroyed () {
|
destroyed () {
|
||||||
window.clearTimeout(this.previewTimeout)
|
window.clearInterval(this.previewInterval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Add table
Reference in a new issue