Skip to content

Commit 094fd2f

Browse files
committed
docs: minimal demo to debug memory leak
Issue: #233
1 parent c9a1da8 commit 094fd2f

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<video id="video" autoplay muted playsinline></video>
10+
</body>
11+
<script type="module">
12+
import BarcodeDetector from "https://cdn.skypack.dev/[email protected]";
13+
14+
const adaptOldFormat = detectedCodes => {
15+
if (detectedCodes.length > 0) {
16+
const [ firstCode ] = detectedCodes;
17+
18+
const [
19+
topLeftCorner,
20+
topRightCorner,
21+
bottomRightCorner,
22+
bottomLeftCorner
23+
] = firstCode.cornerPoints
24+
25+
return {
26+
content: firstCode.rawValue,
27+
location: {
28+
topLeftCorner,
29+
topRightCorner,
30+
bottomRightCorner,
31+
bottomLeftCorner,
32+
33+
// not supported by native API:
34+
topLeftFinderPattern: {},
35+
topRightFinderPattern: {},
36+
bottomLeftFinderPattern: {}
37+
},
38+
imageData: null
39+
}
40+
} else {
41+
return {
42+
content: null,
43+
location: null,
44+
imageData: null
45+
}
46+
}
47+
}
48+
49+
export const keepScanning = (videoElement, options) => {
50+
const barcodeDetector = new BarcodeDetector({ formats: ["qr_code"] });
51+
52+
const { detectHandler, locateHandler, minDelay } = options;
53+
54+
const processFrame = state => async timeNow => {
55+
if (videoElement.readyState > 1) {
56+
console.log("scan")
57+
const { lastScanned, contentBefore, locationBefore } = state
58+
59+
if (timeNow - lastScanned >= minDelay) {
60+
const detectedCodes = await barcodeDetector.detect(videoElement);
61+
const { content, location, imageData } = adaptOldFormat(detectedCodes)
62+
63+
if (content !== null && content !== contentBefore) {
64+
detectHandler({ content, location, imageData });
65+
}
66+
67+
if (location !== null || locationBefore !== null) {
68+
locateHandler(detectedCodes);
69+
}
70+
71+
window.requestAnimationFrame(processFrame({
72+
lastScanned: timeNow,
73+
contentBefore: content ?? contentBefore,
74+
locationBefore: location
75+
}))
76+
} else {
77+
window.requestAnimationFrame(processFrame(state))
78+
}
79+
}
80+
};
81+
82+
processFrame({
83+
contentBefore: null,
84+
locationBefore: null,
85+
lastScanned: performance.now()
86+
})();
87+
};
88+
89+
(async () => {
90+
const videoEl = document.querySelector('#video')
91+
92+
const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false })
93+
94+
videoEl.srcObject = stream
95+
96+
videoEl.addEventListener("loadeddata", () => {
97+
keepScanning(videoEl, { minDelay: 40, detectHandler: console.log, locateHandler: console.log })
98+
})
99+
100+
})()
101+
</script>
102+
</html>

0 commit comments

Comments
 (0)