|
104 | 104 | y: Math.max(l.y1, l.y2, l.y3, l.y4), |
105 | 105 | }; |
106 | 106 |
|
107 | | - /** |
108 | | - * 'convertToPageCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the document. |
109 | | - * Then we can place a div element according to the converted coordinate. |
110 | | - */ |
111 | | - const convertedP1 = scanner.convertToPageCoordinates(p1); |
112 | | - const convertedP2 = scanner.convertToPageCoordinates(p2); |
113 | | - |
114 | 107 | divInf = document.createElement("div"); |
115 | 108 | divInf.className = 'bubble-box'; |
116 | 109 | const bodyStyle = getComputedStyle(document.body); |
117 | 110 | if (bodyStyle.position === "static") { |
| 111 | + /** |
| 112 | + * 'convertToPageCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the document. |
| 113 | + * Then we can place a div element according to the converted coordinate. |
| 114 | + */ |
| 115 | + const convertedP1 = scanner.convertToPageCoordinates(p1); |
| 116 | + const convertedP2 = scanner.convertToPageCoordinates(p2); |
118 | 117 | Object.assign(divInf.style, { |
119 | 118 | left: `${(convertedP1.x + convertedP2.x) / 2}px`, |
120 | 119 | top: `${convertedP1.y}px`, |
121 | 120 | }); |
122 | 121 | } else { // if you set body `position` as `relative`, `absolute`, things can get complicated |
| 122 | + /** |
| 123 | + * 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport. |
| 124 | + * Then we can place a div element according to the converted coordinate. |
| 125 | + */ |
| 126 | + const convertedP1 = scanner.convertToClientCoordinates(p1); |
| 127 | + const convertedP2 = scanner.convertToClientCoordinates(p2); |
123 | 128 | const bodyRect = document.body.getBoundingClientRect(); |
124 | 129 | const bodyBorderLeft = parseFloat(bodyStyle.borderLeftWidth); |
125 | 130 | const bodyBorderTop = parseFloat(bodyStyle.borderTopWidth); |
126 | 131 | Object.assign(divInf.style, { |
127 | | - left: `${(convertedP1.x + convertedP2.x) / 2 - (bodyRect.left + document.documentElement.scrollLeft) - bodyBorderLeft}px`, |
128 | | - top: `${convertedP1.y - (bodyRect.top + document.documentElement.scrollTop) - bodyBorderTop}px`, |
| 132 | + left: `${(convertedP1.x + convertedP2.x) / 2 - bodyRect.left - bodyBorderLeft}px`, |
| 133 | + top: `${convertedP1.y - bodyRect.top - bodyBorderTop}px`, |
129 | 134 | }); |
130 | 135 | } |
131 | 136 | divInf.innerText = result.barcodeText; |
|
0 commit comments