veryfi-lens-wasm 2.0.5 → 2.0.7-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/bottleCapOverlay.js +275 -0
- package/components/codeStripsOverlay.js +278 -0
- package/components/createFileUpload.js +28 -6
- package/components/croppedImage.js +38 -3
- package/package.json +1 -1
- package/utils/parser-wrapper.js +118 -1
- package/veryfi-lens.js +460 -56
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a circular overlay for capturing bottle caps
|
|
3
|
+
* @param {HTMLElement} container - The container element
|
|
4
|
+
* @param {Object} options - Configuration options
|
|
5
|
+
* @param {number} [options.circleRadius=0.7] - The circle radius as a proportion of the smaller video dimension (0-1)
|
|
6
|
+
* @param {string} [options.instructionText="Position bottle cap in circle"] - The instruction text to display
|
|
7
|
+
* @returns {Object} The bottle cap overlay object with element and methods
|
|
8
|
+
*/
|
|
9
|
+
export const createBottleCapOverlay = (container, options = {}) => {
|
|
10
|
+
// Get configuration options with defaults
|
|
11
|
+
const circleRadius = options.circleRadius !== undefined ? options.circleRadius : 0.7;
|
|
12
|
+
const instructionText = options.instructionText || "Position bottle cap in circle";
|
|
13
|
+
|
|
14
|
+
// Create overlay container that will match video dimensions
|
|
15
|
+
const overlay = document.createElement("div");
|
|
16
|
+
overlay.id = "veryfi-bottle-cap-overlay";
|
|
17
|
+
overlay.style.position = "absolute";
|
|
18
|
+
overlay.style.zIndex = "100";
|
|
19
|
+
overlay.style.pointerEvents = "none";
|
|
20
|
+
|
|
21
|
+
// Create a full-screen dark mask
|
|
22
|
+
const darkMask = document.createElement("div");
|
|
23
|
+
darkMask.id = "veryfi-bottle-cap-mask";
|
|
24
|
+
darkMask.style.position = "absolute";
|
|
25
|
+
darkMask.style.top = "0";
|
|
26
|
+
darkMask.style.left = "0";
|
|
27
|
+
darkMask.style.width = "100%";
|
|
28
|
+
darkMask.style.height = "100%";
|
|
29
|
+
darkMask.style.zIndex = "1001";
|
|
30
|
+
|
|
31
|
+
// Create a circular "hole" in the mask
|
|
32
|
+
const circleHole = document.createElement("div");
|
|
33
|
+
circleHole.id = "veryfi-bottle-cap-hole";
|
|
34
|
+
circleHole.style.position = "absolute";
|
|
35
|
+
circleHole.style.top = "50%";
|
|
36
|
+
circleHole.style.left = "50%";
|
|
37
|
+
circleHole.style.transform = "translate(-50%, -50%)";
|
|
38
|
+
circleHole.style.borderRadius = "50%";
|
|
39
|
+
circleHole.style.backgroundColor = "transparent";
|
|
40
|
+
circleHole.style.zIndex = "1002";
|
|
41
|
+
// We'll use a radial-gradient as a mask for the hole
|
|
42
|
+
circleHole.style.boxShadow = "0 0 0 9999px rgba(0, 0, 0, 0.6)";
|
|
43
|
+
|
|
44
|
+
// Create a guide circle to mark the target area
|
|
45
|
+
const circleGuide = document.createElement("div");
|
|
46
|
+
circleGuide.id = "veryfi-bottle-cap-guide";
|
|
47
|
+
circleGuide.style.position = "absolute";
|
|
48
|
+
circleGuide.style.top = "50%";
|
|
49
|
+
circleGuide.style.left = "50%";
|
|
50
|
+
circleGuide.style.transform = "translate(-50%, -50%)";
|
|
51
|
+
circleGuide.style.borderRadius = "50%";
|
|
52
|
+
circleGuide.style.border = "2px solid #54C08B";
|
|
53
|
+
circleGuide.style.zIndex = "1003";
|
|
54
|
+
circleGuide.style.pointerEvents = "none";
|
|
55
|
+
circleGuide.style.boxSizing = "border-box";
|
|
56
|
+
|
|
57
|
+
// Create a text label
|
|
58
|
+
const textLabel = document.createElement("div");
|
|
59
|
+
textLabel.id = "veryfi-bottle-cap-label";
|
|
60
|
+
textLabel.textContent = instructionText;
|
|
61
|
+
textLabel.style.position = "absolute";
|
|
62
|
+
textLabel.style.bottom = "25%";
|
|
63
|
+
textLabel.style.left = "0";
|
|
64
|
+
textLabel.style.width = "100%";
|
|
65
|
+
textLabel.style.textAlign = "center";
|
|
66
|
+
textLabel.style.color = "white";
|
|
67
|
+
textLabel.style.fontFamily = "Arial, sans-serif";
|
|
68
|
+
textLabel.style.fontSize = "16px";
|
|
69
|
+
textLabel.style.fontWeight = "bold";
|
|
70
|
+
textLabel.style.zIndex = "1004";
|
|
71
|
+
textLabel.style.textShadow = "0 1px 2px rgba(0,0,0,0.8)";
|
|
72
|
+
|
|
73
|
+
// Create a loading indicator that shows while waiting for camera
|
|
74
|
+
const loadingIndicator = document.createElement("div");
|
|
75
|
+
loadingIndicator.id = "veryfi-bottle-cap-loading";
|
|
76
|
+
loadingIndicator.style.position = "absolute";
|
|
77
|
+
loadingIndicator.style.top = "50%";
|
|
78
|
+
loadingIndicator.style.left = "50%";
|
|
79
|
+
loadingIndicator.style.transform = "translate(-50%, -50%)";
|
|
80
|
+
loadingIndicator.style.width = "40px";
|
|
81
|
+
loadingIndicator.style.height = "40px";
|
|
82
|
+
loadingIndicator.style.borderRadius = "50%";
|
|
83
|
+
loadingIndicator.style.border = "3px solid rgba(255, 255, 255, 0.3)";
|
|
84
|
+
loadingIndicator.style.borderTopColor = "#54C08B";
|
|
85
|
+
loadingIndicator.style.animation = "veryfi-spin 1s linear infinite";
|
|
86
|
+
loadingIndicator.style.zIndex = "1005";
|
|
87
|
+
|
|
88
|
+
// Add keyframes for spinner animation
|
|
89
|
+
const style = document.createElement("style");
|
|
90
|
+
style.textContent = `
|
|
91
|
+
@keyframes veryfi-spin {
|
|
92
|
+
0% { transform: translate(-50%, -50%) rotate(0deg); }
|
|
93
|
+
100% { transform: translate(-50%, -50%) rotate(360deg); }
|
|
94
|
+
}
|
|
95
|
+
`;
|
|
96
|
+
document.head.appendChild(style);
|
|
97
|
+
|
|
98
|
+
// Add elements to the DOM
|
|
99
|
+
overlay.appendChild(darkMask);
|
|
100
|
+
overlay.appendChild(circleHole);
|
|
101
|
+
overlay.appendChild(circleGuide);
|
|
102
|
+
// overlay.appendChild(textLabel);
|
|
103
|
+
overlay.appendChild(loadingIndicator);
|
|
104
|
+
container.appendChild(overlay);
|
|
105
|
+
|
|
106
|
+
// Function to update the overlay position and size to match the video element
|
|
107
|
+
const updateOverlayPosition = () => {
|
|
108
|
+
const videoElement = document.getElementById("veryfi-video-ref");
|
|
109
|
+
if (!videoElement) return;
|
|
110
|
+
|
|
111
|
+
const videoRect = videoElement.getBoundingClientRect();
|
|
112
|
+
const containerRect = container.getBoundingClientRect();
|
|
113
|
+
|
|
114
|
+
// Position the overlay to exactly match the video element
|
|
115
|
+
overlay.style.top = `${videoRect.top - containerRect.top}px`;
|
|
116
|
+
overlay.style.left = `${videoRect.left - containerRect.left}px`;
|
|
117
|
+
overlay.style.width = `${videoRect.width}px`;
|
|
118
|
+
overlay.style.height = `${videoRect.height}px`;
|
|
119
|
+
|
|
120
|
+
// Size the circle based on the smaller dimension of the video and the configured radius
|
|
121
|
+
const circleDiameter = Math.min(videoRect.width, videoRect.height) * circleRadius;
|
|
122
|
+
|
|
123
|
+
// Update circle hole and guide size
|
|
124
|
+
circleHole.style.width = `${circleDiameter}px`;
|
|
125
|
+
circleHole.style.height = `${circleDiameter}px`;
|
|
126
|
+
|
|
127
|
+
// Update guide circle size
|
|
128
|
+
circleGuide.style.width = `${circleDiameter}px`;
|
|
129
|
+
circleGuide.style.height = `${circleDiameter}px`;
|
|
130
|
+
|
|
131
|
+
// Hide loading indicator if video is properly sized and loaded
|
|
132
|
+
if (videoRect.width > 0 && videoElement.readyState >= 2) {
|
|
133
|
+
const loadingIndicator = document.getElementById("veryfi-bottle-cap-loading");
|
|
134
|
+
if (loadingIndicator) {
|
|
135
|
+
loadingIndicator.style.display = "none";
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// Update positioning immediately
|
|
141
|
+
updateOverlayPosition();
|
|
142
|
+
|
|
143
|
+
// Setup continuous monitoring of size and position
|
|
144
|
+
let resizeObserver;
|
|
145
|
+
let mutationObserver;
|
|
146
|
+
|
|
147
|
+
// Setup resize observer to detect any size changes to the video
|
|
148
|
+
try {
|
|
149
|
+
resizeObserver = new ResizeObserver(() => {
|
|
150
|
+
updateOverlayPosition();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Observe container immediately
|
|
154
|
+
resizeObserver.observe(container);
|
|
155
|
+
|
|
156
|
+
// Observe video element if available
|
|
157
|
+
const videoElement = document.getElementById("veryfi-video-ref");
|
|
158
|
+
if (videoElement) {
|
|
159
|
+
resizeObserver.observe(videoElement);
|
|
160
|
+
}
|
|
161
|
+
} catch (e) {
|
|
162
|
+
console.log("ResizeObserver not supported, falling back to window resize event");
|
|
163
|
+
window.addEventListener("resize", updateOverlayPosition);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Set up mutation observer to watch for video element being added/changed
|
|
167
|
+
mutationObserver = new MutationObserver((mutations) => {
|
|
168
|
+
const videoElement = document.getElementById("veryfi-video-ref");
|
|
169
|
+
if (videoElement) {
|
|
170
|
+
updateOverlayPosition();
|
|
171
|
+
if (resizeObserver) {
|
|
172
|
+
resizeObserver.observe(videoElement);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
mutationObserver.observe(container, { childList: true, subtree: true });
|
|
178
|
+
|
|
179
|
+
// Add event listeners for any potential layout changes
|
|
180
|
+
window.addEventListener("resize", updateOverlayPosition);
|
|
181
|
+
window.addEventListener("orientationchange", updateOverlayPosition);
|
|
182
|
+
document.addEventListener("DOMContentLoaded", updateOverlayPosition);
|
|
183
|
+
|
|
184
|
+
// For devtools opening (which can change layout)
|
|
185
|
+
let lastWidth = window.innerWidth;
|
|
186
|
+
window.addEventListener("resize", () => {
|
|
187
|
+
if (window.innerWidth !== lastWidth) {
|
|
188
|
+
lastWidth = window.innerWidth;
|
|
189
|
+
setTimeout(updateOverlayPosition, 300);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
element: overlay,
|
|
195
|
+
|
|
196
|
+
updatePosition: () => {
|
|
197
|
+
updateOverlayPosition();
|
|
198
|
+
},
|
|
199
|
+
|
|
200
|
+
// Method to crop the bottle cap image
|
|
201
|
+
cropBottleCap: (videoElement) => {
|
|
202
|
+
if (!videoElement) return null;
|
|
203
|
+
|
|
204
|
+
// Get the cropImgRef element
|
|
205
|
+
const cropImgRef = document.getElementById("veryfi-crop-img-ref");
|
|
206
|
+
if (!cropImgRef) {
|
|
207
|
+
console.error("Crop image reference not found");
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const videoRect = videoElement.getBoundingClientRect();
|
|
212
|
+
const circleDiameter = Math.min(videoRect.width, videoRect.height) * circleRadius;
|
|
213
|
+
const squareSize = circleDiameter * 0.7; // Square inside the circle
|
|
214
|
+
|
|
215
|
+
// Calculate scaling factors between screen and video dimensions
|
|
216
|
+
const scaleX = videoElement.videoWidth / videoRect.width;
|
|
217
|
+
const scaleY = videoElement.videoHeight / videoRect.height;
|
|
218
|
+
|
|
219
|
+
// Calculate center point in video coordinates
|
|
220
|
+
const centerX = (videoRect.width / 2) * scaleX;
|
|
221
|
+
const centerY = (videoRect.height / 2) * scaleY;
|
|
222
|
+
|
|
223
|
+
// Calculate the square's dimensions in video coordinates
|
|
224
|
+
const squareVideoSize = squareSize * Math.max(scaleX, scaleY);
|
|
225
|
+
|
|
226
|
+
// Calculate the top-left corner of the square in video coordinates
|
|
227
|
+
const cropX = centerX - (squareVideoSize / 2);
|
|
228
|
+
const cropY = centerY - (squareVideoSize / 2);
|
|
229
|
+
|
|
230
|
+
// Set cropImgRef dimensions to match the crop size - make sure width and height are equal
|
|
231
|
+
cropImgRef.width = squareVideoSize;
|
|
232
|
+
cropImgRef.height = squareVideoSize;
|
|
233
|
+
|
|
234
|
+
// Draw the cropped portion to the cropImgRef canvas
|
|
235
|
+
const ctx = cropImgRef.getContext('2d');
|
|
236
|
+
ctx.clearRect(0, 0, cropImgRef.width, cropImgRef.height);
|
|
237
|
+
ctx.drawImage(
|
|
238
|
+
videoElement,
|
|
239
|
+
cropX, cropY, squareVideoSize, squareVideoSize,
|
|
240
|
+
0, 0, squareVideoSize, squareVideoSize
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
// Apply square display styling if the method exists
|
|
244
|
+
if (typeof cropImgRef.setSquareDisplay === 'function') {
|
|
245
|
+
cropImgRef.setSquareDisplay();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Convert to base64
|
|
249
|
+
return cropImgRef.toDataURL('image/jpeg').split(',')[1];
|
|
250
|
+
},
|
|
251
|
+
|
|
252
|
+
hide: () => {
|
|
253
|
+
overlay.style.display = 'none';
|
|
254
|
+
},
|
|
255
|
+
|
|
256
|
+
show: () => {
|
|
257
|
+
overlay.style.display = 'block';
|
|
258
|
+
updateOverlayPosition();
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
cleanup: () => {
|
|
262
|
+
window.removeEventListener("resize", updateOverlayPosition);
|
|
263
|
+
window.removeEventListener("orientationchange", updateOverlayPosition);
|
|
264
|
+
document.removeEventListener("DOMContentLoaded", updateOverlayPosition);
|
|
265
|
+
|
|
266
|
+
if (resizeObserver) {
|
|
267
|
+
resizeObserver.disconnect();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (mutationObserver) {
|
|
271
|
+
mutationObserver.disconnect();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
};
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
export const createCodeStripsOverlay = (container, options = {}) => {
|
|
2
|
+
// Get configuration options with defaults
|
|
3
|
+
const rectWidth = options.rectWidth !== undefined ? options.rectWidth : 0.8;
|
|
4
|
+
const rectHeight = options.rectHeight !== undefined ? options.rectHeight : 0.2;
|
|
5
|
+
const instructionText = options.instructionText || "Position code strip in rectangle";
|
|
6
|
+
const cornerRadius = options.cornerRadius !== undefined ? options.cornerRadius : 10; // Default corner radius in pixels
|
|
7
|
+
|
|
8
|
+
// Create overlay container that will match video dimensions
|
|
9
|
+
const overlay = document.createElement("div");
|
|
10
|
+
overlay.id = "veryfi-code-strip-overlay";
|
|
11
|
+
overlay.style.position = "absolute";
|
|
12
|
+
overlay.style.zIndex = "100";
|
|
13
|
+
overlay.style.pointerEvents = "none";
|
|
14
|
+
|
|
15
|
+
// Create a full-screen dark mask
|
|
16
|
+
const darkMask = document.createElement("div");
|
|
17
|
+
darkMask.id = "veryfi-code-strip-mask";
|
|
18
|
+
darkMask.style.position = "absolute";
|
|
19
|
+
darkMask.style.top = "0";
|
|
20
|
+
darkMask.style.left = "0";
|
|
21
|
+
darkMask.style.width = "100%";
|
|
22
|
+
darkMask.style.height = "100%";
|
|
23
|
+
darkMask.style.zIndex = "1001";
|
|
24
|
+
|
|
25
|
+
// Create a rectangular "hole" in the mask
|
|
26
|
+
const rectHole = document.createElement("div");
|
|
27
|
+
rectHole.id = "veryfi-code-strip-hole";
|
|
28
|
+
rectHole.style.position = "absolute";
|
|
29
|
+
rectHole.style.top = "50%";
|
|
30
|
+
rectHole.style.left = "50%";
|
|
31
|
+
rectHole.style.transform = "translate(-50%, -50%)";
|
|
32
|
+
rectHole.style.backgroundColor = "transparent";
|
|
33
|
+
rectHole.style.zIndex = "1002";
|
|
34
|
+
rectHole.style.borderRadius = cornerRadius + "px"; // Add rounded corners
|
|
35
|
+
// We'll use a box-shadow as a mask for the hole
|
|
36
|
+
rectHole.style.boxShadow = "0 0 0 9999px rgba(0, 0, 0, 0.6)";
|
|
37
|
+
|
|
38
|
+
// Create a guide rectangle to mark the target area
|
|
39
|
+
const rectGuide = document.createElement("div");
|
|
40
|
+
rectGuide.id = "veryfi-code-strip-guide";
|
|
41
|
+
rectGuide.style.position = "absolute";
|
|
42
|
+
rectGuide.style.top = "50%";
|
|
43
|
+
rectGuide.style.left = "50%";
|
|
44
|
+
rectGuide.style.transform = "translate(-50%, -50%)";
|
|
45
|
+
rectGuide.style.border = "2px solid #54C08B";
|
|
46
|
+
rectGuide.style.borderRadius = cornerRadius + "px"; // Add rounded corners
|
|
47
|
+
rectGuide.style.zIndex = "1003";
|
|
48
|
+
rectGuide.style.pointerEvents = "none";
|
|
49
|
+
rectGuide.style.boxSizing = "border-box";
|
|
50
|
+
|
|
51
|
+
// Create a text label
|
|
52
|
+
const textLabel = document.createElement("div");
|
|
53
|
+
textLabel.id = "veryfi-code-strip-label";
|
|
54
|
+
textLabel.textContent = instructionText;
|
|
55
|
+
textLabel.style.position = "absolute";
|
|
56
|
+
textLabel.style.bottom = "25%";
|
|
57
|
+
textLabel.style.left = "0";
|
|
58
|
+
textLabel.style.width = "100%";
|
|
59
|
+
textLabel.style.textAlign = "center";
|
|
60
|
+
textLabel.style.color = "white";
|
|
61
|
+
textLabel.style.fontFamily = "Arial, sans-serif";
|
|
62
|
+
textLabel.style.fontSize = "16px";
|
|
63
|
+
textLabel.style.fontWeight = "bold";
|
|
64
|
+
textLabel.style.zIndex = "1004";
|
|
65
|
+
textLabel.style.textShadow = "0 1px 2px rgba(0,0,0,0.8)";
|
|
66
|
+
|
|
67
|
+
// Create a loading indicator that shows while waiting for camera
|
|
68
|
+
const loadingIndicator = document.createElement("div");
|
|
69
|
+
loadingIndicator.id = "veryfi-code-strip-loading";
|
|
70
|
+
loadingIndicator.style.position = "absolute";
|
|
71
|
+
loadingIndicator.style.top = "50%";
|
|
72
|
+
loadingIndicator.style.left = "50%";
|
|
73
|
+
loadingIndicator.style.transform = "translate(-50%, -50%)";
|
|
74
|
+
loadingIndicator.style.width = "40px";
|
|
75
|
+
loadingIndicator.style.height = "40px";
|
|
76
|
+
loadingIndicator.style.borderRadius = "50%";
|
|
77
|
+
loadingIndicator.style.border = "3px solid rgba(255, 255, 255, 0.3)";
|
|
78
|
+
loadingIndicator.style.borderTopColor = "#54C08B";
|
|
79
|
+
loadingIndicator.style.animation = "veryfi-spin 1s linear infinite";
|
|
80
|
+
loadingIndicator.style.zIndex = "1005";
|
|
81
|
+
|
|
82
|
+
// Add keyframes for spinner animation
|
|
83
|
+
const style = document.createElement("style");
|
|
84
|
+
style.textContent = `
|
|
85
|
+
@keyframes veryfi-spin {
|
|
86
|
+
0% { transform: translate(-50%, -50%) rotate(0deg); }
|
|
87
|
+
100% { transform: translate(-50%, -50%) rotate(360deg); }
|
|
88
|
+
}
|
|
89
|
+
`;
|
|
90
|
+
document.head.appendChild(style);
|
|
91
|
+
|
|
92
|
+
// Add elements to the DOM
|
|
93
|
+
overlay.appendChild(darkMask);
|
|
94
|
+
overlay.appendChild(rectHole);
|
|
95
|
+
overlay.appendChild(rectGuide);
|
|
96
|
+
overlay.appendChild(loadingIndicator);
|
|
97
|
+
container.appendChild(overlay);
|
|
98
|
+
|
|
99
|
+
// Function to update the overlay position and size to match the video element
|
|
100
|
+
const updateOverlayPosition = () => {
|
|
101
|
+
const videoElement = document.getElementById("veryfi-video-ref");
|
|
102
|
+
if (!videoElement) return;
|
|
103
|
+
|
|
104
|
+
const videoRect = videoElement.getBoundingClientRect();
|
|
105
|
+
const containerRect = container.getBoundingClientRect();
|
|
106
|
+
|
|
107
|
+
// Position the overlay to exactly match the video element
|
|
108
|
+
overlay.style.top = `${videoRect.top - containerRect.top}px`;
|
|
109
|
+
overlay.style.left = `${videoRect.left - containerRect.left}px`;
|
|
110
|
+
overlay.style.width = `${videoRect.width}px`;
|
|
111
|
+
overlay.style.height = `${videoRect.height}px`;
|
|
112
|
+
|
|
113
|
+
// Size the rectangle based on video dimensions and the configured size
|
|
114
|
+
const rectWidth_px = videoRect.width * rectWidth;
|
|
115
|
+
const rectHeight_px = videoRect.height * rectHeight;
|
|
116
|
+
|
|
117
|
+
// Update rectangle hole and guide size
|
|
118
|
+
rectHole.style.width = `${rectWidth_px}px`;
|
|
119
|
+
rectHole.style.height = `${rectHeight_px}px`;
|
|
120
|
+
|
|
121
|
+
// Update guide rectangle size
|
|
122
|
+
rectGuide.style.width = `${rectWidth_px}px`;
|
|
123
|
+
rectGuide.style.height = `${rectHeight_px}px`;
|
|
124
|
+
|
|
125
|
+
// Adjust corner radius if needed based on rectangle size
|
|
126
|
+
const dynamicRadius = Math.min(rectWidth_px, rectHeight_px) * 0.15; // 15% of smaller dimension
|
|
127
|
+
const finalRadius = Math.min(cornerRadius, dynamicRadius); // Use the smaller of fixed or dynamic radius
|
|
128
|
+
|
|
129
|
+
rectHole.style.borderRadius = `${finalRadius}px`;
|
|
130
|
+
rectGuide.style.borderRadius = `${finalRadius}px`;
|
|
131
|
+
|
|
132
|
+
// Hide loading indicator if video is properly sized and loaded
|
|
133
|
+
if (videoRect.width > 0 && videoElement.readyState >= 2) {
|
|
134
|
+
const loadingIndicator = document.getElementById("veryfi-code-strip-loading");
|
|
135
|
+
if (loadingIndicator) {
|
|
136
|
+
loadingIndicator.style.display = "none";
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// Update positioning immediately
|
|
142
|
+
updateOverlayPosition();
|
|
143
|
+
|
|
144
|
+
// Setup continuous monitoring of size and position
|
|
145
|
+
let resizeObserver;
|
|
146
|
+
let mutationObserver;
|
|
147
|
+
|
|
148
|
+
// Setup resize observer to detect any size changes to the video
|
|
149
|
+
try {
|
|
150
|
+
resizeObserver = new ResizeObserver(() => {
|
|
151
|
+
updateOverlayPosition();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Observe container immediately
|
|
155
|
+
resizeObserver.observe(container);
|
|
156
|
+
|
|
157
|
+
// Observe video element if available
|
|
158
|
+
const videoElement = document.getElementById("veryfi-video-ref");
|
|
159
|
+
if (videoElement) {
|
|
160
|
+
resizeObserver.observe(videoElement);
|
|
161
|
+
}
|
|
162
|
+
} catch (e) {
|
|
163
|
+
console.log("ResizeObserver not supported, falling back to window resize event");
|
|
164
|
+
window.addEventListener("resize", updateOverlayPosition);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Set up mutation observer to watch for video element being added/changed
|
|
168
|
+
mutationObserver = new MutationObserver((mutations) => {
|
|
169
|
+
const videoElement = document.getElementById("veryfi-video-ref");
|
|
170
|
+
if (videoElement) {
|
|
171
|
+
updateOverlayPosition();
|
|
172
|
+
if (resizeObserver) {
|
|
173
|
+
resizeObserver.observe(videoElement);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
mutationObserver.observe(container, { childList: true, subtree: true });
|
|
179
|
+
|
|
180
|
+
// Add event listeners for any potential layout changes
|
|
181
|
+
window.addEventListener("resize", updateOverlayPosition);
|
|
182
|
+
window.addEventListener("orientationchange", updateOverlayPosition);
|
|
183
|
+
document.addEventListener("DOMContentLoaded", updateOverlayPosition);
|
|
184
|
+
|
|
185
|
+
// For devtools opening (which can change layout)
|
|
186
|
+
let lastWidth = window.innerWidth;
|
|
187
|
+
window.addEventListener("resize", () => {
|
|
188
|
+
if (window.innerWidth !== lastWidth) {
|
|
189
|
+
lastWidth = window.innerWidth;
|
|
190
|
+
setTimeout(updateOverlayPosition, 300);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
element: overlay,
|
|
196
|
+
|
|
197
|
+
updatePosition: () => {
|
|
198
|
+
updateOverlayPosition();
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
// Method to crop the code strip image
|
|
202
|
+
cropCodeStrip: (videoElement) => {
|
|
203
|
+
if (!videoElement) return null;
|
|
204
|
+
|
|
205
|
+
// Get the cropImgRef element
|
|
206
|
+
const cropImgRef = document.getElementById("veryfi-crop-img-ref");
|
|
207
|
+
if (!cropImgRef) {
|
|
208
|
+
console.error("Crop image reference not found");
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const videoRect = videoElement.getBoundingClientRect();
|
|
213
|
+
|
|
214
|
+
// Calculate rectangle dimensions based on video size
|
|
215
|
+
const rectWidth_px = videoRect.width * rectWidth;
|
|
216
|
+
const rectHeight_px = videoRect.height * rectHeight;
|
|
217
|
+
|
|
218
|
+
// Calculate scaling factors between screen and video dimensions
|
|
219
|
+
const scaleX = videoElement.videoWidth / videoRect.width;
|
|
220
|
+
const scaleY = videoElement.videoHeight / videoRect.height;
|
|
221
|
+
|
|
222
|
+
// Calculate center point in video coordinates
|
|
223
|
+
const centerX = (videoRect.width / 2) * scaleX;
|
|
224
|
+
const centerY = (videoRect.height / 2) * scaleY;
|
|
225
|
+
|
|
226
|
+
// Instead of using the rectangle dimensions, use a square with side length equal to the rectangle width
|
|
227
|
+
const squareSize = rectWidth_px * scaleX; // Use the width of the rectangle for the square size
|
|
228
|
+
|
|
229
|
+
// Calculate the top-left corner of the square in video coordinates
|
|
230
|
+
const cropX = centerX - (squareSize / 2);
|
|
231
|
+
const cropY = centerY - (squareSize / 2); // Center the square vertically
|
|
232
|
+
|
|
233
|
+
// Set cropImgRef dimensions to match the square size (equal width and height)
|
|
234
|
+
cropImgRef.width = squareSize;
|
|
235
|
+
cropImgRef.height = squareSize;
|
|
236
|
+
|
|
237
|
+
// Draw the cropped portion to the cropImgRef canvas
|
|
238
|
+
const ctx = cropImgRef.getContext('2d');
|
|
239
|
+
ctx.clearRect(0, 0, cropImgRef.width, cropImgRef.height);
|
|
240
|
+
ctx.drawImage(
|
|
241
|
+
videoElement,
|
|
242
|
+
cropX, cropY, squareSize, squareSize,
|
|
243
|
+
0, 0, squareSize, squareSize
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
// Set square display if the method exists
|
|
247
|
+
if (typeof cropImgRef.setSquareDisplay === 'function') {
|
|
248
|
+
cropImgRef.setSquareDisplay();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Convert to base64
|
|
252
|
+
return cropImgRef.toDataURL('image/jpeg').split(',')[1];
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
hide: () => {
|
|
256
|
+
overlay.style.display = 'none';
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
show: () => {
|
|
260
|
+
overlay.style.display = 'block';
|
|
261
|
+
updateOverlayPosition();
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
cleanup: () => {
|
|
265
|
+
window.removeEventListener("resize", updateOverlayPosition);
|
|
266
|
+
window.removeEventListener("orientationchange", updateOverlayPosition);
|
|
267
|
+
document.removeEventListener("DOMContentLoaded", updateOverlayPosition);
|
|
268
|
+
|
|
269
|
+
if (resizeObserver) {
|
|
270
|
+
resizeObserver.disconnect();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (mutationObserver) {
|
|
274
|
+
mutationObserver.disconnect();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export function createFileUpload(container, captureUploaded, onSubmit) {
|
|
1
|
+
export function createFileUpload(container, captureUploaded, onSubmit, options = {}) {
|
|
2
2
|
const fileUploadComponent = document.createElement('div');
|
|
3
3
|
fileUploadComponent.id = 'veryfi-file-upload';
|
|
4
4
|
|
|
@@ -54,6 +54,14 @@ export function createFileUpload(container, captureUploaded, onSubmit) {
|
|
|
54
54
|
fileUploadComponent.style.imageContainerTop = '0%';
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
// Define default options for button text
|
|
58
|
+
const defaultOptions = {
|
|
59
|
+
cropButtonText: 'Crop',
|
|
60
|
+
resetButtonText: 'Reset',
|
|
61
|
+
submitButtonText: 'Submit',
|
|
62
|
+
dropZoneText: 'Click or drag and drop to upload an image'
|
|
63
|
+
};
|
|
64
|
+
|
|
57
65
|
fileUploadComponent.innerHTML = `
|
|
58
66
|
<div id="image-container" style="
|
|
59
67
|
width: 100%;
|
|
@@ -96,7 +104,7 @@ export function createFileUpload(container, captureUploaded, onSubmit) {
|
|
|
96
104
|
color: var(--file-upload-upload-zone-color, ${defaultStyles.uploadZoneColor});
|
|
97
105
|
font-family: var(--file-upload-upload-zone-font-family, ${defaultStyles.uploadZoneFontFamily});
|
|
98
106
|
">
|
|
99
|
-
|
|
107
|
+
${options.dropZoneText}
|
|
100
108
|
</div>
|
|
101
109
|
<div id="button-container" style="
|
|
102
110
|
display: none;
|
|
@@ -114,7 +122,7 @@ export function createFileUpload(container, captureUploaded, onSubmit) {
|
|
|
114
122
|
font-family: var(--file-upload-button-font-family, ${defaultStyles.buttonFontFamily});
|
|
115
123
|
font-size: var(--file-upload-button-font-size, ${defaultStyles.buttonFontSize});
|
|
116
124
|
width: var(--file-upload-button-width, ${defaultStyles.buttonWidth});
|
|
117
|
-
"
|
|
125
|
+
">${options.cropButtonText}</button>
|
|
118
126
|
<button id="reset-button" style="
|
|
119
127
|
padding: var(--file-upload-button-padding, ${defaultStyles.buttonPadding});
|
|
120
128
|
background-color: var(--file-upload-button-background-color, ${defaultStyles.buttonBackgroundColor});
|
|
@@ -125,7 +133,7 @@ export function createFileUpload(container, captureUploaded, onSubmit) {
|
|
|
125
133
|
font-family: var(--file-upload-button-font-family, ${defaultStyles.buttonFontFamily});
|
|
126
134
|
font-size: var(--file-upload-button-font-size, ${defaultStyles.buttonFontSize});
|
|
127
135
|
width: var(--file-upload-button-width, ${defaultStyles.buttonWidth});
|
|
128
|
-
"
|
|
136
|
+
">${options.resetButtonText}</button>
|
|
129
137
|
<button id="submit-button" style="
|
|
130
138
|
padding: var(--file-upload-button-padding, ${defaultStyles.buttonPadding});
|
|
131
139
|
background-color: var(--file-upload-button-background-color, ${defaultStyles.buttonBackgroundColor});
|
|
@@ -137,7 +145,7 @@ export function createFileUpload(container, captureUploaded, onSubmit) {
|
|
|
137
145
|
font-size: var(--file-upload-button-font-size, ${defaultStyles.buttonFontSize});
|
|
138
146
|
display: none;
|
|
139
147
|
width: var(--file-upload-button-width, ${defaultStyles.buttonWidth});
|
|
140
|
-
"
|
|
148
|
+
">${options.submitButtonText}</button>
|
|
141
149
|
</div>
|
|
142
150
|
</div>
|
|
143
151
|
`;
|
|
@@ -169,6 +177,7 @@ export function createFileUpload(container, captureUploaded, onSubmit) {
|
|
|
169
177
|
submitButton.style.display = 'none';
|
|
170
178
|
cropButton.style.display = 'inline-block';
|
|
171
179
|
resetButton.style.display = 'inline-block';
|
|
180
|
+
fileInput.value = '';
|
|
172
181
|
};
|
|
173
182
|
|
|
174
183
|
const handleFileSelect = (file) => {
|
|
@@ -221,7 +230,20 @@ export function createFileUpload(container, captureUploaded, onSubmit) {
|
|
|
221
230
|
imagePreview.style.display = 'block';
|
|
222
231
|
|
|
223
232
|
cropButton.style.display = 'none';
|
|
224
|
-
|
|
233
|
+
|
|
234
|
+
// Check if submit button should be shown based on enableSubmit setting
|
|
235
|
+
if (container.dataset.enableSubmit !== 'false') {
|
|
236
|
+
submitButton.style.display = 'inline-block';
|
|
237
|
+
} else {
|
|
238
|
+
// If submit is disabled, call the callback directly with the processed image
|
|
239
|
+
// Format the result to match the document flavor format
|
|
240
|
+
console.log("Submit disabled, returning processed image directly");
|
|
241
|
+
onSubmit({
|
|
242
|
+
image: processedImage,
|
|
243
|
+
// Add any other properties that might be expected
|
|
244
|
+
doc_type: 'receipt'
|
|
245
|
+
});
|
|
246
|
+
}
|
|
225
247
|
} catch (error) {
|
|
226
248
|
console.error('Error during image processing:', error);
|
|
227
249
|
alert(`An error occurred: ${error.message}. Please try again or check your connection. If the problem persists, contact support.`);
|