|
16 | 16 | Dynamsoft Barcode Reader Sample - Show result texts on the video |
17 | 17 | </title> |
18 | 18 | <style> |
19 | | - .bubble-box{ |
| 19 | + .bubble-box-container { |
20 | 20 | 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; |
21 | 28 | background: white; |
22 | 29 | border-radius: 4px; |
23 | 30 | padding: 4px; |
24 | 31 | word-break: break-all; |
25 | 32 | word-wrap: break-word; |
26 | 33 | box-shadow: 5px 5px 5px 3px rgba(0,0,0,0.2); |
| 34 | + transform: translateY(calc(-100% - 4px)); |
27 | 35 | } |
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{ |
36 | 37 | content: ''; |
37 | 38 | position: absolute; |
38 | 39 | bottom: -8px; |
|
108 | 109 | y: Math.max(l.y1, l.y2, l.y3, l.y4), |
109 | 110 | }; |
110 | 111 |
|
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); |
113 | 118 | const bodyStyle = getComputedStyle(document.body); |
114 | 119 | if (bodyStyle.position === "static") { |
115 | 120 | /** |
|
119 | 124 | const pageP1 = scanner.convertToPageCoordinates(p1); |
120 | 125 | const pageP2 = scanner.convertToPageCoordinates(p2); |
121 | 126 | 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`; |
135 | 129 | } else { // if you set body `position` as `relative`, `absolute`, and so on, things can get complicated. |
136 | 130 | /** |
137 | 131 | * 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport. |
|
141 | 135 | const clientP2 = scanner.convertToClientCoordinates(p2); |
142 | 136 | const clientMidX = (clientP1.x + clientP2.x) / 2; |
143 | 137 | 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`; |
157 | 140 | } |
158 | | - divInf.innerText = result.barcodeText; |
159 | | - divInfContainers.append(divInf); |
| 141 | + divInfContainers.append(divInfContainer); |
160 | 142 |
|
161 | 143 | /** |
162 | 144 | * You can also add more information, such as displaying product images. |
|
0 commit comments