@capgo/camera-preview 7.4.0-beta.1 → 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 (31) hide show
  1. package/README.md +195 -31
  2. package/android/.gradle/8.14.2/checksums/checksums.lock +0 -0
  3. package/android/.gradle/8.14.2/checksums/md5-checksums.bin +0 -0
  4. package/android/.gradle/8.14.2/checksums/sha1-checksums.bin +0 -0
  5. package/android/.gradle/8.14.2/executionHistory/executionHistory.bin +0 -0
  6. package/android/.gradle/8.14.2/executionHistory/executionHistory.lock +0 -0
  7. package/android/.gradle/8.14.2/fileHashes/fileHashes.bin +0 -0
  8. package/android/.gradle/8.14.2/fileHashes/fileHashes.lock +0 -0
  9. package/android/.gradle/8.14.2/fileHashes/resourceHashesCache.bin +0 -0
  10. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  11. package/android/.gradle/file-system.probe +0 -0
  12. package/android/build.gradle +3 -1
  13. package/android/src/main/AndroidManifest.xml +5 -3
  14. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java +282 -45
  15. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraXView.java +902 -102
  16. package/android/src/main/java/com/ahm/capacitor/camera/preview/GridOverlayView.java +82 -0
  17. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraSessionConfiguration.java +19 -5
  18. package/dist/docs.json +235 -6
  19. package/dist/esm/definitions.d.ts +119 -3
  20. package/dist/esm/definitions.js.map +1 -1
  21. package/dist/esm/web.d.ts +47 -3
  22. package/dist/esm/web.js +262 -78
  23. package/dist/esm/web.js.map +1 -1
  24. package/dist/plugin.cjs.js +258 -78
  25. package/dist/plugin.cjs.js.map +1 -1
  26. package/dist/plugin.js +258 -78
  27. package/dist/plugin.js.map +1 -1
  28. package/ios/Sources/CapgoCameraPreview/CameraController.swift +245 -28
  29. package/ios/Sources/CapgoCameraPreview/GridOverlayView.swift +65 -0
  30. package/ios/Sources/CapgoCameraPreview/Plugin.swift +657 -90
  31. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AAIA,MAAM,CAAN,IAAY,UAQX;AARD,WAAY,UAAU;IACpB,sCAAwB,CAAA;IACxB,sCAAwB,CAAA;IACxB,qCAAuB,CAAA;IACvB,sCAAwB,CAAA;IACxB,2BAAa,CAAA;IACb,oCAAsB,CAAA;IACtB,+BAAiB,CAAA;AACnB,CAAC,EARW,UAAU,KAAV,UAAU,QAQrB","sourcesContent":["export type CameraPosition = \"rear\" | \"front\";\n\nexport type FlashMode = CameraPreviewFlashMode;\n\nexport enum DeviceType {\n ULTRA_WIDE = \"ultraWide\",\n WIDE_ANGLE = \"wideAngle\",\n TELEPHOTO = \"telephoto\",\n TRUE_DEPTH = \"trueDepth\",\n DUAL = \"dual\",\n DUAL_WIDE = \"dualWide\",\n TRIPLE = \"triple\",\n}\n\n/**\n * Represents a single camera lens on a device. A {@link CameraDevice} can have multiple lenses.\n */\nexport interface CameraLens {\n /** A human-readable name for the lens, e.g., \"Ultra-Wide\". */\n label: string;\n /** The type of the camera lens. */\n deviceType: DeviceType;\n /** The focal length of the lens in millimeters. */\n focalLength: number;\n /** The base zoom factor for this lens (e.g., 0.5 for ultra-wide, 1.0 for wide). */\n baseZoomRatio: number;\n /** The minimum zoom factor supported by this specific lens. */\n minZoom: number;\n /** The maximum zoom factor supported by this specific lens. */\n maxZoom: number;\n}\n\n/**\n * Represents a physical camera on the device (e.g., the front-facing camera).\n */\nexport interface CameraDevice {\n /** A unique identifier for the camera device. */\n deviceId: string;\n /** A human-readable name for the camera device. */\n label: string;\n /** The physical position of the camera on the device. */\n position: CameraPosition;\n /** A list of all available lenses for this camera device. */\n lenses: CameraLens[];\n /** The overall minimum zoom factor available across all lenses on this device. */\n minZoom: number;\n /** The overall maximum zoom factor available across all lenses on this device. */\n maxZoom: number;\n /** Identifies whether the device is a logical camera (composed of multiple physical lenses). */\n isLogical: boolean;\n}\n\n/**\n * Represents the detailed information of the currently active lens.\n */\nexport interface LensInfo {\n /** The focal length of the active lens in millimeters. */\n focalLength: number;\n /** The device type of the active lens. */\n deviceType: DeviceType;\n /** The base zoom ratio of the active lens (e.g., 0.5x, 1.0x). */\n baseZoomRatio: number;\n /** The current digital zoom factor applied on top of the base zoom. */\n digitalZoom: number;\n}\n\n/**\n * Defines the configuration options for starting the camera preview.\n */\nexport interface CameraPreviewOptions {\n /** \n * The parent element to attach the video preview to.\n * @platform web\n */\n parent?: string;\n /** \n * A CSS class name to add to the preview element.\n * @platform web\n */\n className?: string;\n /** \n * The width of the preview in pixels. Defaults to the screen width.\n * @platform android, ios, web\n */\n width?: number;\n /** \n * The height of the preview in pixels. Defaults to the screen height.\n * @platform android, ios, web\n */\n height?: number;\n /** \n * The horizontal origin of the preview, in pixels.\n * @platform android, ios\n */\n x?: number;\n /** \n * The vertical origin of the preview, in pixels.\n * @platform android, ios\n */\n y?: number;\n /** \n * Adjusts the y-position to account for safe areas (e.g., notches).\n * @platform ios\n * @default false\n */\n includeSafeAreaInsets?: boolean;\n /** \n * If true, places the preview behind the webview.\n * @platform android\n * @default true\n */\n toBack?: boolean;\n /** \n * Bottom padding for the preview, in pixels.\n * @platform android, ios\n */\n paddingBottom?: number;\n /** \n * Whether to rotate the preview when the device orientation changes.\n * @platform ios\n * @default true\n */\n rotateWhenOrientationChanged?: boolean;\n /** \n * The camera to use.\n * @default \"rear\"\n */\n position?: CameraPosition | string;\n /** \n * If true, saves the captured image to a file and returns the file path.\n * If false, returns a base64 encoded string.\n * @default false\n */\n storeToFile?: boolean;\n /** \n * If true, prevents the plugin from rotating the image based on EXIF data.\n * @platform android\n * @default false\n */\n disableExifHeaderStripping?: boolean;\n /** \n * If true, enables high-resolution image capture.\n * @platform ios\n * @default false\n */\n enableHighResolution?: boolean;\n /** \n * If true, disables the audio stream, preventing audio permission requests.\n * @default false\n */\n disableAudio?: boolean;\n /** \n * If true, locks the device orientation while the camera is active.\n * @platform android\n * @default false\n */\n lockAndroidOrientation?: boolean;\n /** \n * If true, allows the camera preview's opacity to be changed.\n * @platform android, web\n * @default false\n */\n enableOpacity?: boolean;\n /** \n * If true, enables pinch-to-zoom functionality on the preview.\n * @platform android\n * @default false\n */\n enableZoom?: boolean;\n /** \n * If true, uses the video-optimized preset for the camera session.\n * @platform ios\n * @default false\n */\n enableVideoMode?: boolean;\n /** \n * The `deviceId` of the camera to use. If provided, `position` is ignored.\n * @platform ios\n */\n deviceId?: string;\n}\n\n/**\n * Defines the options for capturing a picture.\n */\nexport interface CameraPreviewPictureOptions {\n /** The desired height of the picture in pixels. If not provided, the device default is used. */\n height?: number;\n /** The desired width of the picture in pixels. If not provided, the device default is used. */\n width?: number;\n /** \n * The quality of the captured image, from 0 to 100.\n * Does not apply to `png` format.\n * @default 85\n */\n quality?: number;\n /** \n * The format of the captured image.\n * @default \"jpeg\"\n */\n format?: PictureFormat;\n}\n\nexport type PictureFormat = \"jpeg\" | \"png\";\n\n/** Defines a standard picture size with width and height. */\nexport interface PictureSize {\n /** The width of the picture in pixels. */\n width: number;\n /** The height of the picture in pixels. */\n height: number;\n}\n\n/** Represents the supported picture sizes for a camera facing a certain direction. */\nexport interface SupportedPictureSizes {\n /** The camera direction (\"front\" or \"rear\"). */\n facing: string;\n /** A list of supported picture sizes for this camera. */\n supportedPictureSizes: PictureSize[];\n}\n\n/**\n * Defines the options for capturing a sample frame from the camera preview.\n */\nexport interface CameraSampleOptions {\n /** \n * The quality of the captured sample, from 0 to 100.\n * @default 85\n */\n quality?: number;\n}\n\n/**\n * The available flash modes for the camera.\n * 'torch' is a continuous light mode.\n */\nexport type CameraPreviewFlashMode =\n | \"off\"\n | \"on\"\n | \"auto\"\n | \"torch\";\n\n/**\n * Defines the options for setting the camera preview's opacity.\n */\nexport interface CameraOpacityOptions {\n /** \n * The opacity percentage, from 0.0 (fully transparent) to 1.0 (fully opaque).\n * @default 1.0\n */\n opacity?: number;\n}\n\n/**\n * The main interface for the CameraPreview plugin.\n */\nexport interface CameraPreviewPlugin {\n /**\n * Starts the camera preview.\n *\n * @param {CameraPreviewOptions} options - The configuration for the camera preview.\n * @returns {Promise<void>} A promise that resolves when the camera preview is started.\n * @since 0.0.1\n */\n start(options: CameraPreviewOptions): Promise<void>;\n\n /**\n * Stops the camera preview.\n *\n * @returns {Promise<void>} A promise that resolves when the camera preview is stopped.\n * @since 0.0.1\n */\n stop(): Promise<void>;\n\n /**\n * Captures a picture from the camera.\n *\n * @param {CameraPreviewPictureOptions} options - The options for capturing the picture.\n * @returns {Promise<{ value: string }>} A promise that resolves with the captured image data.\n * The `value` is a base64 encoded string unless `storeToFile` is true, in which case it's a file path.\n * @since 0.0.1\n */\n capture(options: CameraPreviewPictureOptions): Promise<{ value: string }>;\n\n /**\n * Captures a single frame from the camera preview stream.\n *\n * @param {CameraSampleOptions} options - The options for capturing the sample.\n * @returns {Promise<{ value: string }>} A promise that resolves with the sample image as a base64 encoded string.\n * @since 0.0.1\n */\n captureSample(options: CameraSampleOptions): Promise<{ value: string }>;\n\n /**\n * Gets the flash modes supported by the active camera.\n *\n * @returns {Promise<{ result: CameraPreviewFlashMode[] }>} A promise that resolves with an array of supported flash modes.\n * @since 0.0.1\n */\n getSupportedFlashModes(): Promise<{\n result: CameraPreviewFlashMode[];\n }>;\n\n /**\n * Gets the horizontal field of view (FoV) for the active camera.\n * Note: This can be an estimate on some devices.\n *\n * @returns {Promise<{ result: number }>} A promise that resolves with the horizontal field of view in degrees.\n * @since 0.0.1\n */\n getHorizontalFov(): Promise<{\n result: number;\n }>;\n\n /**\n * Gets the supported picture sizes for all cameras.\n *\n * @returns {Promise<{ supportedPictureSizes: SupportedPictureSizes[] }>} A promise that resolves with the list of supported sizes.\n * @since 7.4.0\n */\n getSupportedPictureSizes(): Promise<{\n supportedPictureSizes: SupportedPictureSizes[];\n }>;\n\n /**\n * Sets the flash mode for the active camera.\n *\n * @param {{ flashMode: CameraPreviewFlashMode | string }} options - The desired flash mode.\n * @returns {Promise<void>} A promise that resolves when the flash mode is set.\n * @since 0.0.1\n */\n setFlashMode(options: {\n flashMode: CameraPreviewFlashMode | string;\n }): Promise<void>;\n\n /**\n * Toggles between the front and rear cameras.\n *\n * @returns {Promise<void>} A promise that resolves when the camera is flipped.\n * @since 0.0.1\n */\n flip(): Promise<void>;\n\n /**\n * Sets the opacity of the camera preview.\n *\n * @param {CameraOpacityOptions} options - The opacity options.\n * @returns {Promise<void>} A promise that resolves when the opacity is set.\n * @since 0.0.1\n */\n setOpacity(options: CameraOpacityOptions): Promise<void>;\n\n /**\n * Stops an ongoing video recording.\n *\n * @returns {Promise<{ videoFilePath: string }>} A promise that resolves with the path to the recorded video file.\n * @since 0.0.1\n */\n stopRecordVideo(): Promise<{ videoFilePath: string }>;\n\n /**\n * Starts recording a video.\n *\n * @param {CameraPreviewOptions} options - The options for video recording.\n * @returns {Promise<void>} A promise that resolves when video recording starts.\n * @since 0.0.1\n */\n startRecordVideo(options: CameraPreviewOptions): Promise<void>;\n\n /**\n * Checks if the camera preview is currently running.\n *\n * @returns {Promise<{ isRunning: boolean }>} A promise that resolves with the running state.\n * @since 7.4.0\n */\n isRunning(): Promise<{ isRunning: boolean }>;\n\n /**\n * Gets all available camera devices.\n *\n * @returns {Promise<{ devices: CameraDevice[] }>} A promise that resolves with the list of available camera devices.\n * @since 7.4.0\n */\n getAvailableDevices(): Promise<{ devices: CameraDevice[] }>;\n\n /**\n * Gets the current zoom state, including min/max and current lens info.\n *\n * @returns {Promise<{ min: number; max: number; current: number; lens: LensInfo }>} A promise that resolves with the zoom state.\n * @since 7.4.0\n */\n getZoom(): Promise<{ \n min: number; \n max: number; \n current: number;\n lens: LensInfo;\n }>;\n\n /**\n * Sets the camera's zoom level.\n *\n * @param {{ level: number; ramp?: boolean }} options - The desired zoom level. `ramp` is currently unused.\n * @returns {Promise<void>} A promise that resolves when the zoom level is set.\n * @since 7.4.0\n */\n setZoom(options: { level: number; ramp?: boolean }): Promise<void>;\n\n /**\n * Gets the current flash mode.\n *\n * @returns {Promise<{ flashMode: FlashMode }>} A promise that resolves with the current flash mode.\n * @since 7.4.0\n */\n getFlashMode(): Promise<{ flashMode: FlashMode }>;\n\n /**\n * Removes all registered listeners.\n *\n * @since 7.4.0\n */\n removeAllListeners(): Promise<void>;\n\n /**\n * Switches the active camera to the one with the specified `deviceId`.\n *\n * @param {{ deviceId: string }} options - The ID of the device to switch to.\n * @returns {Promise<void>} A promise that resolves when the camera is switched.\n * @since 7.4.0\n */\n setDeviceId(options: { deviceId: string }): Promise<void>;\n\n /**\n * Gets the ID of the currently active camera device.\n *\n * @returns {Promise<{ deviceId: string }>} A promise that resolves with the current device ID.\n * @since 7.4.0\n */\n getDeviceId(): Promise<{ deviceId: string }>;\n}\n"]}
