Skip to content

Commit f1edf41

Browse files
authored
adaptive width
1 parent 318b999 commit f1edf41

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

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

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
</title>
1818
<style>
1919
.bubble-box{
20-
max-width: 20rem; /* You are likely to change it */
2120
position: absolute;
22-
right: -999px;
2321
background: white;
2422
border-radius: 4px;
2523
padding: 4px;
2624
word-break: break-all;
2725
word-wrap: break-word;
2826
box-shadow: 5px 5px 5px 3px rgba(0,0,0,0.2);
27+
}
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 */
2933
transform: translate(-50%, calc(-100% - 4px));
3034
}
3135
.bubble-box::after{
@@ -105,33 +109,51 @@
105109
};
106110

107111
divInf = document.createElement("div");
108-
divInf.className = 'bubble-box';
112+
divInf.classList.add('bubble-box', 'adaptive-width'); // or 'fixed-width'
109113
const bodyStyle = getComputedStyle(document.body);
110114
if (bodyStyle.position === "static") {
111115
/**
112116
* 'convertToPageCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the document.
113117
* Then we can place a div element according to the converted coordinate.
114118
*/
115-
const convertedP1 = scanner.convertToPageCoordinates(p1);
116-
const convertedP2 = scanner.convertToPageCoordinates(p2);
117-
Object.assign(divInf.style, {
118-
left: `${(convertedP1.x + convertedP2.x) / 2}px`,
119-
top: `${convertedP1.y}px`,
120-
});
121-
} else { // if you set body `position` as `relative`, `absolute`, things can get complicated
119+
const pageP1 = scanner.convertToPageCoordinates(p1);
120+
const pageP2 = scanner.convertToPageCoordinates(p2);
121+
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+
}
135+
} else { // if you set body `position` as `relative`, `absolute`, and so on, things can get complicated.
122136
/**
123137
* 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport.
124138
* Then we can place a div element according to the converted coordinate.
125139
*/
126-
const convertedP1 = scanner.convertToClientCoordinates(p1);
127-
const convertedP2 = scanner.convertToClientCoordinates(p2);
140+
const clientP1 = scanner.convertToClientCoordinates(p1);
141+
const clientP2 = scanner.convertToClientCoordinates(p2);
142+
const clientMidX = (clientP1.x + clientP2.x) / 2;
128143
const bodyRect = document.body.getBoundingClientRect();
129-
const bodyBorderLeft = parseFloat(bodyStyle.borderLeftWidth);
130-
const bodyBorderTop = parseFloat(bodyStyle.borderTopWidth);
131-
Object.assign(divInf.style, {
132-
left: `${(convertedP1.x + convertedP2.x) / 2 - bodyRect.left - bodyBorderLeft}px`,
133-
top: `${convertedP1.y - bodyRect.top - bodyBorderTop}px`,
134-
});
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+
}
135157
}
136158
divInf.innerText = result.barcodeText;
137159
divInfContainers.append(divInf);

0 commit comments

Comments
 (0)