Skip to content

Commit 8ff04b9

Browse files
author
Cube
authored
Merge pull request #125 from Dynamsoft/Keillion-patch-2
bubble box
2 parents 707976c + 5f8b8ca commit 8ff04b9

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

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

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,36 @@
1515
<title>
1616
Dynamsoft Barcode Reader Sample - Show result texts on the video
1717
</title>
18+
<style>
19+
.bubble-box{
20+
position: absolute;
21+
background: white;
22+
border-radius: 4px;
23+
padding: 4px;
24+
word-break: break-all;
25+
word-wrap: break-word;
26+
box-shadow: 5px 5px 5px 3px rgba(0,0,0,0.2);
27+
}
28+
.bubble-box::after{
29+
content: '';
30+
position: absolute;
31+
bottom: -8px;
32+
left: calc(50% - 8px);
33+
border-left: 8px solid transparent;
34+
border-top: 8px solid white;
35+
border-right: 8px solid transparent;
36+
}
37+
</style>
38+
</head>
39+
<body>
40+
<div id="div-ui-container" style="width: 100%; height: 90vh"></div>
41+
<div id="div-information-containers"></div>
1842
<!--
1943
This sample makes use of the library hosted by the CDN jsDelivr. If you would rather use the
2044
library offline. Please see the guide on how to host the library:
2145
https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=latest#host-the-library-yourself-recommended
2246
-->
2347
<script src="https://cdn.jsdelivr.net/npm/[email protected]/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>
2848
<script>
2949
if (location.protocol === "file:") {
3050
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 +67,7 @@
4767
* LICENSE ALERT - THE END
4868
*/
4969

50-
const divTextContainers = document.querySelector("#div-text-containers");
70+
const divInfContainers = document.querySelector("#div-information-containers");
5171
let scanner;
5272

5373
(async () => {
@@ -68,7 +88,7 @@
6888
console.log(results);
6989

7090
// Clear previous result text elements.
71-
divTextContainers.innerText = "";
91+
divInfContainers.innerText = "";
7292

7393
for (let result of results) {
7494
const l = result.localizationResult;
@@ -88,29 +108,31 @@
88108
const convertedP1 = scanner.convertToPageCoordinates(p1);
89109
const convertedP2 = scanner.convertToPageCoordinates(p2);
90110

91-
textContainer = document.createElement("div");
111+
divInf = document.createElement("div");
112+
divInf.className = 'bubble-box';
113+
// define width of the bubble box
114+
const divInfWidth = "20rem";
115+
divInf.style.width = divInfWidth;
116+
const divInfVerticalOffset = 4; //px
92117
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",
118+
if (bodyStyle.position === "static") {
119+
Object.assign(divInf.style, {
120+
left: `calc(${(convertedP1.x + convertedP2.x) / 2}px - ${divInfWidth} / 2)`,
121+
bottom: `${document.documentElement.clientHeight - convertedP1.y + divInfVerticalOffset}px`,
102122
});
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",
123+
} else { // if you set body `position` as `relative`, `absolute`, things can get complicated
124+
const bodyRect = document.body.getBoundingClientRect();
125+
const bodyBorderLeft = parseFloat(bodyStyle.borderLeftWidth);
126+
const bodyBorderBottom = parseFloat(bodyStyle.borderBottomWidth);
127+
const bodyPaddingLeft = parseFloat(bodyStyle.paddingLeft);
128+
const bodyPaddingBottom = parseFloat(bodyStyle.paddingBottom);
129+
Object.assign(divInf.style, {
130+
left: `calc(${(convertedP1.x + convertedP2.x) / 2 - bodyRect.left - bodyBorderLeft - bodyPaddingLeft}px - ${divInfWidth} / 2)`,
131+
bottom: `${bodyRect.bottom - convertedP1.y - bodyBorderBottom - bodyPaddingBottom + divInfVerticalOffset}px`,
110132
});
111133
}
112-
textContainer.innerText = result.barcodeText;
113-
divTextContainers.append(textContainer);
134+
divInf.innerText = result.barcodeText;
135+
divInfContainers.append(divInf);
114136

115137
/**
116138
* You can also add more information, such as displaying product images.

0 commit comments

Comments
 (0)