veryfi-lens-wasm 3.0.4 → 3.0.5

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/README.md CHANGED
@@ -233,6 +233,9 @@ VeryfiLens.onFailure((error) => { /* error occurred */ });
233
233
  | `prescriptionLabelShowSquareOverlay` | `boolean` | `false` | Show a square/rectangular overlay guide |
234
234
  | `prescriptionLabelSquareWidthRatio` | `number` | `0.8` | Square overlay width (0-1 relative to viewport) |
235
235
  | `prescriptionLabelSquareHeightRatio` | `number` | `0.5` | Square overlay height (0-1 relative to viewport) |
236
+ | `prescriptionLabelShowGuide` | `boolean` | `false` | Show a semi-transparent guide explaining how to record the label. Auto-hides when recording starts and reappears on retake |
237
+ | `prescriptionLabelGuideText` | `string` | `"Position the bottle within the outline.\n\nPress the capture button to start recording, slowly rotate the bottle to capture the entire label and press the capture button again to stop recording."` | Guide text shown when the bottle outline overlay is active. Use `\n` for line breaks |
238
+ | `prescriptionLabelSquareGuideText` | `string` | `"Position the prescription label within the square.\n\nPress the capture button to start recording, press again to stop."` | Guide text shown when the square overlay is active. Use `\n` for line breaks |
236
239
 
237
240
  ### Upload Settings (`upload` flavor)
238
241
 
@@ -0,0 +1,76 @@
1
+ export const createPrescriptionLabelGuide = (container, options = {}) => {
2
+ const text = options.text || '';
3
+
4
+ const guide = document.createElement('div');
5
+ guide.id = 'veryfi-prescription-label-guide';
6
+
7
+ Object.assign(guide.style, {
8
+ position: 'absolute',
9
+ top: 'calc(50% - 120px)',
10
+ left: '50%',
11
+ transform: 'translateX(-50%)',
12
+ width: '240px',
13
+ height: '240px',
14
+ padding: '14px 36px 14px 20px',
15
+ backgroundColor: 'rgba(0, 0, 0, 0.6)',
16
+ color: 'white',
17
+ borderRadius: '12px',
18
+ fontFamily: "system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif",
19
+ fontSize: '14px',
20
+ lineHeight: '1.5',
21
+ textAlign: 'left',
22
+ whiteSpace: 'pre-line',
23
+ zIndex: '1003',
24
+ pointerEvents: 'auto',
25
+ backdropFilter: 'blur(8px)',
26
+ WebkitBackdropFilter: 'blur(8px)',
27
+ border: '1px solid rgba(255, 255, 255, 0.2)',
28
+ boxSizing: 'border-box',
29
+ });
30
+
31
+ const textEl = document.createElement('div');
32
+ textEl.textContent = text;
33
+ guide.appendChild(textEl);
34
+
35
+ const closeButton = document.createElement('button');
36
+ closeButton.id = 'veryfi-prescription-label-guide-close';
37
+ closeButton.setAttribute('aria-label', 'Close guide');
38
+ closeButton.textContent = '\u00D7';
39
+ Object.assign(closeButton.style, {
40
+ position: 'absolute',
41
+ top: '6px',
42
+ right: '6px',
43
+ width: '24px',
44
+ height: '24px',
45
+ padding: '0',
46
+ border: 'none',
47
+ background: 'transparent',
48
+ color: 'white',
49
+ fontSize: '20px',
50
+ lineHeight: '24px',
51
+ fontFamily: 'sans-serif',
52
+ cursor: 'pointer',
53
+ opacity: '0.8',
54
+ pointerEvents: 'auto',
55
+ outline: 'none',
56
+ });
57
+
58
+ closeButton.addEventListener('mouseenter', () => { closeButton.style.opacity = '1'; });
59
+ closeButton.addEventListener('mouseleave', () => { closeButton.style.opacity = '0.8'; });
60
+ closeButton.addEventListener('click', (e) => {
61
+ e.stopPropagation();
62
+ guide.style.display = 'none';
63
+ });
64
+
65
+ guide.appendChild(closeButton);
66
+ container.appendChild(guide);
67
+
68
+ return {
69
+ element: guide,
70
+ show: () => { guide.style.display = 'block'; },
71
+ hide: () => { guide.style.display = 'none'; },
72
+ remove: () => {
73
+ if (guide.parentNode) guide.parentNode.removeChild(guide);
74
+ },
75
+ };
76
+ };
@@ -13,6 +13,8 @@ let videoPreviewEl = null;
13
13
  let retakeButtonEl = null;
