Skip to content

Commit b5a2249

Browse files
committed
feat: create camera prop as alias
deprecate `video-constraints` in favor of `camera` prop which works exactly the same way. The name `camera` is more concise. It also underlines the purpose of this prop, since one can pass `false` to deactivate the camera which would cause an error if passed as raw video constraint.
1 parent 40b3a5f commit b5a2249

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

src/components/QrcodeReader.vue

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<video
2020
ref="video"
21-
class="qrcode-reader__camera"
21+
class="qrcode-reader__camera-layer"
2222
></video>
2323
</div>
2424
</div>
@@ -37,9 +37,16 @@ export default {
3737
default: false,
3838
},
3939
40+
/** deprecated in favor of `camera` **/
4041
videoConstraints: {
4142
type: [Object, Boolean],
42-
default: () => ({}), // empty object
43+
default: undefined,
44+
},
45+
46+
camera: {
47+
type: [Object, Boolean],
48+
// default: () => ({}) // empty object
49+
default: undefined,
4350
},
4451
4552
track: {
@@ -50,7 +57,7 @@ export default {
5057
5158
data () {
5259
return {
53-
camera: null,
60+
cameraInstance: null,
5461
destroyed: false,
5562
readyAfterPause: true,
5663
}
@@ -60,7 +67,7 @@ export default {
6067
6168
shouldScan () {
6269
return this.paused === false &&
63-
this.camera !== null &&
70+
this.cameraInstance !== null &&
6471
this.destroyed === false &&
6572
this.readyAfterPause
6673
},
@@ -82,23 +89,31 @@ export default {
8289
* camera stream. Properties define if a certain camera is adequate or not.
8390
*/
8491
constraints () {
85-
let withDefaults
86-
87-
if (isBoolean(this.videoConstraints)) {
88-
withDefaults = this.videoConstraints
89-
} else {
90-
withDefaults = {
91-
facingMode: { ideal: 'environment' },
92-
width: { min: 360, ideal: 640, max: 1920 },
93-
height: { min: 240, ideal: 480, max: 1080 },
92+
let videoConstraints = {}
9493
95-
...this.videoConstraints,
96-
}
94+
if (this.camera !== undefined) {
95+
videoConstraints = this.camera
96+
} else if (this.videoConstraints !== undefined) {
97+
console.warn('The `video-constraints` prop is deprecated. Use `camera` instead.')
98+
videoConstraints = this.videoConstraints
9799
}
98100
99-
return {
100-
audio: false,
101-
video: withDefaults,
101+
if (isBoolean(videoConstraints)) {
102+
return {
103+
audio: false,
104+
video: videoConstraints,
105+
}
106+
} else {
107+
return {
108+
audio: false,
109+
video: {
110+
facingMode: { ideal: 'environment' },
111+
width: { min: 360, ideal: 640, max: 1920 },
112+
height: { min: 240, ideal: 480, max: 1080 },
113+
114+
...videoConstraints,
115+
},
116+
}
102117
}
103118
},
104119
@@ -179,8 +194,8 @@ export default {
179194
},
180195
181196
beforeDestroy () {
182-
if (this.camera !== null) {
183-
this.camera.stop()
197+
if (this.cameraInstance !== null) {
198+
this.cameraInstance.stop()
184199
}
185200
186201
this.destroyed = true
@@ -189,19 +204,19 @@ export default {
189204
methods: {
190205
191206
async init () {
192-
if (this.camera !== null) {
193-
this.camera.stop()
207+
if (this.cameraInstance !== null) {
208+
this.cameraInstance.stop()
194209
}
195210
196211
if (this.videoConstraints === false) {
197-
this.camera = null
212+
this.cameraInstance = null
198213
} else {
199-
this.camera = await Camera(this.constraints, this.$refs.video)
214+
this.cameraInstance = await Camera(this.constraints, this.$refs.video)
200215
}
201216
},
202217
203218
startScanning () {
204-
Scanner.keepScanning(this.camera, {
219+
Scanner.keepScanning(this.cameraInstance, {
205220
locateHandler: this.onLocate,
206221
detectHandler: scanResult => this.onDetect('stream', scanResult),
207222
shouldContinue: () => this.shouldScan,
@@ -272,8 +287,8 @@ export default {
272287
if (location === null) {
273288
return null
274289
} else {
275-
const widthRatio = this.camera.displayWidth / this.camera.resolutionWidth
276-
const heightRatio = this.camera.displayHeight / this.camera.resolutionHeight
290+
const widthRatio = this.cameraInstance.displayWidth / this.cameraInstance.resolutionWidth
291+
const heightRatio = this.cameraInstance.displayHeight / this.cameraInstance.resolutionHeight
277292
278293
const normalizeEntry = ({ x, y }) => ({
279294
x: Math.floor(x * widthRatio),
@@ -294,8 +309,8 @@ export default {
294309
const canvas = this.$refs.trackingLayer
295310
const ctx = canvas.getContext('2d')
296311
297-
canvas.width = this.camera.displayWidth
298-
canvas.height = this.camera.displayHeight
312+
canvas.width = this.cameraInstance.displayWidth
313+
canvas.height = this.cameraInstance.displayHeight
299314
300315
window.requestAnimationFrame(
301316
() => this.trackRepaintFunction(location, ctx)
@@ -318,7 +333,7 @@ export default {
318333
position: relative;
319334
}
320335
321-
.qrcode-reader__camera {
336+
.qrcode-reader__camera-layer {
322337
display: block;
323338
object-fit: contain;
324339
max-width: 100%;

0 commit comments

Comments
 (0)