Skip to content

Commit 9ecb658

Browse files
authored
Update 3.show-result-texts-on-the-video.html
1 parent 91b6ee4 commit 9ecb658

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@
1616
Dynamsoft Barcode Reader Sample - Show result texts on the video
1717
</title>
1818
<style>
19-
.div-inf{
19+
.bubble-box{
2020
position: absolute;
2121
background: white;
22+
border-radius: 4px;
2223
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);
2327
}
24-
.div-inf::after{
28+
.bubble-box::after{
2529
content: '';
2630
position: absolute;
2731
bottom: -8px;
28-
background: white;
29-
border-left: 8px solid white;
30-
border-bottom: 8px solid white;
31-
border-right: 8px solid white;
32+
left: calc(50% - 8px);
33+
border-left: 8px solid transparent;
34+
border-top: 8px solid white;
35+
border-right: 8px solid transparent;
3236
}
3337
</style>
3438
</head>
@@ -105,21 +109,26 @@
105109
const convertedP2 = scanner.convertToPageCoordinates(p2);
106110

107111
divInf = document.createElement("div");
108-
divInf.className = 'div-inf';
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
109117
const bodyStyle = getComputedStyle(document.body);
110-
if (bodyStyle.position !== "static") {
111-
const rect = document.body.getBoundingClientRect();
112-
const borderWidth = parseInt(bodyStyle.borderWidth);
118+
if (bodyStyle.position === "static") {
113119
Object.assign(divInf.style, {
114-
left: convertedP1.x - rect.left - borderWidth + "px",
115-
right: rect.right - convertedP2.x - borderWidth + "px",
116-
bottom: rect.bottom - convertedP1.y - borderWidth + "px",
120+
left: `calc(${(convertedP1.x + convertedP2.x) / 2}px - ${divInfWidth} / 2)`,
121+
bottom: `${document.documentElement.clientHeight - convertedP1.y + divInfVerticalOffset}px`,
117122
});
118-
} else {
123+
} else { // if you set body `position` as `relative`, `absolute`, things can get complicated
124+
const bodyRect = document.body.getBoundingClientRect();
125+
const bodyBorderLeft = parseInt(bodyStyle.borderLeftWidth);
126+
const bodyBorderBottom = parseInt(bodyStyle.borderBottomWidth);
127+
const bodyPaddingLeft = parseInt(bodyStyle.paddingLeft);
128+
const bodyPaddingBottom = parseInt(bodyStyle.paddingBottom);
119129
Object.assign(divInf.style, {
120-
left: convertedP1.x + "px",
121-
right: document.documentElement.clientWidth - convertedP2.x + "px",
122-
bottom: document.documentElement.clientHeight - convertedP1.y + "px",
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`,
123132
});
124133
}
125134
divInf.innerText = result.barcodeText;

0 commit comments

Comments
 (0)