Skip to content

Commit c655101

Browse files
author
Cube
authored
Merge pull request #134 from Dynamsoft/_dev
position the element based on its center position.
2 parents ed98f55 + 01214da commit c655101

File tree

1 file changed

+21
-39
lines changed

1 file changed

+21
-39
lines changed

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

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@
1616
Dynamsoft Barcode Reader Sample - Show result texts on the video
1717
</title>
1818
<style>
19-
.bubble-box{
19+
.bubble-box-container {
2020
position: absolute;
21+
display: flex;
22+
justify-content: center;
23+
}
24+
.bubble-box-container>.bubble-box{
25+
position: absolute;
26+
width: max-content;
27+
max-width: 20rem;
2128
background: white;
2229
border-radius: 4px;
2330
padding: 4px;
2431
word-break: break-all;
2532
word-wrap: break-word;
2633
box-shadow: 5px 5px 5px 3px rgba(0,0,0,0.2);
34+
transform: translateY(calc(-100% - 4px));
2735
}
28-
.bubble-box.adaptive-width{ /*complicated*/
29-
max-width: 20rem; /* change to value you like */
30-
}
31-
.bubble-box.fixed-width{ /*easy*/
32-
width: 20rem; /* change to value you like */
33-
transform: translate(-50%, calc(-100% - 4px));
34-
}
35-
.bubble-box::after{
36+
.bubble-box-container>.bubble-box::after{
3637
content: '';
3738
position: absolute;
3839
bottom: -8px;
@@ -108,8 +109,12 @@
108109
y: Math.max(l.y1, l.y2, l.y3, l.y4),
109110
};
110111

111-
divInf = document.createElement("div");
112-
divInf.classList.add('bubble-box', 'adaptive-width'); // or 'fixed-width'
112+
const divInfContainer = document.createElement("div");
113+
divInfContainer.className = "bubble-box-container";
114+
const divInf = document.createElement("div");
115+
divInf.className = "bubble-box";
116+
divInf.innerText = result.barcodeText;
117+
divInfContainer.append(divInf);
113118
const bodyStyle = getComputedStyle(document.body);
114119
if (bodyStyle.position === "static") {
115120
/**
@@ -119,19 +124,8 @@
119124
const pageP1 = scanner.convertToPageCoordinates(p1);
120125
const pageP2 = scanner.convertToPageCoordinates(p2);
121126
const pageMidX = (pageP1.x + pageP2.x) / 2;
122-
divInf.style.top = `${pageP1.y}px`;
123-
if(divInf.classList.contains('fixed-width')){
124-
// fixed-width, easy
125-
divInf.style.left = `${pageMidX}px`;
126-
}else if(pageMidX < document.documentElement.clientWidth / 2){
127-
// adaptive-width, near left
128-
divInf.style.left = `${pageMidX}px`;
129-
divInf.style.transform = `translate(-50%, calc(-100% - 4px))`;
130-
}else{
131-
// adaptive-width, near right
132-
divInf.style.right = `${document.documentElement.clientWidth - pageMidX}px`;
133-
divInf.style.transform = `translate(50%, calc(-100% - 4px))`;
134-
}
127+
divInfContainer.style.top = `${pageP1.y}px`;
128+
divInfContainer.style.left = `${pageMidX}px`;
135129
} else { // if you set body `position` as `relative`, `absolute`, and so on, things can get complicated.
136130
/**
137131
* 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport.
@@ -141,22 +135,10 @@
141135
const clientP2 = scanner.convertToClientCoordinates(p2);
142136
const clientMidX = (clientP1.x + clientP2.x) / 2;
143137
const bodyRect = document.body.getBoundingClientRect();
144-
divInf.style.top = `${clientP1.y - bodyRect.top - parseFloat(bodyStyle.borderTopWidth)}px`;
145-
if(divInf.classList.contains('fixed-width')){
146-
// fixed-width, easy
147-
divInf.style.left = `${clientMidX - bodyRect.left - parseFloat(bodyStyle.borderLeftWidth)}px`;
148-
}else if(clientMidX < bodyRect.left + bodyRect.width / 2){
149-
// adaptive-width, near left
150-
divInf.style.left = `${clientMidX - bodyRect.left - parseFloat(bodyStyle.borderLeftWidth)}px`;
151-
divInf.style.transform = `translate(-50%, calc(-100% - 4px))`;
152-
}else{
153-
// adaptive-width, near right
154-
divInf.style.right = `${bodyRect.left + bodyRect.width - clientMidX}px`;
155-
divInf.style.transform = `translate(50%, calc(-100% - 4px))`;
156-
}
138+
divInfContainer.style.top = `${clientP1.y - bodyRect.top - parseFloat(bodyStyle.borderTopWidth)}px`;
139+
divInfContainer.style.left = `${clientMidX - bodyRect.left - parseFloat(bodyStyle.borderLeftWidth)}px`;
157140
}
158-
divInf.innerText = result.barcodeText;
159-
divInfContainers.append(divInf);
141+
divInfContainers.append(divInfContainer);
160142

161143
/**
162144
* You can also add more information, such as displaying product images.

0 commit comments

Comments
 (0)