14
14
  let playPauseButtonEl = null;
15
15
  let lastVideoBlob = null;
16
+ let cropCanvas = null;
17
+ let cropAnimationFrameId = null;
16
18
 
17
19
  const ensurePulseKeyframes = () => {
18
20
  if (pulseStyleEl) return;
@@ -127,6 +129,96 @@ const getRecorderMimeType = () => {
127
129
  return '';
128
130
  };
129
131
 
132
+ const computeOverlayCropRect = (vw, vh) => {
133
+ if (state.prescriptionLabelShowSquareOverlay) {
134
+ const w = vw * state.prescriptionLabelSquareWidthRatio;
135
+ const h = vh * state.prescriptionLabelSquareHeightRatio;
136
+ const cx = vw / 2;
137
+ const cy = vh * 0.46;
138
+ return { x: cx - w / 2, y: cy - h / 2, width: w, height: h };
139
+ }
140
+
141
+ if (state.prescriptionLabelShowOverlay) {
142
+ const bodyW = vw * state.prescriptionLabelWidthRatio;
143
+ const totalH = vh * state.prescriptionLabelHeightRatio;
144
+ const cx = vw / 2;
145
+ const cy = vh * 0.46;
146
+ return { x: cx - bodyW / 2, y: cy - totalH / 2, width: bodyW, height: totalH };
147
+ }
148
+
149
+ return null;
150
+ };
151
+
152
+ const computeSourceCropRegion = (videoElement) => {
153
+ const videoRect = videoElement.getBoundingClientRect();
154
+ const vw = videoRect.width;
155
+ const vh = videoRect.height;
156
+ const sw = videoElement.videoWidth;
157
+ const sh = videoElement.videoHeight;
158
+
159
+ if (vw <= 0 || vh <= 0 || sw <= 0 || sh <= 0) return null;
160
+
161
+ const overlayRect = computeOverlayCropRect(vw, vh);
162
+ if (!overlayRect) return null;
163
+
164
+ // Map from displayed video rect (object-fit: cover) to source pixels.
165
+ const scale = Math.max(vw / sw, vh / sh);
166
+ const visibleSrcW = vw / scale;
167
+ const visibleSrcH = vh / scale;
168
+ const offsetX = (sw - visibleSrcW) / 2;
169
+ const offsetY = (sh - visibleSrcH) / 2;
170
+
171
+ let srcX = overlayRect.x / scale + offsetX;
172
+ let srcY = overlayRect.y / scale + offsetY;
173
+ let srcW = overlayRect.width / scale;
174
+ let srcH = overlayRect.height / scale;
175
+
176
+ srcX = Math.max(0, Math.min(srcX, sw));
177
+ srcY = Math.max(0, Math.min(srcY, sh));
178
+ srcW = Math.max(1, Math.min(srcW, sw - srcX));
179
+ srcH = Math.max(1, Math.min(srcH, sh - srcY));
180
+
181
+ return { srcX, srcY, srcW, srcH };
182
+ };
183
+
184
+ const startCroppedDrawLoop = (videoElement, region) => {
185
+ // Even dimensions are required by some video codecs (e.g. H.264).
186
+ const outW = Math.max(2, Math.floor(region.srcW / 2) * 2);
187
+ const outH = Math.max(2, Math.floor(region.srcH / 2) * 2);
188
+
189
+ cropCanvas = document.createElement('canvas');
190
+ cropCanvas.width = outW;
191
+ cropCanvas.height = outH;
192
+ const ctx = cropCanvas.getContext('2d');
193
+
194
+ const draw = () => {
195
+ if (!cropCanvas) return;
196
+ if (videoElement.readyState >= 2 && !videoElement.paused && !videoElement.ended) {
197
+ try {
198
+ ctx.drawImage(
199
+ videoElement,
200
+ region.srcX, region.srcY, region.srcW, region.srcH,
201
+ 0, 0, outW, outH
202
+ );
203
+ } catch (e) {
204
+ // ignore transient draw errors (video not ready, etc.)
205
+ }
206
+ }
207
+ cropAnimationFrameId = requestAnimationFrame(draw);
208
+ };
209
+ draw();
210
+
211
+ return cropCanvas.captureStream(30);
212
+ };
213
+
214
+ const stopCroppedDrawLoop = () => {
215
+ if (cropAnimationFrameId !== null) {
216
+ cancelAnimationFrame(cropAnimationFrameId);
217
+ cropAnimationFrameId = null;
218
+ }
219
+ cropCanvas = null;
220
+ };
221
+
130
222
  const startRecording = () => {
131
223
  const stream = state.videoRef?.srcObject;
132
224
  if (!stream) {
@@ -140,8 +232,21 @@ const startRecording = () => {
140
232
  const recorderOptions = mimeType ? { mimeType } : {};
141
233
  console.log(`[prescription_label] MediaRecorder options:`, recorderOptions);
142
234
 
235
+ let recordingStream = stream;
236
+ const region = computeSourceCropRegion(state.videoRef);
237
+ if (region) {
238
+ try {
239
+ recordingStream = startCroppedDrawLoop(state.videoRef, region);
240
+ state.debug_mode && console.log(`[prescription_label] Cropping video to ${cropCanvas.width}x${cropCanvas.height} (source region ${Math.round(region.srcW)}x${Math.round(region.srcH)} at ${Math.round(region.srcX)},${Math.round(region.srcY)})`);
241
+ } catch (e) {
242
+ console.warn('[prescription_label] Failed to set up cropped recording stream, falling back to full video:', e);
243
+ stopCroppedDrawLoop();
244
+ recordingStream = stream;
245
+ }
246
+ }
247
+
143
248
  try {
144
- mediaRecorder = new MediaRecorder(stream, recorderOptions);
249
+ mediaRecorder = new MediaRecorder(recordingStream, recorderOptions);
145
250
  } catch (e) {
146
251
  console.error('Failed to create MediaRecorder:', e);
147
252
  state.failureHandler?.(e);
@@ -157,6 +262,7 @@ const startRecording = () => {
157
262
  mediaRecorder.start(200);
158
263
  isRecording = true;
159
264
  updateRecordingUI(true);
265
+ state.prescriptionLabelGuide?.hide?.();
160
266
  state.updateHandler?.({ status: 'recording_started' });
161
267
  state.debug_mode && console.log('[EVENT] prescription label recording started');
162
268
  };
@@ -174,6 +280,7 @@ const stopRecording = () => {
174
280
  const blob = new Blob(recordedChunks, { type: mediaRecorder.mimeType });
175
281
  recordedChunks = [];
176
282
  mediaRecorder = null;
283
+ stopCroppedDrawLoop();
177
284
  state.updateHandler?.({ status: 'recording_stopped' });
178
285
  state.debug_mode && console.log('[EVENT] prescription label recording stopped, blob size:', blob.size);
179
286
  resolve(blob);
@@ -230,7 +337,7 @@ const showVideoPreview = (videoBlob) => {
230
337
  height: '100vh',
231
338
  maxWidth: 'none',
232
339
  transform: 'none',
233
- objectFit: 'cover',
340
+ objectFit: 'contain',
234
341
  zIndex: '900',
235
342
  background: '#000',
236
343
  });
@@ -241,7 +348,7 @@ const showVideoPreview = (videoBlob) => {
241
348
  height: 'auto',
242
349
  maxWidth: '500px',
243
350
  transform: 'none',
244
- objectFit: 'cover',
351
+ objectFit: 'contain',
245
352
  zIndex: '900',
246
353
  background: '#000',
247
354
  });
@@ -318,6 +425,7 @@ const showVideoPreview = (videoBlob) => {
318
425
  state.submitButton = null;
319
426
  }
320
427
  showVideoOverlay();
428
+ state.prescriptionLabelGuide?.show?.();
321
429
  await newStartLens();
322
430
  });
323
431
  containerEl.appendChild(retakeButtonEl);
@@ -364,6 +472,7 @@ export const capturePrescriptionLabel = async () => {
364
472
  }
365
473
 
366
474
  hideVideoOverlay();
475
+ state.exitButtonRef?.hide?.();
367
476
 
368
477
  if (state.videoRef?.srcObject) {
369
478
  state.videoRef.srcObject.getTracks().forEach((track) => track.stop());
@@ -387,6 +496,7 @@ export const capturePrescriptionLabel = async () => {
387
496
  submitBtnEl.style.padding = '12px 0';
388
497
  submitBtnEl.style.textAlign = 'center';
389
498
  submitBtnEl.style.boxSizing = 'border-box';
499
+ submitBtnEl.style.border = '1px solid transparent';
390
500
  }
391
501
  state.submitButton.show();
392
502
 
@@ -413,5 +523,6 @@ export const cleanupPrescriptionLabelRecording = () => {
413
523
  recordedChunks = [];
414
524
  isRecording = false;
415
525
  updateRecordingUI(false);
526
+ stopCroppedDrawLoop();
416
527
  cleanupPreview();
417
528
  };
package/core/constants.js CHANGED
@@ -12,7 +12,7 @@ export const API_URLS = {
12
12
  };
13
13
 
14
14
  export const DEFAULTS = {
15
- SDK_VERSION: '3.0.4',
15
+ SDK_VERSION: '3.0.5',
16
16
  BLUR_THRESHOLD: 0.7,
17
17
  LCD_THRESHOLD: 0.5,
18
18
  BOX_COLOR: 'rgba(84, 192, 139, 0.6)',
@@ -58,6 +58,8 @@ export const MESSAGES = {
58
58
  WRONG_SIDE_BACK: 'This looks like the front of the check. Please flip and capture the back side.',
59
59
  BOTTLE_CAP: 'Position bottle cap in circle',
60
60
  PRESCRIPTION_LABEL: 'Position prescription label in outline',
61
+ PRESCRIPTION_LABEL_GUIDE: 'Position the bottle within the outline.\n\nPress the capture button to start recording, slowly rotate the bottle to capture the entire label and press the capture button again to stop recording.',
62
+ PRESCRIPTION_SQUARE_GUIDE: 'Position the prescription label within the square.\n\nPress the capture button to start recording, press again to stop.',
61
63
  CODE_STRIPS: 'Position code strip in rectangle',
62
64
  CHECK_POSITION: 'We could not detect a check, hope this will make it easier',
63
65
  ANYDOC_ADD_PAGE: 'Add Page',
package/core/state.js CHANGED
@@ -197,6 +197,10 @@ export const state = {
197
197
  prescriptionLabelShowSquareOverlay: false,
198
198
  prescriptionLabelSquareWidthRatio: DEFAULTS.PRESCRIPTION_SQUARE_WIDTH,
199
199
  prescriptionLabelSquareHeightRatio: DEFAULTS.PRESCRIPTION_SQUARE_HEIGHT,
200
+ prescriptionLabelShowGuide: false,
201
+ prescriptionLabelGuideText: MESSAGES.PRESCRIPTION_LABEL_GUIDE,
202
+ prescriptionLabelSquareGuideText: MESSAGES.PRESCRIPTION_SQUARE_GUIDE,
203
+ prescriptionLabelGuide: null,
200
204
  preferredVideoFormat: null,
201
205
 
202
206
  codeStripsOverlay: null,
@@ -311,6 +315,7 @@ export const resetState = () => {
311
315
  state.bottleCapOverlay = null;
312
316
  state.prescriptionLabelOverlay = null;
313
317
  state.prescriptionSquareOverlay = null;
318
+ state.prescriptionLabelGuide = null;
314
319
  state.codeStripsOverlay = null;
315
320
  state.checkOverlay = null;
316
321
  state.isManualCheckCapture = false;
@@ -18,6 +18,7 @@ import { createBottleCapOverlay } from '../components/bottleCapOverlay.js';
18
18
  import { createCodeStripsOverlay } from '../components/codeStripsOverlay.js';
19
19
  import { createPrescriptionLabelOverlay } from '../components/prescriptionLabelOverlay.js';
20
20
  import { createPrescriptionSquareOverlay } from '../components/prescriptionSquareOverlay.js';
21
+ import { createPrescriptionLabelGuide } from '../components/prescriptionLabelGuide.js';
21
22
  import { createMirrorButton } from '../components/mirrorButton.js';
22
23
  import { createSwitchCameraButton } from '../components/switchCameraButton.js';
23
24
 
@@ -302,6 +303,16 @@ export const renderPrescriptionLabelOverlayIfNeeded = () => {
302
303
  squareHeightRatio: state.prescriptionLabelSquareHeightRatio,
303
304
  });
304
305
  }
306
+
307
+ if (state.prescriptionLabelShowGuide && !state.prescriptionLabelGuide) {
308
+ const guideText = state.prescriptionLabelShowSquareOverlay
309
+ ? state.prescriptionLabelSquareGuideText
310
+ : state.prescriptionLabelGuideText;
311
+ state.prescriptionLabelGuide = createPrescriptionLabelGuide(
312
+ state.videoOverlay?.overlay || state.container,
313
+ { text: guideText }
314
+ );
315
+ }
305
316
  };
306
317
 
307
318
  export const renderCodeStripsOverlayIfNeeded = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "3.0.4",
3
+ "version": "3.0.5",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",