|
16 | 16 | Dynamsoft Barcode Reader Sample - Show result texts on the video |
17 | 17 | </title> |
18 | 18 | <style> |
19 | | - .div-inf{ |
| 19 | + .bubble-box{ |
20 | 20 | position: absolute; |
21 | 21 | background: white; |
| 22 | + border-radius: 4px; |
22 | 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); |
23 | 27 | } |
24 | | - .div-inf::after{ |
| 28 | + .bubble-box::after{ |
25 | 29 | content: ''; |
26 | 30 | position: absolute; |
27 | 31 | 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; |
32 | 36 | } |
33 | 37 | </style> |
34 | 38 | </head> |
|
105 | 109 | const convertedP2 = scanner.convertToPageCoordinates(p2); |
106 | 110 |
|
107 | 111 | 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 |
109 | 117 | 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") { |
113 | 119 | 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`, |
117 | 122 | }); |
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); |
119 | 129 | 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`, |
123 | 132 | }); |
124 | 133 | } |
125 | 134 | divInf.innerText = result.barcodeText; |
|
0 commit comments