Skip to content

Commit 8c775d8

Browse files
committed
fix(tracking): paint persists on canvas
In some cases the tracking canvas is not cleared and the paint is still visible although no QR code is in sight anymore. Now fixed.
1 parent 59db045 commit 8c775d8

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

src/components/QrcodeReader.vue

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,24 @@ export default {
120120
trackRepaintFunction () {
121121
if (this.track === true) {
122122
return function (location, ctx) {
123-
if (location !== null) {
124-
const {
125-
topLeftCorner,
126-
topRightCorner,
127-
bottomLeftCorner,
128-
bottomRightCorner,
129-
} = location
130-
131-
ctx.strokeStyle = 'red'
132-
133-
ctx.beginPath()
134-
ctx.moveTo(topLeftCorner.x, topLeftCorner.y)
135-
ctx.lineTo(bottomLeftCorner.x, bottomLeftCorner.y)
136-
ctx.lineTo(bottomRightCorner.x, bottomRightCorner.y)
137-
ctx.lineTo(topRightCorner.x, topRightCorner.y)
138-
ctx.lineTo(topLeftCorner.x, topLeftCorner.y)
139-
ctx.closePath()
140-
141-
ctx.stroke()
142-
}
123+
const {
124+
topLeftCorner,
125+
topRightCorner,
126+
bottomLeftCorner,
127+
bottomRightCorner,
128+
} = location
129+
130+
ctx.strokeStyle = 'red'
131+
132+
ctx.beginPath()
133+
ctx.moveTo(topLeftCorner.x, topLeftCorner.y)
134+
ctx.lineTo(bottomLeftCorner.x, bottomLeftCorner.y)
135+
ctx.lineTo(bottomRightCorner.x, bottomRightCorner.y)
136+
ctx.lineTo(topRightCorner.x, topRightCorner.y)
137+
ctx.lineTo(topLeftCorner.x, topLeftCorner.y)
138+
ctx.closePath()
139+
140+
ctx.stroke()
143141
}
144142
} else if (this.track === false) {
145143
return null
@@ -170,6 +168,8 @@ export default {
170168
171169
this.readyAfterPause = false
172170
} else {
171+
this.repaintTrack(null)
172+
173173
video.play()
174174
175175
video.addEventListener(
@@ -283,38 +283,42 @@ export default {
283283
* video element is responsive and scales with space available. Therefore
284284
* the coordinates are re-calculated to be relative to the video element.
285285
*/
286-
normalizeLocation (location) {
287-
if (location === null) {
288-
return null
289-
} else {
290-
const widthRatio = this.cameraInstance.displayWidth / this.cameraInstance.resolutionWidth
291-
const heightRatio = this.cameraInstance.displayHeight / this.cameraInstance.resolutionHeight
292-
293-
const normalizeEntry = ({ x, y }) => ({
294-
x: Math.floor(x * widthRatio),
295-
y: Math.floor(y * heightRatio),
296-
})
286+
normalizeLocation (widthRatio, heightRatio, location) {
287+
const normalized = {}
297288
298-
const joinObjects = (objA, objB) => ({ ...objA, ...objB })
299-
300-
return Object.entries(location)
301-
.map(([ key, val ]) => ({ [key]: normalizeEntry(val) }))
302-
.reduce(joinObjects, {})
289+
for (const key in location) {
290+
normalized[key] = {
291+
x: Math.floor(location[key].x * widthRatio),
292+
y: Math.floor(location[key].y * heightRatio),
293+
}
303294
}
295+
296+
return normalized
304297
},
305298
306299
repaintTrack (location) {
307-
location = this.normalizeLocation(location)
308-
309300
const canvas = this.$refs.trackingLayer
310301
const ctx = canvas.getContext('2d')
302+
const cameraInstance = this.cameraInstance
303+
304+
window.requestAnimationFrame(() => {
305+
if (location === null) {
306+
ctx.clearRect(0, 0, canvas.width, canvas.height)
307+
} else {
308+
const displayWidth = cameraInstance.displayWidth
309+
const displayHeight = cameraInstance.displayHeight
310+
311+
canvas.width = displayWidth
312+
canvas.height = displayHeight
311313
312-
canvas.width = this.cameraInstance.displayWidth
313-
canvas.height = this.cameraInstance.displayHeight
314+
const widthRatio = displayWidth / cameraInstance.resolutionWidth
315+
const heightRatio = displayHeight / cameraInstance.resolutionHeight
314316
315-
window.requestAnimationFrame(
316-
() => this.trackRepaintFunction(location, ctx)
317-
)
317+
location = this.normalizeLocation(widthRatio, heightRatio, location)
318+
319+
this.trackRepaintFunction(location, ctx)
320+
}
321+
})
318322
},
319323
320324
},

0 commit comments

Comments
 (0)