@capgo/camera-preview 7.3.12 → 7.4.0-beta.10

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.
Files changed (61) hide show
  1. package/CapgoCameraPreview.podspec +16 -13
  2. package/README.md +467 -73
  3. package/android/.gradle/8.14.2/checksums/checksums.lock +0 -0
  4. package/android/.gradle/8.14.2/checksums/md5-checksums.bin +0 -0
  5. package/android/.gradle/8.14.2/checksums/sha1-checksums.bin +0 -0
  6. package/android/.gradle/8.14.2/executionHistory/executionHistory.bin +0 -0
  7. package/android/.gradle/8.14.2/executionHistory/executionHistory.lock +0 -0
  8. package/android/.gradle/8.14.2/fileChanges/last-build.bin +0 -0
  9. package/android/.gradle/8.14.2/fileHashes/fileHashes.bin +0 -0
  10. package/android/.gradle/8.14.2/fileHashes/fileHashes.lock +0 -0
  11. package/android/.gradle/8.14.2/fileHashes/resourceHashesCache.bin +0 -0
  12. package/android/.gradle/8.14.2/gc.properties +0 -0
  13. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  14. package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
  15. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  16. package/android/.gradle/file-system.probe +0 -0
  17. package/android/.gradle/vcs-1/gc.properties +0 -0
  18. package/android/build.gradle +11 -0
  19. package/android/gradle/wrapper/gradle-wrapper.properties +1 -1
  20. package/android/src/main/AndroidManifest.xml +5 -3
  21. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java +472 -541
  22. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraXView.java +1648 -0
  23. package/android/src/main/java/com/ahm/capacitor/camera/preview/GridOverlayView.java +82 -0
  24. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraDevice.java +54 -0
  25. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraLens.java +70 -0
  26. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraSessionConfiguration.java +79 -0
  27. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/LensInfo.java +34 -0
  28. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/ZoomFactors.java +34 -0
  29. package/dist/docs.json +934 -154
  30. package/dist/esm/definitions.d.ts +445 -83
  31. package/dist/esm/definitions.js +10 -1
  32. package/dist/esm/definitions.js.map +1 -1
  33. package/dist/esm/web.d.ts +73 -3
  34. package/dist/esm/web.js +492 -68
  35. package/dist/esm/web.js.map +1 -1
  36. package/dist/plugin.cjs.js +498 -68
  37. package/dist/plugin.cjs.js.map +1 -1
  38. package/dist/plugin.js +498 -68
  39. package/dist/plugin.js.map +1 -1
  40. package/ios/{Plugin → Sources/CapgoCameraPreview}/CameraController.swift +601 -59
  41. package/ios/Sources/CapgoCameraPreview/GridOverlayView.swift +65 -0
  42. package/ios/Sources/CapgoCameraPreview/Plugin.swift +1369 -0
  43. package/ios/Tests/CameraPreviewPluginTests/CameraPreviewPluginTests.swift +15 -0
  44. package/package.json +1 -1
  45. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraActivity.java +0 -1279
  46. package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomSurfaceView.java +0 -29
  47. package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomTextureView.java +0 -39
  48. package/android/src/main/java/com/ahm/capacitor/camera/preview/Preview.java +0 -461
  49. package/android/src/main/java/com/ahm/capacitor/camera/preview/TapGestureDetector.java +0 -24
  50. package/ios/Plugin/Info.plist +0 -24
  51. package/ios/Plugin/Plugin.h +0 -10
  52. package/ios/Plugin/Plugin.m +0 -18
  53. package/ios/Plugin/Plugin.swift +0 -511
  54. package/ios/Plugin.xcodeproj/project.pbxproj +0 -593
  55. package/ios/Plugin.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  56. package/ios/Plugin.xcworkspace/contents.xcworkspacedata +0 -10
  57. package/ios/Plugin.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  58. package/ios/PluginTests/Info.plist +0 -22
  59. package/ios/PluginTests/PluginTests.swift +0 -83
  60. package/ios/Podfile +0 -13
  61. package/ios/Podfile.lock +0 -23
