浏览代码

fix(virtual-background): Fixes upload virtual background on Firefox

Fixes: #8892
j8
Tudor D. Pop 4 年前
父节点
当前提交
dfd33521bf
没有帐户链接到提交者的电子邮件
共有 1 个文件被更改,包括 30 次插入22 次删除
  1. 30
    22
      react/features/virtual-background/functions.js

+ 30
- 22
react/features/virtual-background/functions.js 查看文件

25
  * @param {Blob} blob - The link to add info with.
25
  * @param {Blob} blob - The link to add info with.
26
  * @returns {Promise<string>}
26
  * @returns {Promise<string>}
27
  */
27
  */
28
-export const blobToData = (blob: Blob): Promise<string> => new Promise(resolve => {
29
-    const reader = new FileReader();
28
+export const blobToData = (blob: Blob): Promise<string> =>
29
+    new Promise(resolve => {
30
+        const reader = new FileReader();
30
 
31
 
31
-    reader.onloadend = () => resolve(reader.result.toString());
32
-    reader.readAsDataURL(blob);
33
-});
32
+        reader.onloadend = () => resolve(reader.result.toString());
33
+        reader.readAsDataURL(blob);
34
+    });
34
 
35
 
35
 /**
36
 /**
36
  * Convert blob to base64.
37
  * Convert blob to base64.
52
  * @param {Object} base64image - Base64 image extraction.
53
  * @param {Object} base64image - Base64 image extraction.
53
  * @param {number} width - Value for resizing the image width.
54
  * @param {number} width - Value for resizing the image width.
54
  * @param {number} height - Value for resizing the image height.
55
  * @param {number} height - Value for resizing the image height.
55
- * @returns {Object} Returns the canvas output.
56
+ * @returns {Promise<string>}
56
  *
57
  *
57
  */
58
  */
58
-export async function resizeImage(base64image: any, width: number = 1920, height: number = 1080) {
59
-    const img = document.createElement('img');
59
+export function resizeImage(base64image: any, width: number = 1920, height: number = 1080): Promise<string> {
60
+
61
+    // In order to work on Firefox browser we need to handle the asynchronous nature of image loading;  We need to use
62
+    // a promise mechanism. The reason why it 'works' without this mechanism in Chrome is actually 'by accident' because
63
+    // the image happens to be in the cache and the browser is able to deliver the uncompressed/decoded image
64
+    // before using the image in the drawImage call.
65
+    return new Promise(resolve => {
66
+        const img = document.createElement('img');
60
 
67
 
61
-    img.src = base64image;
62
-    /* eslint-disable no-empty-function */
63
-    img.onload = await function() {};
68
+        img.onload = function() {
69
+            // Create an off-screen canvas.
70
+            const canvas = document.createElement('canvas');
64
 
71
 
65
-    // Create an off-screen canvas.
66
-    const canvas = document.createElement('canvas');
67
-    const ctx = canvas.getContext('2d');
72
+            // Set its dimension to target size.
73
+            const context = canvas.getContext('2d');
68
 
74
 
69
-    // Set its dimension to target size.
70
-    canvas.width = width;
71
-    canvas.height = height;
75
+            canvas.width = width;
76
+            canvas.height = height;
72
 
77
 
73
-    // Draw source image into the off-screen canvas.
74
-    // TODO: keep aspect ratio and implement object-fit: cover.
75
-    ctx.drawImage(img, 0, 0, width, height);
78
+            // Draw source image into the off-screen canvas.
79
+            // TODO: keep aspect ratio and implement object-fit: cover.
80
+            context.drawImage(img, 0, 0, width, height);
76
 
81
 
77
-    // Encode image to data-uri with base64 version of compressed image.
78
-    return canvas.toDataURL('image/jpeg', 0.5);
82
+            // Encode image to data-uri with base64 version of compressed image.
83
+            resolve(canvas.toDataURL('image/jpeg', 0.5));
84
+        };
85
+        img.src = base64image;
86
+    });
79
 }
87
 }

正在加载...
取消
保存