1
+ {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AAMA,MAAM,CAAN,IAAY,UAQX;AARD,WAAY,UAAU;IACpB,sCAAwB,CAAA;IACxB,sCAAwB,CAAA;IACxB,qCAAuB,CAAA;IACvB,sCAAwB,CAAA;IACxB,2BAAa,CAAA;IACb,oCAAsB,CAAA;IACtB,+BAAiB,CAAA;AACnB,CAAC,EARW,UAAU,KAAV,UAAU,QAQrB","sourcesContent":["export type CameraPosition = \"rear\" | \"front\";\n\nexport type FlashMode = CameraPreviewFlashMode;\n\nexport type GridMode = \"none\" | \"3x3\" | \"4x4\";\n\nexport enum DeviceType {\n ULTRA_WIDE = \"ultraWide\",\n WIDE_ANGLE = \"wideAngle\",\n TELEPHOTO = \"telephoto\",\n TRUE_DEPTH = \"trueDepth\",\n DUAL = \"dual\",\n DUAL_WIDE = \"dualWide\",\n TRIPLE = \"triple\",\n}\n\n/**\n * Represents a single camera lens on a device. A {@link CameraDevice} can have multiple lenses.\n */\nexport interface CameraLens {\n /** A human-readable name for the lens, e.g., \"Ultra-Wide\". */\n label: string;\n /** The type of the camera lens. */\n deviceType: DeviceType;\n /** The focal length of the lens in millimeters. */\n focalLength: number;\n /** The base zoom factor for this lens (e.g., 0.5 for ultra-wide, 1.0 for wide). */\n baseZoomRatio: number;\n /** The minimum zoom factor supported by this specific lens. */\n minZoom: number;\n /** The maximum zoom factor supported by this specific lens. */\n maxZoom: number;\n}\n\n/**\n * Represents a physical camera on the device (e.g., the front-facing camera).\n */\nexport interface CameraDevice {\n /** A unique identifier for the camera device. */\n deviceId: string;\n /** A human-readable name for the camera device. */\n label: string;\n /** The physical position of the camera on the device. */\n position: CameraPosition;\n /** A list of all available lenses for this camera device. */\n lenses: CameraLens[];\n /** The overall minimum zoom factor available across all lenses on this device. */\n minZoom: number;\n /** The overall maximum zoom factor available across all lenses on this device. */\n maxZoom: number;\n /** Identifies whether the device is a logical camera (composed of multiple physical lenses). */\n isLogical: boolean;\n}\n\n/**\n * Represents the detailed information of the currently active lens.\n */\nexport interface LensInfo {\n /** The focal length of the active lens in millimeters. */\n focalLength: number;\n /** The device type of the active lens. */\n deviceType: DeviceType;\n /** The base zoom ratio of the active lens (e.g., 0.5x, 1.0x). */\n baseZoomRatio: number;\n /** The current digital zoom factor applied on top of the base zoom. */\n digitalZoom: number;\n}\n\n/**\n * Defines the configuration options for starting the camera preview.\n */\nexport interface CameraPreviewOptions {\n /**\n * The parent element to attach the video preview to.\n * @platform web\n */\n parent?: string;\n /**\n * A CSS class name to add to the preview element.\n * @platform web\n */\n className?: string;\n /**\n * The width of the preview in pixels. Defaults to the screen width.\n * @platform android, ios, web\n */\n width?: number;\n /**\n * The height of the preview in pixels. Defaults to the screen height.\n * @platform android, ios, web\n */\n height?: number;\n /**\n * The horizontal origin of the preview, in pixels.\n * @platform android, ios\n */\n x?: number;\n /**\n * The vertical origin of the preview, in pixels.\n * @platform android, ios\n */\n y?: number;\n /**\n * The aspect ratio of the camera preview, '4:3' or '16:9' or 'fill'.\n * Cannot be set if width or height is provided, otherwise the call will be rejected.\n * Use setPreviewSize to adjust size after starting.\n *\n * @since 2.0.0\n */\n aspectRatio?: '4:3' | '16:9' | 'fill';\n /**\n * The grid overlay to display on the camera preview.\n * @default \"none\"\n * @since 2.1.0\n */\n gridMode?: GridMode;\n /**\n * Adjusts the y-position to account for safe areas (e.g., notches).\n * @platform ios\n * @default false\n */\n includeSafeAreaInsets?: boolean;\n /**\n * If true, places the preview behind the webview.\n * @platform android\n * @default true\n */\n toBack?: boolean;\n /**\n * Bottom padding for the preview, in pixels.\n * @platform android, ios\n */\n paddingBottom?: number;\n /**\n * Whether to rotate the preview when the device orientation changes.\n * @platform ios\n * @default true\n */\n rotateWhenOrientationChanged?: boolean;\n /**\n * The camera to use.\n * @default \"rear\"\n */\n position?: CameraPosition | string;\n /**\n * If true, saves the captured image to a file and returns the file path.\n * If false, returns a base64 encoded string.\n * @default false\n */\n storeToFile?: boolean;\n /**\n * If true, prevents the plugin from rotating the image based on EXIF data.\n * @platform android\n * @default false\n */\n disableExifHeaderStripping?: boolean;\n /**\n * If true, enables high-resolution image capture.\n * @platform ios\n * @default false\n */\n enableHighResolution?: boolean;\n /**\n * If true, disables the audio stream, preventing audio permission requests.\n * @default true\n */\n disableAudio?: boolean;\n /**\n * If true, locks the device orientation while the camera is active.\n * @platform android\n * @default false\n */\n lockAndroidOrientation?: boolean;\n /**\n * If true, allows the camera preview's opacity to be changed.\n * @platform android, web\n * @default false\n */\n enableOpacity?: boolean;\n /**\n * If true, enables pinch-to-zoom functionality on the preview.\n * @platform android\n * @default false\n */\n enableZoom?: boolean;\n /**\n * If true, uses the video-optimized preset for the camera session.\n * @platform ios\n * @default false\n */\n enableVideoMode?: boolean;\n /**\n * The `deviceId` of the camera to use. If provided, `position` is ignored.\n * @platform ios\n */\n deviceId?: string;\n}\n\n/**\n * Defines the options for capturing a picture.\n */\nexport interface CameraPreviewPictureOptions {\n /** The desired height of the picture in pixels. If not provided, the device default is used. */\n height?: number;\n /** The desired width of the picture in pixels. If not provided, the device default is used. */\n width?: number;\n /**\n * The quality of the captured image, from 0 to 100.\n * Does not apply to `png` format.\n * @default 85\n */\n quality?: number;\n /**\n * The format of the captured image.\n * @default \"jpeg\"\n */\n format?: PictureFormat;\n /**\n * If true, the captured image will be saved to the user's gallery.\n * @default false\n * @since 7.5.0\n */\n saveToGallery?: boolean;\n /**\n * If true, the plugin will attempt to add GPS location data to the image's EXIF metadata.\n * This may prompt the user for location permissions.\n * @default false\n * @since 7.6.0\n */\n withExifLocation?: boolean;\n}\n\n/** Represents EXIF data extracted from an image. */\nexport interface ExifData {\n [key: string]: any;\n}\n\nexport type PictureFormat = \"jpeg\" | \"png\";\n\n/** Defines a standard picture size with width and height. */\nexport interface PictureSize {\n /** The width of the picture in pixels. */\n width: number;\n /** The height of the picture in pixels. */\n height: number;\n}\n\n/** Represents the supported picture sizes for a camera facing a certain direction. */\nexport interface SupportedPictureSizes {\n /** The camera direction (\"front\" or \"rear\"). */\n facing: string;\n /** A list of supported picture sizes for this camera. */\n supportedPictureSizes: PictureSize[];\n}\n\n/**\n * Defines the options for capturing a sample frame from the camera preview.\n */\nexport interface CameraSampleOptions {\n /**\n * The quality of the captured sample, from 0 to 100.\n * @default 85\n */\n quality?: number;\n}\n\n/**\n * The available flash modes for the camera.\n * 'torch' is a continuous light mode.\n */\nexport type CameraPreviewFlashMode =\n | \"off\"\n | \"on\"\n | \"auto\"\n | \"torch\";\n\n/**\n * Defines the options for setting the camera preview's opacity.\n */\nexport interface CameraOpacityOptions {\n /**\n * The opacity percentage, from 0.0 (fully transparent) to 1.0 (fully opaque).\n * @default 1.0\n */\n opacity?: number;\n}\n\n/**\n * The main interface for the CameraPreview plugin.\n */\nexport interface CameraPreviewPlugin {\n /**\n * Starts the camera preview.\n *\n * @param {CameraPreviewOptions} options - The configuration for the camera preview.\n * @returns {Promise<{ width: number; height: number; x: number; y: number }>} A promise that resolves with the preview dimensions.\n * @since 0.0.1\n */\n start(options: CameraPreviewOptions): Promise<{\n /** The width of the preview in pixels. */\n width: number;\n /** The height of the preview in pixels. */\n height: number;\n /** The horizontal origin of the preview, in pixels. */\n x: number;\n /** The vertical origin of the preview, in pixels. */\n y: number;\n }>;\n\n /**\n * Stops the camera preview.\n *\n * @returns {Promise<void>} A promise that resolves when the camera preview is stopped.\n * @since 0.0.1\n */\n stop(): Promise<void>;\n\n /**\n * Captures a picture from the camera.\n *\n * @param {CameraPreviewPictureOptions} options - The options for capturing the picture.\n * @returns {Promise<{ value: string }>} A promise that resolves with the captured image data.\n * The `value` is a base64 encoded string unless `storeToFile` is true, in which case it's a file path.\n * @since 0.0.1\n */\n capture(\n options: CameraPreviewPictureOptions\n ): Promise<{ value: string; exif: ExifData }>;\n\n /**\n * Captures a single frame from the camera preview stream.\n *\n * @param {CameraSampleOptions} options - The options for capturing the sample.\n * @returns {Promise<{ value: string }>} A promise that resolves with the sample image as a base64 encoded string.\n * @since 0.0.1\n */\n captureSample(options: CameraSampleOptions): Promise<{ value: string }>;\n\n /**\n * Gets the flash modes supported by the active camera.\n *\n * @returns {Promise<{ result: CameraPreviewFlashMode[] }>} A promise that resolves with an array of supported flash modes.\n * @since 0.0.1\n */\n getSupportedFlashModes(): Promise<{\n result: CameraPreviewFlashMode[];\n }>;\n\n /**\n * Set the aspect ratio of the camera preview.\n *\n * @param {{ aspectRatio: '4:3' | '16:9'; x?: number; y?: number }} options - The desired aspect ratio and optional position.\n * - aspectRatio: The desired aspect ratio ('4:3' or '16:9')\n * - x: Optional x coordinate for positioning. If not provided, view will be auto-centered horizontally.\n * - y: Optional y coordinate for positioning. If not provided, view will be auto-centered vertically.\n * @returns {Promise<{ width: number; height: number; x: number; y: number }>} A promise that resolves with the actual preview dimensions and position.\n * @since 7.4.0\n */\n setAspectRatio(options: { aspectRatio: '4:3' | '16:9'; x?: number; y?: number }): Promise<{\n width: number;\n height: number;\n x: number;\n y: number;\n }>;\n\n /**\n * Gets the current aspect ratio of the camera preview.\n *\n * @returns {Promise<{ aspectRatio: '4:3' | '16:9' }>} A promise that resolves with the current aspect ratio.\n * @since 7.4.0\n */\n getAspectRatio(): Promise<{ aspectRatio: '4:3' | '16:9' }>;\n\n /**\n * Sets the grid mode of the camera preview overlay.\n *\n * @param {{ gridMode: GridMode }} options - The desired grid mode ('none', '3x3', or '4x4').\n * @returns {Promise<void>} A promise that resolves when the grid mode is set.\n * @since 8.0.0\n */\n setGridMode(options: { gridMode: GridMode }): Promise<void>;\n\n /**\n * Gets the current grid mode of the camera preview overlay.\n *\n * @returns {Promise<{ gridMode: GridMode }>} A promise that resolves with the current grid mode.\n * @since 8.0.0\n */\n getGridMode(): Promise<{ gridMode: GridMode }>;\n\n /**\n * Gets the horizontal field of view (FoV) for the active camera.\n * Note: This can be an estimate on some devices.\n *\n * @returns {Promise<{ result: number }>} A promise that resolves with the horizontal field of view in degrees.\n * @since 0.0.1\n */\n getHorizontalFov(): Promise<{\n result: number;\n }>;\n\n /**\n * Gets the supported picture sizes for all cameras.\n *\n * @returns {Promise<{ supportedPictureSizes: SupportedPictureSizes[] }>} A promise that resolves with the list of supported sizes.\n * @since 7.4.0\n */\n getSupportedPictureSizes(): Promise<{\n supportedPictureSizes: SupportedPictureSizes[];\n }>;\n\n /**\n * Sets the flash mode for the active camera.\n *\n * @param {{ flashMode: CameraPreviewFlashMode | string }} options - The desired flash mode.\n * @returns {Promise<void>} A promise that resolves when the flash mode is set.\n * @since 0.0.1\n */\n setFlashMode(options: {\n flashMode: CameraPreviewFlashMode | string;\n }): Promise<void>;\n\n /**\n * Toggles between the front and rear cameras.\n *\n * @returns {Promise<void>} A promise that resolves when the camera is flipped.\n * @since 0.0.1\n */\n flip(): Promise<void>;\n\n /**\n * Sets the opacity of the camera preview.\n *\n * @param {CameraOpacityOptions} options - The opacity options.\n * @returns {Promise<void>} A promise that resolves when the opacity is set.\n * @since 0.0.1\n */\n setOpacity(options: CameraOpacityOptions): Promise<void>;\n\n /**\n * Stops an ongoing video recording.\n *\n * @returns {Promise<{ videoFilePath: string }>} A promise that resolves with the path to the recorded video file.\n * @since 0.0.1\n */\n stopRecordVideo(): Promise<{ videoFilePath: string }>;\n\n /**\n * Starts recording a video.\n *\n * @param {CameraPreviewOptions} options - The options for video recording.\n * @returns {Promise<void>} A promise that resolves when video recording starts.\n * @since 0.0.1\n */\n startRecordVideo(options: CameraPreviewOptions): Promise<void>;\n\n /**\n * Checks if the camera preview is currently running.\n *\n * @returns {Promise<{ isRunning: boolean }>} A promise that resolves with the running state.\n * @since 7.4.0\n */\n isRunning(): Promise<{ isRunning: boolean }>;\n\n /**\n * Gets all available camera devices.\n *\n * @returns {Promise<{ devices: CameraDevice[] }>} A promise that resolves with the list of available camera devices.\n * @since 7.4.0\n */\n getAvailableDevices(): Promise<{ devices: CameraDevice[] }>;\n\n /**\n * Gets the current zoom state, including min/max and current lens info.\n *\n * @returns {Promise<{ min: number; max: number; current: number; lens: LensInfo }>} A promise that resolves with the zoom state.\n * @since 7.4.0\n */\n getZoom(): Promise<{\n min: number;\n max: number;\n current: number;\n lens: LensInfo;\n }>;\n\n /**\n * Sets the camera's zoom level.\n *\n * @param {{ level: number; ramp?: boolean }} options - The desired zoom level. `ramp` is currently unused.\n * @returns {Promise<void>} A promise that resolves when the zoom level is set.\n * @since 7.4.0\n */\n setZoom(options: { level: number; ramp?: boolean }): Promise<void>;\n\n /**\n * Gets the current flash mode.\n *\n * @returns {Promise<{ flashMode: FlashMode }>} A promise that resolves with the current flash mode.\n * @since 7.4.0\n */\n getFlashMode(): Promise<{ flashMode: FlashMode }>;\n\n /**\n * Removes all registered listeners.\n *\n * @since 7.4.0\n */\n removeAllListeners(): Promise<void>;\n\n /**\n * Switches the active camera to the one with the specified `deviceId`.\n *\n * @param {{ deviceId: string }} options - The ID of the device to switch to.\n * @returns {Promise<void>} A promise that resolves when the camera is switched.\n * @since 7.4.0\n */\n setDeviceId(options: { deviceId: string }): Promise<void>;\n\n /**\n * Gets the ID of the currently active camera device.\n *\n * @returns {Promise<{ deviceId: string }>} A promise that resolves with the current device ID.\n * @since 7.4.0\n */\n getDeviceId(): Promise<{ deviceId: string }>;\n\n /**\n * Gets the current preview size and position.\n * @returns {Promise<{x: number, y: number, width: number, height: number}>}\n */\n getPreviewSize(): Promise<{x: number, y: number, width: number, height: number}>;\n /**\n * Sets the preview size and position.\n * @param options The new position and dimensions.\n * @returns {Promise<{ width: number; height: number; x: number; y: number }>} A promise that resolves with the actual preview dimensions and position.\n */\n setPreviewSize(options: {x: number, y: number, width: number, height: number}): Promise<{\n width: number;\n height: number;\n x: number;\n y: number;\n }>;\n}\n"]}
package/dist/esm/web.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { WebPlugin } from "@capacitor/core";
2
- import type { CameraDevice, CameraOpacityOptions, CameraPreviewFlashMode, CameraPreviewOptions, CameraPreviewPictureOptions, CameraPreviewPlugin, CameraSampleOptions, FlashMode, LensInfo } from "./definitions";
2
+ import type { CameraDevice, CameraOpacityOptions, CameraPreviewFlashMode, CameraPreviewOptions, CameraPreviewPictureOptions, CameraPreviewPlugin, CameraSampleOptions, GridMode, FlashMode, LensInfo } from "./definitions";
3
3
  export declare class CameraPreviewWeb extends WebPlugin implements CameraPreviewPlugin {
4
4
  /**
5
5
  * track which camera is used based on start options
@@ -7,11 +7,18 @@ export declare class CameraPreviewWeb extends WebPlugin implements CameraPreview
7
7
  */
8
8
  private isBackCamera;
9
9
  private currentDeviceId;
10
+ private videoElement;
11
+ private isStarted;
10
12
  constructor();
11
13
  getSupportedPictureSizes(): Promise<any>;
12
- start(options: CameraPreviewOptions): Promise<void>;
14
+ start(options: CameraPreviewOptions): Promise<{
15
+ width: number;
16
+ height: number;
17
+ x: number;
18
+ y: number;
19
+ }>;
13
20
  private stopStream;
14
- stop(): Promise<any>;
21
+ stop(): Promise<void>;
15
22
  capture(options: CameraPreviewPictureOptions): Promise<any>;
16
23
  captureSample(_options: CameraSampleOptions): Promise<any>;
17
24
  stopRecordVideo(): Promise<any>;
@@ -52,4 +59,41 @@ export declare class CameraPreviewWeb extends WebPlugin implements CameraPreview
52
59
  setDeviceId(options: {
53
60
  deviceId: string;
54
61
  }): Promise<void>;
62
+ getAspectRatio(): Promise<{
63
+ aspectRatio: '4:3' | '16:9';
64
+ }>;
65
+ setAspectRatio(options: {
66
+ aspectRatio: '4:3' | '16:9';
67
+ x?: number;
68
+ y?: number;
69
+ }): Promise<{
70
+ width: number;
71
+ height: number;
72
+ x: number;
73
+ y: number;
74
+ }>;
75
+ private createGridOverlay;
76
+ setGridMode(options: {
77
+ gridMode: GridMode;
78
+ }): Promise<void>;
79
+ getGridMode(): Promise<{
80
+ gridMode: GridMode;
81
+ }>;
82
+ getPreviewSize(): Promise<{
83
+ x: number;
84
+ y: number;
85
+ width: number;
86
+ height: number;
87
+ }>;
88
+ setPreviewSize(options: {
89
+ x: number;
90
+ y: number;
91
+ width: number;
92
+ height: number;
93
+ }): Promise<{
94
+ width: number;
95
+ height: number;
96
+ x: number;
97
+ y: number;
98
+ }>;
55
99
  }
