@capgo/camera-preview 6.2.30 → 6.3.4

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/dist/esm/web.js CHANGED
@@ -1,10 +1,15 @@
1
- import { WebPlugin } from '@capacitor/core';
1
+ import { WebPlugin } from "@capacitor/core";
2
2
  export class CameraPreviewWeb extends WebPlugin {
3
3
  constructor() {
4
4
  super({
5
- name: 'CameraPreview',
6
- platforms: ['web'],
5
+ name: "CameraPreview",
6
+ platforms: ["web"],
7
7
  });
8
+ /**
9
+ * track which camera is used based on start options
10
+ * used in capture
11
+ */
12
+ this.isBackCamera = false;
8
13
  }
9
14
  async start(options) {
10
15
  var _a;
@@ -15,32 +20,32 @@ export class CameraPreviewWeb extends WebPlugin {
15
20
  })
16
21
  .then((stream) => {
17
22
  // Stop any existing stream so we can request media with different constraints based on user input
18
- stream.getTracks().forEach(track => track.stop());
23
+ stream.getTracks().forEach((track) => track.stop());
19
24
  })
20
25
  .catch((error) => {
21
26
  Promise.reject(error);
22
27
  });
23
- const video = document.getElementById('video');
24
- const parent = document.getElementById(options.parent);
28
+ const video = document.getElementById("video");
29
+ const parent = document.getElementById((options === null || options === void 0 ? void 0 : options.parent) || "");
25
30
  if (!video) {
26
- const videoElement = document.createElement('video');
27
- videoElement.id = 'video';
28
- videoElement.setAttribute('class', options.className || '');
31
+ const videoElement = document.createElement("video");
32
+ videoElement.id = "video";
33
+ videoElement.setAttribute("class", (options === null || options === void 0 ? void 0 : options.className) || "");
29
34
  // Don't flip video feed if camera is rear facing
30
- if (options.position !== 'rear') {
31
- videoElement.setAttribute('style', '-webkit-transform: scaleX(-1); transform: scaleX(-1);');
35
+ if (options.position !== "rear") {
36
+ videoElement.setAttribute("style", "-webkit-transform: scaleX(-1); transform: scaleX(-1);");
32
37
  }
33
38
  const userAgent = navigator.userAgent.toLowerCase();
34
- const isSafari = userAgent.includes('safari') && !userAgent.includes('chrome');
39
+ const isSafari = userAgent.includes("safari") && !userAgent.includes("chrome");
35
40
  // Safari on iOS needs to have the autoplay, muted and playsinline attributes set for video.play() to be successful
36
41
  // Without these attributes videoElement.play() will throw a NotAllowedError
37
42
  // https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari
38
43
  if (isSafari) {
39
- videoElement.setAttribute('autoplay', 'true');
40
- videoElement.setAttribute('muted', 'true');
41
- videoElement.setAttribute('playsinline', 'true');
44
+ videoElement.setAttribute("autoplay", "true");
45
+ videoElement.setAttribute("muted", "true");
46
+ videoElement.setAttribute("playsinline", "true");
42
47
  }
43
- parent.appendChild(videoElement);
48
+ parent === null || parent === void 0 ? void 0 : parent.appendChild(videoElement);
44
49
  if ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia) {
45
50
  const constraints = {
46
51
  video: {
@@ -48,18 +53,17 @@ export class CameraPreviewWeb extends WebPlugin {
48
53
  height: { ideal: options.height },
49
54
  },
50
55
  };
51
- if (options.position === 'rear') {
52
- constraints.video.facingMode
53
- = 'environment';
56
+ if (options.position === "rear") {
57
+ constraints.video.facingMode =
58
+ "environment";
54
59
  this.isBackCamera = true;
55
60
  }
56
61
  else {
57
62
  this.isBackCamera = false;
58
63
  }
59
- // eslint-disable-next-line ts/no-this-alias
60
64
  const self = this;
61
65
  await navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
62
- if (document.getElementById('video')) {
66
+ if (document.getElementById("video")) {
63
67
  // video.src = window.URL.createObjectURL(stream);
64
68
  videoElement.srcObject = stream;
65
69
  videoElement.play();
@@ -67,7 +71,7 @@ export class CameraPreviewWeb extends WebPlugin {
67
71
  }
68
72
  else {
69
73
  self.stopStream(stream);
70
- Promise.reject(new Error('camera already stopped'));
74
+ Promise.reject(new Error("camera already stopped"));
71
75
  }
72
76
  }, (err) => {
73
77
  Promise.reject(new Error(err));
@@ -75,7 +79,7 @@ export class CameraPreviewWeb extends WebPlugin {
75
79
  }
76
80
  }
77
81
  else {
78
- Promise.reject(new Error('camera already started'));
82
+ Promise.reject(new Error("camera already started"));
79
83
  }
80
84
  }
81
85
  stopStream(stream) {
@@ -86,7 +90,7 @@ export class CameraPreviewWeb extends WebPlugin {
86
90
  }
87
91
  }
88
92
  async stop() {
89
- const video = document.getElementById('video');
93
+ const video = document.getElementById("video");
90
94
  if (video) {
91
95
  video.pause();
92
96
  this.stopStream(video.srcObject);
@@ -95,33 +99,33 @@ export class CameraPreviewWeb extends WebPlugin {
95
99
  }
96
100
  async capture(options) {
97
101
  return new Promise((resolve, reject) => {
98
- const video = document.getElementById('video');
102
+ const video = document.getElementById("video");
99
103
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
100
- reject(new Error('camera is not running'));
104
+ reject(new Error("camera is not running"));
101
105
  return;
102
106
  }
103
107
  // video.width = video.offsetWidth;
104
108
  let base64EncodedImage;
105
109
  if (video && video.videoWidth > 0 && video.videoHeight > 0) {
106
- const canvas = document.createElement('canvas');
107
- const context = canvas.getContext('2d');
110
+ const canvas = document.createElement("canvas");
111
+ const context = canvas.getContext("2d");
108
112
  canvas.width = video.videoWidth;
109
113
  canvas.height = video.videoHeight;
110
114
  // flip horizontally back camera isn't used
111
115
  if (!this.isBackCamera) {
112
- context.translate(video.videoWidth, 0);
113
- context.scale(-1, 1);
116
+ context === null || context === void 0 ? void 0 : context.translate(video.videoWidth, 0);
117
+ context === null || context === void 0 ? void 0 : context.scale(-1, 1);
114
118
  }
115
- context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
116
- if ((options.format || 'jpeg') === 'jpeg') {
119
+ context === null || context === void 0 ? void 0 : context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
120
+ if ((options.format || "jpeg") === "jpeg") {
117
121
  base64EncodedImage = canvas
118
- .toDataURL('image/jpeg', (options.quality || 85) / 100.0)
119
- .replace('data:image/jpeg;base64,', '');
122
+ .toDataURL("image/jpeg", (options.quality || 85) / 100.0)
123
+ .replace("data:image/jpeg;base64,", "");
120
124
  }
121
125
  else {
122
126
  base64EncodedImage = canvas
123
- .toDataURL('image/png')
124
- .replace('data:image/png;base64,', '');
127
+ .toDataURL("image/png")
128
+ .replace("data:image/png;base64,", "");
125
129
  }
126
130
  }
127
131
  resolve({
@@ -133,27 +137,28 @@ export class CameraPreviewWeb extends WebPlugin {
133
137
  return this.capture(_options);
134
138
  }
135
139
  async stopRecordVideo() {
136
- throw new Error('stopRecordVideo not supported under the web platform');
140
+ throw new Error("stopRecordVideo not supported under the web platform");
137
141
  }
138
142
  async startRecordVideo(_options) {
139
- throw new Error('startRecordVideo not supported under the web platform');
143
+ console.log("startRecordVideo", _options);
144
+ throw new Error("startRecordVideo not supported under the web platform");
140
145
  }
141
146
  async getSupportedFlashModes() {
142
- throw new Error('getSupportedFlashModes not supported under the web platform');
147
+ throw new Error("getSupportedFlashModes not supported under the web platform");
143
148
  }
144
149
  async getHorizontalFov() {
145
- throw new Error('getHorizontalFov not supported under the web platform');
150
+ throw new Error("getHorizontalFov not supported under the web platform");
146
151
  }
147
152
  async setFlashMode(_options) {
148
153
  throw new Error(`setFlashMode not supported under the web platform${_options}`);
149
154
  }
150
155
  async flip() {
151
- throw new Error('flip not supported under the web platform');
156
+ throw new Error("flip not supported under the web platform");
152
157
  }
153
158
  async setOpacity(_options) {
154
- const video = document.getElementById('video');
159
+ const video = document.getElementById("video");
155
160
  if (!!video && !!_options.opacity)
156
- video.style.setProperty('opacity', _options.opacity.toString());
161
+ video.style.setProperty("opacity", _options.opacity.toString());
157
162
  }
158
163
  }
159
164
  //# sourceMappingURL=web.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAW3C,MAAM,OAAO,gBAAiB,SAAQ,SAAS;IAO7C;QACE,KAAK,CAAC;YACJ,IAAI,EAAE,eAAe;YACrB,SAAS,EAAE,CAAC,KAAK,CAAC;SACnB,CA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CAAC,QAA6B;QAC/C,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;IAC/B,CAAC;IAED,KAAK,CAAC,eAAe;QACnB,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAA;IACzE,CAAC;IAED,KAAK,CAAC,gBAAgB,CAAC,QAA8B;QACnD,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAA;IAC1E,CAAC;IAED,KAAK,CAAC,sBAAsB;QAG1B,MAAM,IAAI,KAAK,CACb,6DAA6D,CAC9D,CAAA;IACH,CAAC;IAED,KAAK,CAAC,gBAAgB;QAGpB,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAA;IAC1E,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,QAElB;QACC,MAAM,IAAI,KAAK,CACb,oDAAoD,QAAQ,EAAE,CAC/D,CAAA;IACH,CAAC;IAED,KAAK,CAAC,IAAI;QACR,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAA;IAC9D,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,QAA8B;QAC7C,MAAM,KAAK,GAAG,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAqB,CAAA;QAClE,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO;YAC/B,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAA;IACnE,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core'\n\nimport type {\n CameraOpacityOptions,\n CameraPreviewFlashMode,\n CameraPreviewOptions,\n CameraPreviewPictureOptions,\n CameraPreviewPlugin,\n CameraSampleOptions,\n} from './definitions'\n\nexport class CameraPreviewWeb extends WebPlugin implements CameraPreviewPlugin {\n /**\n * track which camera is used based on start options\n * used in capture\n */\n private isBackCamera: boolean\n\n constructor() {\n super({\n name: 'CameraPreview',\n platforms: ['web'],\n })\n }\n\n async start(options: CameraPreviewOptions): Promise<void> {\n await navigator.mediaDevices\n .getUserMedia({\n audio: !options.disableAudio,\n video: true,\n })\n .then((stream: MediaStream) => {\n // Stop any existing stream so we can request media with different constraints based on user input\n stream.getTracks().forEach(track => track.stop())\n })\n .catch((error) => {\n Promise.reject(error)\n })\n\n const video = document.getElementById('video')\n const parent = document.getElementById(options.parent)\n\n if (!video) {\n const videoElement = document.createElement('video')\n videoElement.id = 'video'\n videoElement.setAttribute('class', options.className || '')\n\n // Don't flip video feed if camera is rear facing\n if (options.position !== 'rear') {\n videoElement.setAttribute(\n 'style',\n '-webkit-transform: scaleX(-1); transform: scaleX(-1);',\n )\n }\n\n const userAgent = navigator.userAgent.toLowerCase()\n const isSafari\n = userAgent.includes('safari') && !userAgent.includes('chrome')\n\n // Safari on iOS needs to have the autoplay, muted and playsinline attributes set for video.play() to be successful\n // Without these attributes videoElement.play() will throw a NotAllowedError\n // https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari\n if (isSafari) {\n videoElement.setAttribute('autoplay', 'true')\n videoElement.setAttribute('muted', 'true')\n videoElement.setAttribute('playsinline', 'true')\n }\n\n parent.appendChild(videoElement)\n\n if (navigator?.mediaDevices?.getUserMedia) {\n const constraints: MediaStreamConstraints = {\n video: {\n width: { ideal: options.width },\n height: { ideal: options.height },\n },\n }\n\n if (options.position === 'rear') {\n (constraints.video as MediaTrackConstraints).facingMode\n = 'environment'\n this.isBackCamera = true\n }\n else {\n this.isBackCamera = false\n }\n\n // eslint-disable-next-line ts/no-this-alias\n const self = this\n await navigator.mediaDevices.getUserMedia(constraints).then(\n (stream) => {\n if (document.getElementById('video')) {\n // video.src = window.URL.createObjectURL(stream);\n videoElement.srcObject = stream\n videoElement.play()\n Promise.resolve({})\n }\n else {\n self.stopStream(stream)\n Promise.reject(new Error('camera already stopped'))\n }\n },\n (err) => {\n Promise.reject(new Error(err))\n },\n )\n }\n }\n else {\n Promise.reject(new Error('camera already started'))\n }\n }\n\n private stopStream(stream: any) {\n if (stream) {\n const tracks = stream.getTracks()\n\n for (const track of tracks) track.stop()\n }\n }\n\n async stop(): Promise<any> {\n const video = document.getElementById('video') as HTMLVideoElement\n if (video) {\n video.pause()\n\n this.stopStream(video.srcObject)\n\n video.remove()\n }\n }\n\n async capture(options: CameraPreviewPictureOptions): Promise<any> {\n return new Promise((resolve, reject) => {\n const video = document.getElementById('video') as HTMLVideoElement\n if (!video?.srcObject) {\n reject(new Error('camera is not running'))\n return\n }\n\n // video.width = video.offsetWidth;\n\n let base64EncodedImage\n\n if (video && video.videoWidth > 0 && video.videoHeight > 0) {\n const canvas = document.createElement('canvas')\n const context = canvas.getContext('2d')\n canvas.width = video.videoWidth\n canvas.height = video.videoHeight\n\n // flip horizontally back camera isn't used\n if (!this.isBackCamera) {\n context.translate(video.videoWidth, 0)\n context.scale(-1, 1)\n }\n context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight)\n\n if ((options.format || 'jpeg') === 'jpeg') {\n base64EncodedImage = canvas\n .toDataURL('image/jpeg', (options.quality || 85) / 100.0)\n .replace('data:image/jpeg;base64,', '')\n }\n else {\n base64EncodedImage = canvas\n .toDataURL('image/png')\n .replace('data:image/png;base64,', '')\n }\n }\n\n resolve({\n value: base64EncodedImage,\n })\n })\n }\n\n async captureSample(_options: CameraSampleOptions): Promise<any> {\n return this.capture(_options)\n }\n\n async stopRecordVideo(): Promise<any> {\n throw new Error('stopRecordVideo not supported under the web platform')\n }\n\n async startRecordVideo(_options: CameraPreviewOptions): Promise<any> {\n throw new Error('startRecordVideo not supported under the web platform')\n }\n\n async getSupportedFlashModes(): Promise<{\n result: CameraPreviewFlashMode[]\n }> {\n throw new Error(\n 'getSupportedFlashModes not supported under the web platform',\n )\n }\n\n async getHorizontalFov(): Promise<{\n result: any\n }> {\n throw new Error('getHorizontalFov not supported under the web platform')\n }\n\n async setFlashMode(_options: {\n flashMode: CameraPreviewFlashMode | string\n }): Promise<void> {\n throw new Error(\n `setFlashMode not supported under the web platform${_options}`,\n )\n }\n\n async flip(): Promise<void> {\n throw new Error('flip not supported under the web platform')\n }\n\n async setOpacity(_options: CameraOpacityOptions): Promise<any> {\n const video = document.getElementById('video') as HTMLVideoElement\n if (!!video && !!_options.opacity)\n video.style.setProperty('opacity', _options.opacity.toString())\n }\n}\n"]}
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAW5C,MAAM,OAAO,gBAAiB,SAAQ,SAAS;IAO7C;QACE,KAAK,CAAC;YACJ,IAAI,EAAE,eAAe;YACrB,SAAS,EAAE,CAAC,KAAK,CAAC;SACnB,CAAC,CAAC;QAVL;;;WAGG;QACK,iBAAY,GAAY,KAAK,CAAC;IAOtC,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,OAA6B;;QACvC,MAAM,SAAS,CAAC,YAAY;aACzB,YAAY,CAAC;YACZ,KAAK,EAAE,CAAC,OAAO,CAAC,YAAY;YAC5B,KAAK,EAAE,IAAI;SACZ,CAAC;aACD,IAAI,CAAC,CAAC,MAAmB,EAAE,EAAE;YAC5B,kGAAkG;YAClG,MAAM,CAAC,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;QACtD,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACf,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACxB,CAAC,CAAC,CAAC;QAEL,MAAM,KAAK,GAAG,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QAC/C,MAAM,MAAM,GAAG,QAAQ,CAAC,cAAc,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,KAAI,EAAE,CAAC,CAAC;QAE9D,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,YAAY,GAAG,Q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oD,QAAQ,EAAE,CAC/D,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,IAAI;QACR,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;IAC/D,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,QAA8B;QAC7C,MAAM,KAAK,GAAG,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAqB,CAAC;QACnE,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO;YAC/B,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IACpE,CAAC;CACF","sourcesContent":["import { WebPlugin } from \"@capacitor/core\";\n\nimport type {\n CameraOpacityOptions,\n CameraPreviewFlashMode,\n CameraPreviewOptions,\n CameraPreviewPictureOptions,\n CameraPreviewPlugin,\n CameraSampleOptions,\n} from \"./definitions\";\n\nexport class CameraPreviewWeb extends WebPlugin implements CameraPreviewPlugin {\n /**\n * track which camera is used based on start options\n * used in capture\n */\n private isBackCamera: boolean = false;\n\n constructor() {\n super({\n name: \"CameraPreview\",\n platforms: [\"web\"],\n });\n }\n\n async start(options: CameraPreviewOptions): Promise<void> {\n await navigator.mediaDevices\n .getUserMedia({\n audio: !options.disableAudio,\n video: true,\n })\n .then((stream: MediaStream) => {\n // Stop any existing stream so we can request media with different constraints based on user input\n stream.getTracks().forEach((track) => track.stop());\n })\n .catch((error) => {\n Promise.reject(error);\n });\n\n const video = document.getElementById(\"video\");\n const parent = document.getElementById(options?.parent || \"\");\n\n if (!video) {\n const videoElement = document.createElement(\"video\");\n videoElement.id = \"video\";\n videoElement.setAttribute(\"class\", options?.className || \"\");\n\n // Don't flip video feed if camera is rear facing\n if (options.position !== \"rear\") {\n videoElement.setAttribute(\n \"style\",\n \"-webkit-transform: scaleX(-1); transform: scaleX(-1);\",\n );\n }\n\n const userAgent = navigator.userAgent.toLowerCase();\n const isSafari =\n userAgent.includes(\"safari\") && !userAgent.includes(\"chrome\");\n\n // Safari on iOS needs to have the autoplay, muted and playsinline attributes set for video.play() to be successful\n // Without these attributes videoElement.play() will throw a NotAllowedError\n // https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari\n if (isSafari) {\n videoElement.setAttribute(\"autoplay\", \"true\");\n videoElement.setAttribute(\"muted\", \"true\");\n videoElement.setAttribute(\"playsinline\", \"true\");\n }\n\n parent?.appendChild(videoElement);\n\n if (navigator?.mediaDevices?.getUserMedia) {\n const constraints: MediaStreamConstraints = {\n video: {\n width: { ideal: options.width },\n height: { ideal: options.height },\n },\n };\n\n if (options.position === \"rear\") {\n (constraints.video as MediaTrackConstraints).facingMode =\n \"environment\";\n this.isBackCamera = true;\n } else {\n this.isBackCamera = false;\n }\n\n const self = this;\n await navigator.mediaDevices.getUserMedia(constraints).then(\n (stream) => {\n if (document.getElementById(\"video\")) {\n // video.src = window.URL.createObjectURL(stream);\n videoElement.srcObject = stream;\n videoElement.play();\n Promise.resolve({});\n } else {\n self.stopStream(stream);\n Promise.reject(new Error(\"camera already stopped\"));\n }\n },\n (err) => {\n Promise.reject(new Error(err));\n },\n );\n }\n } else {\n Promise.reject(new Error(\"camera already started\"));\n }\n }\n\n private stopStream(stream: any) {\n if (stream) {\n const tracks = stream.getTracks();\n\n for (const track of tracks) track.stop();\n }\n }\n\n async stop(): Promise<any> {\n const video = document.getElementById(\"video\") as HTMLVideoElement;\n if (video) {\n video.pause();\n\n this.stopStream(video.srcObject);\n\n video.remove();\n }\n }\n\n async capture(options: CameraPreviewPictureOptions): Promise<any> {\n return new Promise((resolve, reject) => {\n const video = document.getElementById(\"video\") as HTMLVideoElement;\n if (!video?.srcObject) {\n reject(new Error(\"camera is not running\"));\n return;\n }\n\n // video.width = video.offsetWidth;\n\n let base64EncodedImage;\n\n if (video && video.videoWidth > 0 && video.videoHeight > 0) {\n const canvas = document.createElement(\"canvas\");\n const context = canvas.getContext(\"2d\");\n canvas.width = video.videoWidth;\n canvas.height = video.videoHeight;\n\n // flip horizontally back camera isn't used\n if (!this.isBackCamera) {\n context?.translate(video.videoWidth, 0);\n context?.scale(-1, 1);\n }\n context?.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);\n\n if ((options.format || \"jpeg\") === \"jpeg\") {\n base64EncodedImage = canvas\n .toDataURL(\"image/jpeg\", (options.quality || 85) / 100.0)\n .replace(\"data:image/jpeg;base64,\", \"\");\n } else {\n base64EncodedImage = canvas\n .toDataURL(\"image/png\")\n .replace(\"data:image/png;base64,\", \"\");\n }\n }\n\n resolve({\n value: base64EncodedImage,\n });\n });\n }\n\n async captureSample(_options: CameraSampleOptions): Promise<any> {\n return this.capture(_options);\n }\n\n async stopRecordVideo(): Promise<any> {\n throw new Error(\"stopRecordVideo not supported under the web platform\");\n }\n\n async startRecordVideo(_options: CameraPreviewOptions): Promise<any> {\n console.log(\"startRecordVideo\", _options);\n throw new Error(\"startRecordVideo not supported under the web platform\");\n }\n\n async getSupportedFlashModes(): Promise<{\n result: CameraPreviewFlashMode[];\n }> {\n throw new Error(\n \"getSupportedFlashModes not supported under the web platform\",\n );\n }\n\n async getHorizontalFov(): Promise<{\n result: any;\n }> {\n throw new Error(\"getHorizontalFov not supported under the web platform\");\n }\n\n async setFlashMode(_options: {\n flashMode: CameraPreviewFlashMode | string;\n }): Promise<void> {\n throw new Error(\n `setFlashMode not supported under the web platform${_options}`,\n );\n }\n\n async flip(): Promise<void> {\n throw new Error(\"flip not supported under the web platform\");\n }\n\n async setOpacity(_options: CameraOpacityOptions): Promise<any> {\n const video = document.getElementById(\"video\") as HTMLVideoElement;\n if (!!video && !!_options.opacity)\n video.style.setProperty(\"opacity\", _options.opacity.toString());\n }\n}\n"]}
@@ -2,16 +2,21 @@
2
2
 
3
3
  var core = require('@capacitor/core');
4
4
 
5
- const CameraPreview = core.registerPlugin('CameraPreview', {
6
- web: () => Promise.resolve().then(function () { return web; }).then(m => new m.CameraPreviewWeb()),
5
+ const CameraPreview = core.registerPlugin("CameraPreview", {
6
+ web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.CameraPreviewWeb()),
7
7
  });
8
8
 
9
9
  class CameraPreviewWeb extends core.WebPlugin {
10
10
  constructor() {
11
11
  super({
12
- name: 'CameraPreview',
13
- platforms: ['web'],
12
+ name: "CameraPreview",
13
+ platforms: ["web"],
14
14
  });
15
+ /**
16
+ * track which camera is used based on start options
17
+ * used in capture
18
+ */
19
+ this.isBackCamera = false;
15
20
  }
16
21
  async start(options) {
17
22
  var _a;
@@ -22,32 +27,32 @@ class CameraPreviewWeb extends core.WebPlugin {
22
27
  })
23
28
  .then((stream) => {
24
29
  // Stop any existing stream so we can request media with different constraints based on user input
25
- stream.getTracks().forEach(track => track.stop());
30
+ stream.getTracks().forEach((track) => track.stop());
26
31
  })
27
32
  .catch((error) => {
28
33
  Promise.reject(error);
29
34
  });
30
- const video = document.getElementById('video');
31
- const parent = document.getElementById(options.parent);
35
+ const video = document.getElementById("video");
36
+ const parent = document.getElementById((options === null || options === void 0 ? void 0 : options.parent) || "");
32
37
  if (!video) {
33
- const videoElement = document.createElement('video');
34
- videoElement.id = 'video';
35
- videoElement.setAttribute('class', options.className || '');
38
+ const videoElement = document.createElement("video");
39
+ videoElement.id = "video";
40
+ videoElement.setAttribute("class", (options === null || options === void 0 ? void 0 : options.className) || "");
36
41
  // 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);');
42
+ if (options.position !== "rear") {
43
+ videoElement.setAttribute("style", "-webkit-transform: scaleX(-1); transform: scaleX(-1);");
39
44
  }
40
45
  const userAgent = navigator.userAgent.toLowerCase();
41
- const isSafari = userAgent.includes('safari') && !userAgent.includes('chrome');
46
+ const isSafari = userAgent.includes("safari") && !userAgent.includes("chrome");
42
47
  // Safari on iOS needs to have the autoplay, muted and playsinline attributes set for video.play() to be successful
43
48
  // Without these attributes videoElement.play() will throw a NotAllowedError
44
49
  // https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari
45
50
  if (isSafari) {
46
- videoElement.setAttribute('autoplay', 'true');
47
- videoElement.setAttribute('muted', 'true');
48
- videoElement.setAttribute('playsinline', 'true');
51
+ videoElement.setAttribute("autoplay", "true");
52
+ videoElement.setAttribute("muted", "true");
53
+ videoElement.setAttribute("playsinline", "true");
49
54
  }
50
- parent.appendChild(videoElement);
55
+ parent === null || parent === void 0 ? void 0 : parent.appendChild(videoElement);
51
56
  if ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia) {
52
57
  const constraints = {
53
58
  video: {
@@ -55,18 +60,17 @@ class CameraPreviewWeb extends core.WebPlugin {
55
60
  height: { ideal: options.height },
56
61
  },
57
62
  };
58
- if (options.position === 'rear') {
59
- constraints.video.facingMode
60
- = 'environment';
63
+ if (options.position === "rear") {
64
+ constraints.video.facingMode =
65
+ "environment";
61
66
  this.isBackCamera = true;
62
67
  }
63
68
  else {
64
69
  this.isBackCamera = false;
65
70
  }
66
- // eslint-disable-next-line ts/no-this-alias
67
71
  const self = this;
68
72
  await navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
69
- if (document.getElementById('video')) {
73
+ if (document.getElementById("video")) {
70
74
  // video.src = window.URL.createObjectURL(stream);
71
75
  videoElement.srcObject = stream;
72
76
  videoElement.play();
@@ -74,7 +78,7 @@ class CameraPreviewWeb extends core.WebPlugin {
74
78
  }
75
79
  else {
76
80
  self.stopStream(stream);
77
- Promise.reject(new Error('camera already stopped'));
81
+ Promise.reject(new Error("camera already stopped"));
78
82
  }
79
83
  }, (err) => {
80
84
  Promise.reject(new Error(err));
@@ -82,7 +86,7 @@ class CameraPreviewWeb extends core.WebPlugin {
82
86
  }
83
87
  }
84
88
  else {
85
- Promise.reject(new Error('camera already started'));
89
+ Promise.reject(new Error("camera already started"));
86
90
  }
87
91
  }
88
92
  stopStream(stream) {
@@ -93,7 +97,7 @@ class CameraPreviewWeb extends core.WebPlugin {
93
97
  }
94
98
  }
95
99
  async stop() {
96
- const video = document.getElementById('video');
100
+ const video = document.getElementById("video");
97
101
  if (video) {
98
102
  video.pause();
99
103
  this.stopStream(video.srcObject);
@@ -102,33 +106,33 @@ class CameraPreviewWeb extends core.WebPlugin {
102
106
  }
103
107
  async capture(options) {
104
108
  return new Promise((resolve, reject) => {
105
- const video = document.getElementById('video');
109
+ const video = document.getElementById("video");
106
110
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
107
- reject(new Error('camera is not running'));
111
+ reject(new Error("camera is not running"));
108
112
  return;
109
113
  }
110
114
  // video.width = video.offsetWidth;
111
115
  let base64EncodedImage;
112
116
  if (video && video.videoWidth > 0 && video.videoHeight > 0) {
113
- const canvas = document.createElement('canvas');
114
- const context = canvas.getContext('2d');
117
+ const canvas = document.createElement("canvas");
118
+ const context = canvas.getContext("2d");
115
119
  canvas.width = video.videoWidth;
116
120
  canvas.height = video.videoHeight;
117
121
  // flip horizontally back camera isn't used
118
122
  if (!this.isBackCamera) {
119
- context.translate(video.videoWidth, 0);
120
- context.scale(-1, 1);
123
+ context === null || context === void 0 ? void 0 : context.translate(video.videoWidth, 0);
124
+ context === null || context === void 0 ? void 0 : context.scale(-1, 1);
121
125
  }
122
- context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
123
- if ((options.format || 'jpeg') === 'jpeg') {
126
+ context === null || context === void 0 ? void 0 : context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
127
+ if ((options.format || "jpeg") === "jpeg") {
124
128
  base64EncodedImage = canvas
125
- .toDataURL('image/jpeg', (options.quality || 85) / 100.0)
126
- .replace('data:image/jpeg;base64,', '');
129
+ .toDataURL("image/jpeg", (options.quality || 85) / 100.0)
130
+ .replace("data:image/jpeg;base64,", "");
127
131
  }
128
132
  else {
129
133
  base64EncodedImage = canvas
130
- .toDataURL('image/png')
131
- .replace('data:image/png;base64,', '');
134
+ .toDataURL("image/png")
135
+ .replace("data:image/png;base64,", "");
132
136
  }
133
137
  }
134
138
  resolve({
@@ -140,27 +144,28 @@ class CameraPreviewWeb extends core.WebPlugin {
140
144
  return this.capture(_options);
141
145
  }
142
146
  async stopRecordVideo() {
143
- throw new Error('stopRecordVideo not supported under the web platform');
147
+ throw new Error("stopRecordVideo not supported under the web platform");
144
148
  }
145
149
  async startRecordVideo(_options) {
146
- throw new Error('startRecordVideo not supported under the web platform');
150
+ console.log("startRecordVideo", _options);
151
+ throw new Error("startRecordVideo not supported under the web platform");
147
152
  }
148
153
  async getSupportedFlashModes() {
149
- throw new Error('getSupportedFlashModes not supported under the web platform');
154
+ throw new Error("getSupportedFlashModes not supported under the web platform");
150
155
  }
151
156
  async getHorizontalFov() {
152
- throw new Error('getHorizontalFov not supported under the web platform');
157
+ throw new Error("getHorizontalFov not supported under the web platform");
153
158
  }
154
159
  async setFlashMode(_options) {
155
160
  throw new Error(`setFlashMode not supported under the web platform${_options}`);
156
161
  }
157
162
  async flip() {
158
- throw new Error('flip not supported under the web platform');
163
+ throw new Error("flip not supported under the web platform");
159
164
  }
160
165
  async setOpacity(_options) {
161
- const video = document.getElementById('video');
166
+ const video = document.getElementById("video");
162
167
  if (!!video && !!_options.opacity)
163
- video.style.setProperty('opacity', _options.opacity.toString());
168
+ video.style.setProperty("opacity", _options.opacity.toString());
164
169
  }
165
170
  }
166
171
 
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst CameraPreview = registerPlugin('CameraPreview', {\n web: () => import('./web').then(m => new m.CameraPreviewWeb()),\n});\nexport * from './definitions';\nexport { CameraPreview };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class CameraPreviewWeb extends WebPlugin {\n constructor() {\n super({\n name: 'CameraPreview',\n platforms: ['web'],\n });\n }\n async start(options) {\n var _a;\n await navigator.mediaDevices\n .getUserMedia({\n audio: !options.disableAudio,\n video: true,\n })\n .then((stream) => {\n // Stop any existing stream so we can request media with different constraints based on user input\n stream.getTracks().forEach(track => track.stop());\n })\n .catch((error) => {\n Promise.reject(error);\n });\n const video = document.getElementById('video');\n const parent = document.getElementById(options.parent);\n if (!video) {\n const videoElement = document.createElement('video');\n videoElement.id = 'video';\n videoElement.setAttribute('class', options.className || '');\n // Don't flip video feed if camera is rear facing\n if (options.position !== 'rear') {\n videoElement.setAttribute('style', '-webkit-transform: scaleX(-1); transform: scaleX(-1);');\n }\n const userAgent = navigator.userAgent.toLowerCase();\n const isSafari = userAgent.includes('safari') && !userAgent.includes('chrome');\n // Safari on iOS needs to have the autoplay, muted and playsinline attributes set for video.play() to be successful\n // Without these attributes videoElement.play() will throw a NotAllowedError\n // https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari\n if (isSafari) {\n videoElement.setAttribute('autoplay', 'true');\n videoElement.setAttribute('muted', 'true');\n videoElement.setAttribute('playsinline', 'true');\n }\n parent.appendChild(videoElement);\n if ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia) {\n const constraints = {\n video: {\n width: { ideal: options.width },\n height: { ideal: options.height },\n },\n };\n if (options.position === 'rear') {\n constraints.video.facingMode\n = 'environment';\n this.isBackCamera = true;\n }\n else {\n this.isBackCamera = false;\n }\n // eslint-disable-next-line ts/no-this-alias\n const self = this;\n await navigator.mediaDevices.getUserMedia(constraints).then((stream) => {\n if (document.getElementById('video')) {\n // video.src = window.URL.createObjectURL(stream);\n videoElement.srcObject = stream;\n videoElement.play();\n Promise.resolve({});\n }\n else {\n self.stopStream(stream);\n Promise.reject(new Error('camera already stopped'));\n }\n }, (err) => {\n Promise.reject(new Error(err));\n });\n }\n }\n else {\n Promise.reject(new Error('camera already started'));\n }\n }\n stopStream(stream) {\n if (stream) {\n const tracks = stream.getTracks();\n for (const track of tracks)\n track.stop();\n }\n }\n async stop() {\n const video = document.getElementById('video');\n if (video) {\n video.pause();\n this.stopStream(video.srcObject);\n video.remove();\n }\n }\n async capture(options) {\n return new Promise((resolve, reject) => {\n const video = document.getElementById('video');\n if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {\n reject(new Error('camera is not running'));\n return;\n }\n // video.width = video.offsetWidth;\n let base64EncodedImage;\n if (video && video.videoWidth > 0 && video.videoHeight > 0) {\n const canvas = document.createElement('canvas');\n const context = canvas.getContext('2d');\n canvas.width = video.videoWidth;\n canvas.height = video.videoHeight;\n // flip horizontally back camera isn't used\n if (!this.isBackCamera) {\n context.translate(video.videoWidth, 0);\n context.scale(-1, 1);\n }\n context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);\n if ((options.format || 'jpeg') === 'jpeg') {\n base64EncodedImage = canvas\n .toDataURL('image/jpeg', (options.quality || 85) / 100.0)\n .replace('data:image/jpeg;base64,', '');\n }\n else {\n base64EncodedImage = canvas\n .toDataURL('image/png')\n .replace('data:image/png;base64,', '');\n }\n }\n resolve({\n value: base64EncodedImage,\n });\n });\n }\n async captureSample(_options) {\n return this.capture(_options);\n }\n async stopRecordVideo() {\n throw new Error('stopRecordVideo not supported under the web platform');\n }\n async startRecordVideo(_options) {\n throw new Error('startRecordVideo not supported under the web platform');\n }\n async getSupportedFlashModes() {\n throw new Error('getSupportedFlashModes not supported under the web platform');\n }\n async getHorizontalFov() {\n throw new Error('getHorizontalFov not supported under the web platform');\n }\n async setFlashMode(_options) {\n throw new Error(`setFlashMode not supported under the web platform${_options}`);\n }\n async flip() {\n throw new Error('flip not supported under the web platform');\n }\n async setOpacity(_options) {\n const video = document.getElementById('video');\n if (!!video && !!_options.opacity)\n video.style.setProperty('opacity', _options.opacity.toString());\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,aAAa,GAAGA,mBAAc,CAAC,eAAe,EAAE;AACtD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,gBAAg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B,QAAQ,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;AACjF,KAAK;AACL,IAAI,MAAM,YAAY,CAAC,QAAQ,EAAE;AACjC,QAAQ,MAAM,IAAI,KAAK,CAAC,CAAC,iDAAiD,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;AACxF,KAAK;AACL,IAAI,MAAM,IAAI,GAAG;AACjB,QAAQ,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;AACrE,KAAK;AACL,IAAI,MAAM,UAAU,CAAC,QAAQ,EAAE;AAC/B,QAAQ,MAAM,KAAK,GAAG,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;AACvD,QAAQ,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO;AACzC,YAAY,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;AAC5E,KAAK;AACL;;;;;;;;;"}
1
+ {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from \"@capacitor/core\";\nconst CameraPreview = registerPlugin(\"CameraPreview\", {\n web: () => import(\"./web\").then((m) => new m.CameraPreviewWeb()),\n});\nexport * from \"./definitions\";\nexport { CameraPreview };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from \"@capacitor/core\";\nexport class CameraPreviewWeb extends WebPlugin {\n constructor() {\n super({\n name: \"CameraPreview\",\n platforms: [\"web\"],\n });\n /**\n * track which camera is used based on start options\n * used in capture\n */\n this.isBackCamera = false;\n }\n async start(options) {\n var _a;\n await navigator.mediaDevices\n .getUserMedia({\n audio: !options.disableAudio,\n video: true,\n })\n .then((stream) => {\n // Stop any existing stream so we can request media with different constraints based on user input\n stream.getTracks().forEach((track) => track.stop());\n })\n .catch((error) => {\n Promise.reject(error);\n });\n const video = document.getElementById(\"video\");\n const parent = document.getElementById((options === null || options === void 0 ? void 0 : options.parent) || \"\");\n if (!video) {\n const videoElement = document.createElement(\"video\");\n videoElement.id = \"video\";\n videoElement.setAttribute(\"class\", (options === null || options === void 0 ? void 0 : options.className) || \"\");\n // Don't flip video feed if camera is rear facing\n if (options.position !== \"rear\") {\n videoElement.setAttribute(\"style\", \"-webkit-transform: scaleX(-1); transform: scaleX(-1);\");\n }\n const userAgent = navigator.userAgent.toLowerCase();\n const isSafari = userAgent.includes(\"safari\") && !userAgent.includes(\"chrome\");\n // Safari on iOS needs to have the autoplay, muted and playsinline attributes set for video.play() to be successful\n // Without these attributes videoElement.play() will throw a NotAllowedError\n // https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari\n if (isSafari) {\n videoElement.setAttribute(\"autoplay\", \"true\");\n videoElement.setAttribute(\"muted\", \"true\");\n videoElement.setAttribute(\"playsinline\", \"true\");\n }\n parent === null || parent === void 0 ? void 0 : parent.appendChild(videoElement);\n if ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia) {\n const constraints = {\n video: {\n width: { ideal: options.width },\n height: { ideal: options.height },\n },\n };\n if (options.position === \"rear\") {\n constraints.video.facingMode =\n \"environment\";\n this.isBackCamera = true;\n }\n else {\n this.isBackCamera = false;\n }\n const self = this;\n await navigator.mediaDevices.getUserMedia(constraints).then((stream) => {\n if (document.getElementById(\"video\")) {\n // video.src = window.URL.createObjectURL(stream);\n videoElement.srcObject = stream;\n videoElement.play();\n Promise.resolve({});\n }\n else {\n self.stopStream(stream);\n Promise.reject(new Error(\"camera already stopped\"));\n }\n }, (err) => {\n Promise.reject(new Error(err));\n });\n }\n }\n else {\n Promise.reject(new Error(\"camera already started\"));\n }\n }\n stopStream(stream) {\n if (stream) {\n const tracks = stream.getTracks();\n for (const track of tracks)\n track.stop();\n }\n }\n async stop() {\n const video = document.getElementById(\"video\");\n if (video) {\n video.pause();\n this.stopStream(video.srcObject);\n video.remove();\n }\n }\n async capture(options) {\n return new Promise((resolve, reject) => {\n const video = document.getElementById(\"video\");\n if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {\n reject(new Error(\"camera is not running\"));\n return;\n }\n // video.width = video.offsetWidth;\n let base64EncodedImage;\n if (video && video.videoWidth > 0 && video.videoHeight > 0) {\n const canvas = document.createElement(\"canvas\");\n const context = canvas.getContext(\"2d\");\n canvas.width = video.videoWidth;\n canvas.height = video.videoHeight;\n // flip horizontally back camera isn't used\n if (!this.isBackCamera) {\n context === null || context === void 0 ? void 0 : context.translate(video.videoWidth, 0);\n context === null || context === void 0 ? void 0 : context.scale(-1, 1);\n }\n context === null || context === void 0 ? void 0 : context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);\n if ((options.format || \"jpeg\") === \"jpeg\") {\n base64EncodedImage = canvas\n .toDataURL(\"image/jpeg\", (options.quality || 85) / 100.0)\n .replace(\"data:image/jpeg;base64,\", \"\");\n }\n else {\n base64EncodedImage = canvas\n .toDataURL(\"image/png\")\n .replace(\"data:image/png;base64,\", \"\");\n }\n }\n resolve({\n value: base64EncodedImage,\n });\n });\n }\n async captureSample(_options) {\n return this.capture(_options);\n }\n async stopRecordVideo() {\n throw new Error(\"stopRecordVideo not supported under the web platform\");\n }\n async startRecordVideo(_options) {\n console.log(\"startRecordVideo\", _options);\n throw new Error(\"startRecordVideo not supported under the web platform\");\n }\n async getSupportedFlashModes() {\n throw new Error(\"getSupportedFlashModes not supported under the web platform\");\n }\n async getHorizontalFov() {\n throw new Error(\"getHorizontalFov not supported under the web platform\");\n }\n async setFlashMode(_options) {\n throw new Error(`setFlashMode not supported under the web platform${_options}`);\n }\n async flip() {\n throw new Error(\"flip not supported under the web platform\");\n }\n async setOpacity(_options) {\n const video = document.getElementById(\"video\");\n if (!!video && !!_options.opacity)\n video.style.setProperty(\"opacity\", _options.opacity.toString());\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,aAAa,GAAGA,mB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