feat(capture option): Switch camera

This commit is contained in:
wryk 2019-03-12 17:41:30 +01:00
parent e278ea149e
commit 84e58f0e8f
5 changed files with 47 additions and 37 deletions

27
package-lock.json generated
View file

@ -3495,8 +3495,7 @@
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"aproba": { "aproba": {
"version": "1.2.0", "version": "1.2.0",
@ -3539,8 +3538,7 @@
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
@ -3551,8 +3549,7 @@
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@ -3669,8 +3666,7 @@
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@ -3682,7 +3678,6 @@
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@ -3712,7 +3707,6 @@
"version": "2.3.5", "version": "2.3.5",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@ -3731,7 +3725,6 @@
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@ -3825,7 +3818,6 @@
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -3911,8 +3903,7 @@
"safe-buffer": { "safe-buffer": {
"version": "5.1.2", "version": "5.1.2",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"safer-buffer": { "safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
@ -3948,7 +3939,6 @@
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",
@ -3968,7 +3958,6 @@
"version": "3.0.1", "version": "3.0.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
@ -4012,14 +4001,12 @@
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
}, },
"yallist": { "yallist": {
"version": "3.0.3", "version": "3.0.3",
"bundled": true, "bundled": true,
"dev": true, "dev": true
"optional": true
} }
} }
}, },

View file

@ -1,7 +1,7 @@
<template lang="html"> <template lang="html">
<div class="layout"> <div class="layout">
<welcome-screen v-if="!mediaStream && !downloading.status"></welcome-screen> <welcome-screen v-if="!welcomed && !downloading.status"></welcome-screen>
<capture-screen v-if="mediaStream && !downloading.status"></capture-screen> <capture-screen v-if="welcomed && !downloading.status"></capture-screen>
<download-screen v-if="downloading.status"></download-screen> <download-screen v-if="downloading.status"></download-screen>
</div> </div>
</template> </template>
@ -23,7 +23,7 @@ export default {
}, },
computed: { computed: {
...mapState([ ...mapState([
'mediaStream', 'welcomed',
'downloading' 'downloading'
]) ])
} }

View file

@ -9,6 +9,7 @@ Vue.use(Vuex)
export default new Vuex.Store({ export default new Vuex.Store({
strict: process.env.NODE_ENV !== 'production', strict: process.env.NODE_ENV !== 'production',
state: { state: {
welcomed: false,
mediaStream: null, mediaStream: null,
timer: { timer: {
selected: 2, selected: 2,
@ -16,6 +17,7 @@ export default new Vuex.Store({
}, },
capturing: { capturing: {
status: false, status: false,
shouldFaceUser: true,
state: 0 state: 0
}, },
encoding: { encoding: {
@ -28,12 +30,19 @@ export default new Vuex.Store({
} }
}, },
mutations: { mutations: {
updateMediaStream (store, mediaStream) { welcome (state) {
if (store.mediaStream) { state.welcomed = true;
store.mediaStream.getTracks().forEach(track => track.stop()) },
startCamera (state, mediaStream) {
state.mediaStream = mediaStream
},
stopCamera (state) {
if (state.mediaStream) {
state.mediaStream.getTracks().forEach(track => track.stop())
} }
},
store.mediaStream = mediaStream inverseFacingMode (store) {
store.capturing.shouldFaceUser = !store.capturing.shouldFaceUser
}, },
updateTimer (store, time) { updateTimer (store, time) {
store.timer.selected = time store.timer.selected = time
@ -69,17 +78,31 @@ export default new Vuex.Store({
} }
}, },
actions: { actions: {
requestCamera ({ commit }) { welcome ({ commit, dispatch }) {
const constaints = { commit('welcome')
dispatch('requestCamera', false)
},
requestCamera ({ state, commit }, inverseFacingMode) {
const shouldFaceUser = inverseFacingMode
? !state.capturing.shouldFaceUser
: state.capturing.shouldFaceUser
const constraints = {
video: { video: {
facingMode: 'user' facingMode: shouldFaceUser ? 'user' : 'environment'
}, },
audio: false audio: false
} }
navigator.mediaDevices.getUserMedia(constaints) commit('stopCamera')
navigator.mediaDevices.getUserMedia(constraints)
.then(mediaStream => { .then(mediaStream => {
commit('updateMediaStream', mediaStream) commit('startCamera', mediaStream)
if (inverseFacingMode) {
commit('inverseFacingMode')
}
}) })
.catch(error => console.error(error)) .catch(error => console.error(error))
}, },

View file

@ -5,7 +5,7 @@
{{ timeLabel(time) }} {{ timeLabel(time) }}
</option> </option>
</select> </select>
<button class="options__btn" :disabled="encoding.status" @click="back"><icon-switch/>switch</button> <button class="options__btn" :disabled="encoding.status" @click="switchCamera"><icon-switch/>switch</button>
</div> </div>
</template> </template>
@ -26,8 +26,8 @@ export default {
]) ])
}, },
methods: { methods: {
back () { switchCamera () {
this.$store.commit('updateMediaStream', null) this.$store.dispatch('requestCamera', true)
}, },
timeLabel (time) { timeLabel (time) {
return time + 's' return time + 's'

View file

@ -17,7 +17,7 @@ export default {
name: 'welcome', name: 'welcome',
methods: { methods: {
welcome () { welcome () {
this.$store.dispatch('requestCamera') this.$store.dispatch('welcome')
} }
} }
} }