package/dist/esm/web.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { WebPlugin } from "@capacitor/core";
2
2
  import { DeviceType } from "./definitions";
3
+ const DEFAULT_VIDEO_ID = "capgo_video";
3
4
  export class CameraPreviewWeb extends WebPlugin {
4
5
  constructor() {
5
6
  super();
@@ -9,87 +10,100 @@ export class CameraPreviewWeb extends WebPlugin {
9
10
  */
10
11
  this.isBackCamera = false;
11
12
  this.currentDeviceId = null;
13
+ this.videoElement = null;
14
+ this.isStarted = false;
12
15
  }
13
16
  async getSupportedPictureSizes() {
14
17
  throw new Error("getSupportedPictureSizes not supported under the web platform");
15
18
  }
16
19
  async start(options) {
17
- var _a;
18
- await navigator.mediaDevices
19
- .getUserMedia({
20
- audio: !options.disableAudio,
21
- video: true,
22
- })
23
- .then((stream) => {
24
- // Stop any existing stream so we can request media with different constraints based on user input
25
- stream.getTracks().forEach((track) => track.stop());
26
- })
27
- .catch((error) => {
28
- Promise.reject(error);
29
- });
30
- 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;
31
28
  const parent = document.getElementById((options === null || options === void 0 ? void 0 : options.parent) || "");
32
- if (!video) {
33
- const videoElement = document.createElement("video");
34
- videoElement.id = "video";
35
- videoElement.setAttribute("class", (options === null || options === void 0 ? void 0 : options.className) || "");
36
- // Don't flip video feed if camera is rear facing
37
- if (options.position !== "rear") {
38
- videoElement.setAttribute("style", "-webkit-transform: scaleX(-1); transform: scaleX(-1);");
39
- }
40
- const userAgent = navigator.userAgent.toLowerCase();
41
- const isSafari = userAgent.includes("safari") && !userAgent.includes("chrome");
42
- // Safari on iOS needs to have the autoplay, muted and playsinline attributes set for video.play() to be successful
43
- // Without these attributes videoElement.play() will throw a NotAllowedError
44
- // https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari
45
- if (isSafari) {
46
- videoElement.setAttribute("autoplay", "true");
47
- videoElement.setAttribute("muted", "true");
48
- videoElement.setAttribute("playsinline", "true");
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;
49
74
  }
50
- parent === null || parent === void 0 ? void 0 : parent.appendChild(videoElement);
51
- if ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia) {
52
- const constraints = {
53
- video: {
54
- width: { ideal: options.width },
55
- height: { ideal: options.height },
56
- },
57
- };
58
- if (options.deviceId) {
59
- constraints.video.deviceId = { exact: options.deviceId };
60
- this.currentDeviceId = options.deviceId;
61
- // Try to determine camera position from device
62
- const devices = await navigator.mediaDevices.enumerateDevices();
63
- const device = devices.find(d => d.deviceId === options.deviceId);
64
- 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;
65
- }
66
- else if (options.position === "rear") {
67
- constraints.video.facingMode = "environment";
68
- this.isBackCamera = true;
69
- }
70
- else {
71
- this.isBackCamera = false;
72
- }
73
- const self = this;
74
- await navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
75
- if (document.getElementById("video")) {
76
- // video.src = window.URL.createObjectURL(stream);
77
- videoElement.srcObject = stream;
78
- videoElement.play();
79
- Promise.resolve({});
80
- }
81
- else {
82
- self.stopStream(stream);
83
- Promise.reject(new Error("camera already stopped"));
84
- }
85
- }, (err) => {
86
- Promise.reject(new Error(err));
87
- });
75
+ else if (options.height) {
76
+ this.videoElement.width = options.height * ratio;
88
77
  }
89
78
  }
90
79
  else {
91
- 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");
92
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
+ };
93
107
  }
