Skip to content

Commit 1faf6e2

Browse files
author
Cube
authored
Merge pull request #127 from Dynamsoft/_dev
Dev
2 parents 7310a16 + d3cb9a1 commit 1faf6e2

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

4.use-case/3.show-result-texts-on-the-video.html

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,38 @@
1515
<title>
1616
Dynamsoft Barcode Reader Sample - Show result texts on the video
1717
</title>
18+
<style>
19+
.bubble-box{
20+
max-width: 20rem; /* You are likely to change it */
21+
position: absolute;
22+
background: white;
23+
border-radius: 4px;
24+
padding: 4px;
25+
word-break: break-all;
26+
word-wrap: break-word;
27+
box-shadow: 5px 5px 5px 3px rgba(0,0,0,0.2);
28+
transform: translate(-50%, -4px);
29+
}
30+
.bubble-box::after{
31+
content: '';
32+
position: absolute;
33+
bottom: -8px;
34+
left: calc(50% - 8px);
35+
border-left: 8px solid transparent;
36+
border-top: 8px solid white;
37+
border-right: 8px solid transparent;
38+
}
39+
</style>
40+
</head>
41+
<body>
42+
<div id="div-ui-container" style="width: 100%; height: 90vh"></div>
43+
<div id="div-information-containers"></div>
1844
<!--
1945
This sample makes use of the library hosted by the CDN jsDelivr. If you would rather use the
2046
library offline. Please see the guide on how to host the library:
2147
https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=latest#host-the-library-yourself-recommended
2248
-->
2349
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.20/dist/dbr.js"></script>
24-
</head>
25-
<body>
26-
<div id="div-ui-container" style="width: 100%; height: 90vh"></div>
27-
<div id="div-text-containers"></div>
2850
<script>
2951
if (location.protocol === "file:") {
3052
const message = `The page is opened via file:// and "BarcodeScanner" may not work properly. Please open the page via https:// or host it on "http://localhost/".`;
@@ -47,7 +69,7 @@
4769
* LICENSE ALERT - THE END
4870
*/
4971

50-
const divTextContainers = document.querySelector("#div-text-containers");
72+
const divInfContainers = document.querySelector("#div-information-containers");
5173
let scanner;
5274

5375
(async () => {
@@ -68,7 +90,7 @@
6890
console.log(results);
6991

7092
// Clear previous result text elements.
71-
divTextContainers.innerText = "";
93+
divInfContainers.innerText = "";
7294

7395
for (let result of results) {
7496
const l = result.localizationResult;
@@ -88,29 +110,25 @@
88110
const convertedP1 = scanner.convertToPageCoordinates(p1);
89111
const convertedP2 = scanner.convertToPageCoordinates(p2);
90112

91-
textContainer = document.createElement("div");
113+
divInf = document.createElement("div");
114+
divInf.className = 'bubble-box';
92115
const bodyStyle = getComputedStyle(document.body);
93-
if (bodyStyle.position !== "static") {
94-
const rect = document.body.getBoundingClientRect();
95-
const borderWidth = parseInt(bodyStyle.borderWidth);
96-
Object.assign(textContainer.style, {
97-
position: "absolute",
98-
left: convertedP1.x - rect.left - borderWidth + "px",
99-
right: rect.right - convertedP2.x - borderWidth + "px",
100-
bottom: rect.bottom - convertedP1.y - borderWidth + "px",
101-
color: "#FE8E14",
116+
if (bodyStyle.position === "static") {
117+
Object.assign(divInf.style, {
118+
left: `${(convertedP1.x + convertedP2.x) / 2}px`,
119+
bottom: `${document.documentElement.clientHeight - convertedP1.y}px`,
102120
});
103-
} else {
104-
Object.assign(textContainer.style, {
105-
position: "absolute",
106-
left: convertedP1.x + "px",
107-
right: document.documentElement.clientWidth - convertedP2.x + "px",
108-
bottom: document.documentElement.clientHeight - convertedP1.y + "px",
109-
color: "#FE8E14",
121+
} else { // if you set body `position` as `relative`, `absolute`, things can get complicated
122+
const bodyRect = document.body.getBoundingClientRect();
123+
const bodyBorderLeft = parseFloat(bodyStyle.borderLeftWidth);
124+
const bodyBorderBottom = parseFloat(bodyStyle.borderBottomWidth);
125+
Object.assign(divInf.style, {
126+
left: `${(convertedP1.x + convertedP2.x) / 2 - bodyRect.left - bodyBorderLeft}px`,
127+
bottom: `${bodyRect.bottom - convertedP1.y - bodyBorderBottom}px`,
110128
});
111129
}
112-
textContainer.innerText = result.barcodeText;
113-
divTextContainers.append(textContainer);
130+
divInf.innerText = result.barcodeText;
131+
divInfContainers.append(divInf);
114132

115133
/**
116134
* You can also add more information, such as displaying product images.

0 commit comments

Comments
 (0)