package/dist/esm/web.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { WebPlugin } from "@capacitor/core";
2
+ import { DeviceType } from "./definitions";
3
+ const DEFAULT_VIDEO_ID = "capgo_video";
2
4
  export class CameraPreviewWeb extends WebPlugin {
3
5
  constructor() {
4
6
  super();
@@ -7,80 +9,101 @@ export class CameraPreviewWeb extends WebPlugin {
7
9
  * used in capture
8
10
  */
9
11
  this.isBackCamera = false;
12
+ this.currentDeviceId = null;
13
+ this.videoElement = null;
14
+ this.isStarted = false;
10
15
  }
11
16
  async getSupportedPictureSizes() {
12
17
  throw new Error("getSupportedPictureSizes not supported under the web platform");
13
18
  }
14
19
  async start(options) {
15
- var _a;
16
- await navigator.mediaDevices
17
- .getUserMedia({
18
- audio: !options.disableAudio,
19
- video: true,
20
- })
21
- .then((stream) => {
22
- // Stop any existing stream so we can request media with different constraints based on user input
23
- stream.getTracks().forEach((track) => track.stop());
24
- })
25
- .catch((error) => {
26
- Promise.reject(error);
27
- });
28
- const video = document.getElementById("video");
20
+ if (options.aspectRatio && (options.width || options.height)) {
21
+ throw new Error("Cannot set both aspectRatio and size (width/height). Use setPreviewSize after start.");
22
+ }
23
+ if (this.isStarted) {
24
+ throw new Error("camera already started");
25
+ }
26
+ this.isBackCamera = true;
27
+ this.isStarted = false;
29
28
  const parent = document.getElementById((options === null || options === void 0 ? void 0 : options.parent) || "");
30
- if (!video) {
31
- const videoElement = document.createElement("video");
32
- videoElement.id = "video";
33
- videoElement.setAttribute("class", (options === null || options === void 0 ? void 0 : options.className) || "");
34
- // Don't flip video feed if camera is rear facing
35
- if (options.position !== "rear") {
36
- videoElement.setAttribute("style", "-webkit-transform: scaleX(-1); transform: scaleX(-1);");
37
- }
38
- const userAgent = navigator.userAgent.toLowerCase();
39
- const isSafari = userAgent.includes("safari") && !userAgent.includes("chrome");
40
- // Safari on iOS needs to have the autoplay, muted and playsinline attributes set for video.play() to be successful
41
- // Without these attributes videoElement.play() will throw a NotAllowedError
42
- // https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari
43
- if (isSafari) {
44
- videoElement.setAttribute("autoplay", "true");
45
- videoElement.setAttribute("muted", "true");
46
- videoElement.setAttribute("playsinline", "true");
47
- }
48
- parent === null || parent === void 0 ? void 0 : parent.appendChild(videoElement);
49
- if ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia) {
50
- const constraints = {
51
- video: {
52
- width: { ideal: options.width },
53
- height: { ideal: options.height },
54
- },
55
- };
56
- if (options.position === "rear") {
57
- constraints.video.facingMode =
58
- "environment";
59
- this.isBackCamera = true;
60
- }
61
- else {
62
- this.isBackCamera = false;
63
- }
64
- const self = this;
65
- await navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
66
- if (document.getElementById("video")) {
67
- // video.src = window.URL.createObjectURL(stream);
68
- videoElement.srcObject = stream;
69
- videoElement.play();
70
- Promise.resolve({});
71
- }
72
- else {
73
- self.stopStream(stream);
74
- Promise.reject(new Error("camera already stopped"));
75
- }
76
- }, (err) => {
77
- Promise.reject(new Error(err));
78
- });
29
+ const gridMode = (options === null || options === void 0 ? void 0 : options.gridMode) || "none";
30
+ if (options.position) {
31
+ this.isBackCamera = options.position === "rear";
32
+ }
33
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
34
+ if (video) {
35
+ video.remove();
36
+ }
37
+ const container = options.parent ? document.getElementById(options.parent) : document.body;
38
+ if (!container) {
39
+ throw new Error("container not found");
40
+ }
41
+ this.videoElement = document.createElement("video");
42
+ this.videoElement.id = DEFAULT_VIDEO_ID;
43
+ this.videoElement.className = options.className || "";
44
+ this.videoElement.playsInline = true;
45
+ this.videoElement.muted = true;
46
+ this.videoElement.autoplay = true;
47
+ container.appendChild(this.videoElement);
48
+ if (options.toBack) {
49
+ this.videoElement.style.zIndex = "-1";
50
+ }
51
+ if (options.width) {
52
+ this.videoElement.width = options.width;
53
+ }
54
+ if (options.height) {
55
+ this.videoElement.height = options.height;
56
+ }
57
+ if (options.x) {
58
+ this.videoElement.style.left = `${options.x}px`;
59
+ }
60
+ // Create and add grid overlay if needed
61
+ if (gridMode !== "none") {
62
+ const gridOverlay = this.createGridOverlay(gridMode);
63
+ gridOverlay.id = "camera-grid-overlay";
64
+ parent === null || parent === void 0 ? void 0 : parent.appendChild(gridOverlay);
65
+ }
66
+ if (options.y) {
67
+ this.videoElement.style.top = `${options.y}px`;
68
+ }
69
+ if (options.aspectRatio) {
70
+ const [widthRatio, heightRatio] = options.aspectRatio.split(':').map(Number);
71
+ const ratio = widthRatio / heightRatio;
72
+ if (options.width) {
73
+ this.videoElement.height = options.width / ratio;
74
+ }
75
+ else if (options.height) {
76
+ this.videoElement.width = options.height * ratio;
79
77
  }
80
78
  }
81
79
  else {
82
- Promise.reject(new Error("camera already started"));
80
+ this.videoElement.style.objectFit = 'cover';
81
+ }
82
+ const constraints = {
83
+ video: {
84
+ width: { ideal: this.videoElement.width || 640 },
85
+ height: { ideal: this.videoElement.height || window.innerHeight },
86
+ facingMode: this.isBackCamera ? "environment" : "user",
87
+ },
88
+ };
89
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
90
+ if (!stream) {
91
+ throw new Error("could not acquire stream");
83
92
  }
93
+ if (!this.videoElement) {
94
+ throw new Error("video element not found");
95
+ }
96
+ this.videoElement.srcObject = stream;
97
+ if (!this.isBackCamera) {
98
+ this.videoElement.style.transform = "scaleX(-1)";
99
+ }
100
+ this.isStarted = true;
101
+ return {
102
+ width: this.videoElement.width,
103
+ height: this.videoElement.height,
104
+ x: this.videoElement.getBoundingClientRect().x,
105
+ y: this.videoElement.getBoundingClientRect().y,
106
+ };
84
107
  }
85
108
  stopStream(stream) {
86
109
  if (stream) {
@@ -90,16 +113,20 @@ export class CameraPreviewWeb extends WebPlugin {
90
113
  }
91
114
  }
92
115
  async stop() {
93
- const video = document.getElementById("video");
116
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
94
117
  if (video) {
95
118
  video.pause();
96
119
  this.stopStream(video.srcObject);
97
120
  video.remove();
121
+ this.isStarted = false;
98
122
  }
123
+ // Remove grid overlay if it exists
124
+ const gridOverlay = document.getElementById("camera-grid-overlay");
125
+ gridOverlay === null || gridOverlay === void 0 ? void 0 : gridOverlay.remove();
99
126
  }
100
127
  async capture(options) {
101
128
  return new Promise((resolve, reject) => {
102
- const video = document.getElementById("video");
129
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
103
130
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
104
131
  reject(new Error("camera is not running"));
105
132
  return;
@@ -117,6 +144,12 @@ export class CameraPreviewWeb extends WebPlugin {
117
144
  context === null || context === void 0 ? void 0 : context.scale(-1, 1);
118
145
  }
119
146
  context === null || context === void 0 ? void 0 : context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
147
+ if (options.saveToGallery) {
148
+ // saveToGallery is not supported on web
149
+ }
150
+ if (options.withExifLocation) {
151
+ // withExifLocation is not supported on web
152
+ }
120
153
  if ((options.format || "jpeg") === "jpeg") {
121
154
  base64EncodedImage = canvas
122
155
  .toDataURL("image/jpeg", (options.quality || 85) / 100.0)
@@ -130,6 +163,7 @@ export class CameraPreviewWeb extends WebPlugin {
130
163
  }
131
164
  resolve({
132
165
  value: base64EncodedImage,
166
+ exif: {},
133
167
  });
134
168
  });
135
169
  }
@@ -153,12 +187,402 @@ export class CameraPreviewWeb extends WebPlugin {
153
187
  throw new Error(`setFlashMode not supported under the web platform${_options}`);
154
188
  }
155
189
  async flip() {
156
- throw new Error("flip not supported under the web platform");
190
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
191
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
192
+ throw new Error("camera is not running");
193
+ }
194
+ // Stop current stream
195
+ this.stopStream(video.srcObject);
196
+ // Toggle camera position
197
+ this.isBackCamera = !this.isBackCamera;
198
+ // Get new constraints
199
+ const constraints = {
200
+ video: {
201
+ facingMode: this.isBackCamera ? "environment" : "user",
202
+ width: { ideal: video.videoWidth || 640 },
203
+ height: { ideal: video.videoHeight || 480 },
204
+ },
205
+ };
206
+ try {
207
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
208
+ video.srcObject = stream;
209
+ // Update current device ID from the new stream
210
+ const videoTrack = stream.getVideoTracks()[0];
211
+ if (videoTrack) {
212
+ this.currentDeviceId = videoTrack.getSettings().deviceId || null;
213
+ }
214
+ // Update video transform based on camera
215
+ if (this.isBackCamera) {
216
+ video.style.transform = "none";
217
+ video.style.webkitTransform = "none";
218
+ }
219
+ else {
220
+ video.style.transform = "scaleX(-1)";
221
+ video.style.webkitTransform = "scaleX(-1)";
222
+ }
223
+ await video.play();
224
+ }
225
+ catch (error) {
226
+ throw new Error(`Failed to flip camera: ${error}`);
227
+ }
157
228
  }
158
229
  async setOpacity(_options) {
159
- const video = document.getElementById("video");
230
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
160
231
  if (!!video && !!_options.opacity)
161
232
  video.style.setProperty("opacity", _options.opacity.toString());
162
233
  }
234
+ async isRunning() {
235
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
236
+ return { isRunning: !!video && !!video.srcObject };
237
+ }
238
+ async getAvailableDevices() {
239
+ var _a;
240
+ if (!((_a = navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.enumerateDevices)) {
241
+ throw new Error("getAvailableDevices not supported under the web platform");
242
+ }
243
+ const devices = await navigator.mediaDevices.enumerateDevices();
244
+ const videoDevices = devices.filter(device => device.kind === 'videoinput');
245
+ // Group devices by position (front/back)
246
+ const frontDevices = [];
247
+ const backDevices = [];
248
+ videoDevices.forEach((device, index) => {
249
+ const label = device.label || `Camera ${index + 1}`;
250
+ const labelLower = label.toLowerCase();
251
+ // Determine device type based on label
252
+ let deviceType = DeviceType.WIDE_ANGLE;
253
+ let baseZoomRatio = 1.0;
254
+ if (labelLower.includes('ultra') || labelLower.includes('0.5')) {
255
+ deviceType = DeviceType.ULTRA_WIDE;
256
+ baseZoomRatio = 0.5;
257
+ }
258
+ else if (labelLower.includes('telephoto') || labelLower.includes('tele') || labelLower.includes('2x') || labelLower.includes('3x')) {
259
+ deviceType = DeviceType.TELEPHOTO;
260
+ baseZoomRatio = 2.0;
261
+ }
262
+ else if (labelLower.includes('depth') || labelLower.includes('truedepth')) {
263
+ deviceType = DeviceType.TRUE_DEPTH;
264
+ baseZoomRatio = 1.0;
265
+ }
266
+ const lensInfo = {
267
+ deviceId: device.deviceId,
268
+ label,
269
+ deviceType,
270
+ focalLength: 4.25,
271
+ baseZoomRatio,
272
+ minZoom: 1.0,
273
+ maxZoom: 1.0
274
+ };
275
+ // Determine position and add to appropriate array
276
+ if (labelLower.includes('back') || labelLower.includes('rear')) {
277
+ backDevices.push(lensInfo);
278
+ }
279
+ else {
280
+ frontDevices.push(lensInfo);
281
+ }
282
+ });
283
+ const result = [];
284
+ if (frontDevices.length > 0) {
285
+ result.push({
286
+ deviceId: frontDevices[0].deviceId,
287
+ label: "Front Camera",
288
+ position: "front",
289
+ lenses: frontDevices,
290
+ isLogical: false,
291
+ minZoom: Math.min(...frontDevices.map(d => d.minZoom)),
292
+ maxZoom: Math.max(...frontDevices.map(d => d.maxZoom))
293
+ });
294
+ }
295
+ if (backDevices.length > 0) {
296
+ result.push({
297
+ deviceId: backDevices[0].deviceId,
298
+ label: "Back Camera",
299
+ position: "rear",
300
+ lenses: backDevices,
301
+ isLogical: false,
302
+ minZoom: Math.min(...backDevices.map(d => d.minZoom)),
303
+ maxZoom: Math.max(...backDevices.map(d => d.maxZoom))
304
+ });
305
+ }
306
+ return { devices: result };
307
+ }
308
+ async getZoom() {
309
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
310
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
311
+ throw new Error("camera is not running");
312
+ }
313
+ const stream = video.srcObject;
314
+ const videoTrack = stream.getVideoTracks()[0];
315
+ if (!videoTrack) {
316
+ throw new Error("no video track found");
317
+ }
318
+ const capabilities = videoTrack.getCapabilities();
319
+ const settings = videoTrack.getSettings();
320
+ if (!capabilities.zoom) {
321
+ throw new Error("zoom not supported by this device");
322
+ }
323
+ // Get current device info to determine lens type
324
+ let deviceType = DeviceType.WIDE_ANGLE;
325
+ let baseZoomRatio = 1.0;
326
+ if (this.currentDeviceId) {
327
+ const devices = await navigator.mediaDevices.enumerateDevices();
328
+ const device = devices.find(d => d.deviceId === this.currentDeviceId);
329
+ if (device) {
330
+ const labelLower = device.label.toLowerCase();
331
+ if (labelLower.includes('ultra') || labelLower.includes('0.5')) {
332
+ deviceType = DeviceType.ULTRA_WIDE;
333
+ baseZoomRatio = 0.5;
334
+ }
335
+ else if (labelLower.includes('telephoto') || labelLower.includes('tele') || labelLower.includes('2x') || labelLower.includes('3x')) {
336
+ deviceType = DeviceType.TELEPHOTO;
337
+ baseZoomRatio = 2.0;
338
+ }
339
+ else if (labelLower.includes('depth') || labelLower.includes('truedepth')) {
340
+ deviceType = DeviceType.TRUE_DEPTH;
341
+ baseZoomRatio = 1.0;
342
+ }
343
+ }
344
+ }
345
+ const currentZoom = settings.zoom || 1;
346
+ const lensInfo = {
347
+ focalLength: 4.25,
348
+ deviceType,
349
+ baseZoomRatio,
350
+ digitalZoom: currentZoom / baseZoomRatio
351
+ };
352
+ return {
353
+ min: capabilities.zoom.min || 1,
354
+ max: capabilities.zoom.max || 1,
355
+ current: currentZoom,
356
+ lens: lensInfo,
357
+ };
358
+ }
359
+ async setZoom(options) {
360
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
361
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
362
+ throw new Error("camera is not running");
363
+ }
364
+ const stream = video.srcObject;
365
+ const videoTrack = stream.getVideoTracks()[0];
366
+ if (!videoTrack) {
367
+ throw new Error("no video track found");
368
+ }
369
+ const capabilities = videoTrack.getCapabilities();
370
+ if (!capabilities.zoom) {
371
+ throw new Error("zoom not supported by this device");
372
+ }
373
+ const zoomLevel = Math.max(capabilities.zoom.min || 1, Math.min(capabilities.zoom.max || 1, options.level));
374
+ try {
375
+ await videoTrack.applyConstraints({
376
+ advanced: [{ zoom: zoomLevel }]
377
+ });
378
+ }
379
+ catch (error) {
380
+ throw new Error(`Failed to set zoom: ${error}`);
381
+ }
382
+ }
383
+ async getFlashMode() {
384
+ throw new Error("getFlashMode not supported under the web platform");
385
+ }
386
+ async getDeviceId() {
387
+ return { deviceId: this.currentDeviceId || "" };
388
+ }
389
+ async setDeviceId(options) {
390
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
391
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
392
+ throw new Error("camera is not running");
393
+ }
394
+ // Stop current stream
395
+ this.stopStream(video.srcObject);
396
+ // Update current device ID
397
+ this.currentDeviceId = options.deviceId;
398
+ // Get new constraints with specific device ID
399
+ const constraints = {
400
+ video: {
401
+ deviceId: { exact: options.deviceId },
402
+ width: { ideal: video.videoWidth || 640 },
403
+ height: { ideal: video.videoHeight || 480 },
404
+ },
405
+ };
406
+ try {
407
+ // Try to determine camera position from device
408
+ const devices = await navigator.mediaDevices.enumerateDevices();
409
+ const device = devices.find(d => d.deviceId === options.deviceId);
410
+ this.isBackCamera = (device === null || device === void 0 ? void 0 : device.label.toLowerCase().includes('back')) || (device === null || device === void 0 ? void 0 : device.label.toLowerCase().includes('rear')) || false;
411
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
412
+ video.srcObject = stream;
413
+ // Update video transform based on camera
414
+ if (this.isBackCamera) {
415
+ video.style.transform = "none";
416
+ video.style.webkitTransform = "none";
417
+ }
418
+ else {
419
+ video.style.transform = "scaleX(-1)";
420
+ video.style.webkitTransform = "scaleX(-1)";
421
+ }
422
+ await video.play();
423
+ }
424
+ catch (error) {
425
+ throw new Error(`Failed to swap to device ${options.deviceId}: ${error}`);
426
+ }
427
+ }
428
+ async getAspectRatio() {
429
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
430
+ if (!video) {
431
+ throw new Error("camera is not running");
432
+ }
433
+ const width = video.offsetWidth;
434
+ const height = video.offsetHeight;
435
+ if (width && height) {
436
+ const ratio = width / height;
437
+ // Check for portrait camera ratios: 4:3 -> 3:4, 16:9 -> 9:16
438
+ if (Math.abs(ratio - (3 / 4)) < 0.01) {
439
+ return { aspectRatio: '4:3' };
440
+ }
441
+ if (Math.abs(ratio - (9 / 16)) < 0.01) {
442
+ return { aspectRatio: '16:9' };
443
+ }
444
+ }
445
+ // Default to 4:3 if no specific aspect ratio is matched
446
+ return { aspectRatio: '4:3' };
447
+ }
448
+ async setAspectRatio(options) {
449
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
450
+ if (!video) {
451
+ throw new Error("camera is not running");
452
+ }
453
+ if (options.aspectRatio) {
454
+ const [widthRatio, heightRatio] = options.aspectRatio.split(':').map(Number);
455
+ // For camera, use portrait orientation: 4:3 becomes 3:4, 16:9 becomes 9:16
456
+ const ratio = heightRatio / widthRatio;
457
+ // Get current position and size
458
+ const rect = video.getBoundingClientRect();
459
+ const currentWidth = rect.width;
460
+ const currentHeight = rect.height;
461
+ const currentRatio = currentWidth / currentHeight;
462
+ let newWidth;
463
+ let newHeight;
464
+ if (currentRatio > ratio) {
465
+ // Width is larger, fit by height and center horizontally
466
+ newWidth = currentHeight * ratio;
467
+ newHeight = currentHeight;
468
+ }
469
+ else {
470
+ // Height is larger, fit by width and center vertically
471
+ newWidth = currentWidth;
472
+ newHeight = currentWidth / ratio;
473
+ }
474
+ // Calculate position
475
+ let x, y;
476
+ if (options.x !== undefined && options.y !== undefined) {
477
+ // Use provided coordinates, ensuring they stay within screen boundaries
478
+ x = Math.max(0, Math.min(options.x, window.innerWidth - newWidth));
479
+ y = Math.max(0, Math.min(options.y, window.innerHeight - newHeight));
480
+ }
481
+ else {
482
+ // Auto-center the view
483
+ x = (window.innerWidth - newWidth) / 2;
484
+ y = (window.innerHeight - newHeight) / 2;
485
+ }
486
+ video.style.width = `${newWidth}px`;
487
+ video.style.height = `${newHeight}px`;
488
+ video.style.left = `${x}px`;
489
+ video.style.top = `${y}px`;
490
+ video.style.position = 'absolute';
491
+ return {
492
+ width: Math.round(newWidth),
493
+ height: Math.round(newHeight),
494
+ x: Math.round(x),
495
+ y: Math.round(y)
496
+ };
497
+ }
498
+ else {
499
+ video.style.objectFit = 'cover';
500
+ const rect = video.getBoundingClientRect();
501
+ return {
502
+ width: Math.round(rect.width),
503
+ height: Math.round(rect.height),
504
+ x: Math.round(rect.left),
505
+ y: Math.round(rect.top)
506
+ };
507
+ }
508
+ }
509
+ createGridOverlay(gridMode) {
510
+ const overlay = document.createElement("div");
511
+ overlay.style.position = "absolute";
512
+ overlay.style.top = "0";
513
+ overlay.style.left = "0";
514
+ overlay.style.width = "100%";
515
+ overlay.style.height = "100%";
516
+ overlay.style.pointerEvents = "none";
517
+ overlay.style.zIndex = "10";
518
+ const divisions = gridMode === "3x3" ? 3 : 4;
519
+ // Create SVG for grid lines
520
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
521
+ svg.style.width = "100%";
522
+ svg.style.height = "100%";
523
+ svg.style.position = "absolute";
524
+ svg.style.top = "0";
525
+ svg.style.left = "0";
526
+ // Create grid lines
527
+ for (let i = 1; i < divisions; i++) {
528
+ // Vertical lines
529
+ const verticalLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
530
+ verticalLine.setAttribute("x1", `${(i / divisions) * 100}%`);
531
+ verticalLine.setAttribute("y1", "0%");
532
+ verticalLine.setAttribute("x2", `${(i / divisions) * 100}%`);
533
+ verticalLine.setAttribute("y2", "100%");
534
+ verticalLine.setAttribute("stroke", "rgba(255, 255, 255, 0.5)");
535
+ verticalLine.setAttribute("stroke-width", "1");
536
+ svg.appendChild(verticalLine);
537
+ // Horizontal lines
538
+ const horizontalLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
539
+ horizontalLine.setAttribute("x1", "0%");
540
+ horizontalLine.setAttribute("y1", `${(i / divisions) * 100}%`);
541
+ horizontalLine.setAttribute("x2", "100%");
542
+ horizontalLine.setAttribute("y2", `${(i / divisions) * 100}%`);
543
+ horizontalLine.setAttribute("stroke", "rgba(255, 255, 255, 0.5)");
544
+ horizontalLine.setAttribute("stroke-width", "1");
545
+ svg.appendChild(horizontalLine);
546
+ }
547
+ overlay.appendChild(svg);
548
+ return overlay;
549
+ }
550
+ async setGridMode(options) {
551
+ // Web implementation of grid mode would need to be implemented
552
+ // For now, just resolve as a no-op
553
+ console.warn(`Grid mode '${options.gridMode}' is not yet implemented for web platform`);
554
+ }
555
+ async getGridMode() {
556
+ // Web implementation - default to none
557
+ return { gridMode: 'none' };
558
+ }
559
+ async getPreviewSize() {
560
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
561
+ if (!video) {
562
+ throw new Error("camera is not running");
563
+ }
564
+ return {
565
+ x: video.offsetLeft,
566
+ y: video.offsetTop,
567
+ width: video.width,
568
+ height: video.height
569
+ };
570
+ }
571
+ async setPreviewSize(options) {
572
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
573
+ if (!video) {
574
+ throw new Error("camera is not running");
575
+ }
576
+ video.style.left = `${options.x}px`;
577
+ video.style.top = `${options.y}px`;
578
+ video.width = options.width;
579
+ video.height = options.height;
580
+ return {
581
+ width: options.width,
582
+ height: options.height,
583
+ x: options.x,
584
+ y: options.y
585
+ };
586
+ }
163
587
  }
164
588
  //# sourceMappingURL=web.js.map