94
108
  stopStream(stream) {
95
109
  if (stream) {
@@ -99,16 +113,20 @@ export class CameraPreviewWeb extends WebPlugin {
99
113
  }
100
114
  }
101
115
  async stop() {
102
- const video = document.getElementById("video");
116
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
103
117
  if (video) {
104
118
  video.pause();
105
119
  this.stopStream(video.srcObject);
106
120
  video.remove();
121
+ this.isStarted = false;
107
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();
108
126
  }
109
127
  async capture(options) {
110
128
  return new Promise((resolve, reject) => {
111
- const video = document.getElementById("video");
129
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
112
130
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
113
131
  reject(new Error("camera is not running"));
114
132
  return;
@@ -126,6 +144,12 @@ export class CameraPreviewWeb extends WebPlugin {
126
144
  context === null || context === void 0 ? void 0 : context.scale(-1, 1);
127
145
  }
128
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
+ }
129
153
  if ((options.format || "jpeg") === "jpeg") {
130
154
  base64EncodedImage = canvas
131
155
  .toDataURL("image/jpeg", (options.quality || 85) / 100.0)
@@ -139,6 +163,7 @@ export class CameraPreviewWeb extends WebPlugin {
139
163
  }
140
164
  resolve({
141
165
  value: base64EncodedImage,
166
+ exif: {},
142
167
  });
143
168
  });
144
169
  }
@@ -162,7 +187,7 @@ export class CameraPreviewWeb extends WebPlugin {
162
187
  throw new Error(`setFlashMode not supported under the web platform${_options}`);
163
188
  }
164
189
  async flip() {
165
- const video = document.getElementById("video");
190
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
166
191
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
167
192
  throw new Error("camera is not running");
168
193
  }
@@ -202,12 +227,12 @@ export class CameraPreviewWeb extends WebPlugin {
202
227
  }
203
228
  }
204
229
  async setOpacity(_options) {
205
- const video = document.getElementById("video");
230
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
206
231
  if (!!video && !!_options.opacity)
207
232
  video.style.setProperty("opacity", _options.opacity.toString());
208
233
  }
209
234
  async isRunning() {
210
- const video = document.getElementById("video");
235
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
211
236
  return { isRunning: !!video && !!video.srcObject };
212
237
  }
213
238
  async getAvailableDevices() {
@@ -281,7 +306,7 @@ export class CameraPreviewWeb extends WebPlugin {
281
306
  return { devices: result };
282
307
  }
283
308
  async getZoom() {
284
- const video = document.getElementById("video");
309
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
285
310
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
286
311
  throw new Error("camera is not running");
287
312
  }
@@ -332,7 +357,7 @@ export class CameraPreviewWeb extends WebPlugin {
332
357
  };
333
358
  }
334
359
  async setZoom(options) {
335
- const video = document.getElementById("video");
360
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
336
361
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
337
362
  throw new Error("camera is not running");
338
363
  }
@@ -362,7 +387,7 @@ export class CameraPreviewWeb extends WebPlugin {
362
387
  return { deviceId: this.currentDeviceId || "" };
363
388
  }
364
389
  async setDeviceId(options) {
365
- const video = document.getElementById("video");
390
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
366
391
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
367
392
  throw new Error("camera is not running");
368
393
  }
@@ -400,5 +425,164 @@ export class CameraPreviewWeb extends WebPlugin {
400
425
  throw new Error(`Failed to swap to device ${options.deviceId}: ${error}`);
401
426
  }
402
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
+ }
403
587
  }
404
588
  //# sourceMappingURL=web.js.map