You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { IReduxState } from '../app/types';
  2. let filterSupport: boolean | undefined;
  3. /**
  4. * Checks context filter support.
  5. *
  6. * @returns {boolean} True if the filter is supported and false if the filter is not supported by the browser.
  7. */
  8. export function checkBlurSupport() {
  9. if (typeof filterSupport === 'undefined') {
  10. const canvas = document.createElement('canvas');
  11. const ctx = canvas.getContext('2d');
  12. filterSupport = typeof ctx?.filter !== 'undefined';
  13. canvas.remove();
  14. }
  15. return filterSupport;
  16. }
  17. /**
  18. * Checks if virtual background is enabled.
  19. *
  20. * @param {IReduxState} state - The state of the app.
  21. * @returns {boolean} True if virtual background is enabled and false if virtual background is disabled.
  22. */
  23. export function checkVirtualBackgroundEnabled(state: IReduxState) {
  24. return state['features/base/config'].disableVirtualBackground !== true;
  25. }
  26. /**
  27. * Convert blob to base64.
  28. *
  29. * @param {Blob} blob - The link to add info with.
  30. * @returns {Promise<string>}
  31. */
  32. export const blobToData = (blob: Blob) =>
  33. new Promise(resolve => {
  34. const reader = new FileReader();
  35. reader.onloadend = () => resolve(reader.result?.toString());
  36. reader.readAsDataURL(blob);
  37. });
  38. /**
  39. * Convert blob to base64.
  40. *
  41. * @param {string} url - The image url.
  42. * @returns {Object} - Returns the converted blob to base64.
  43. */
  44. export const toDataURL = async (url: string) => {
  45. const response = await fetch(url);
  46. const blob = await response.blob();
  47. const resData = await blobToData(blob);
  48. return resData;
  49. };
  50. /**
  51. * Resize image and adjust original aspect ratio.
  52. *
  53. * @param {Object} base64image - Base64 image extraction.
  54. * @param {number} width - Value for resizing the image width.
  55. * @param {number} height - Value for resizing the image height.
  56. * @returns {Promise<string>}
  57. */
  58. export function resizeImage(base64image: any, width = 1920, height = 1080): Promise<string> {
  59. // In order to work on Firefox browser we need to handle the asynchronous nature of image loading; We need to use
  60. // a promise mechanism. The reason why it 'works' without this mechanism in Chrome is actually 'by accident' because
  61. // the image happens to be in the cache and the browser is able to deliver the uncompressed/decoded image
  62. // before using the image in the drawImage call.
  63. return new Promise(resolve => {
  64. const img = document.createElement('img');
  65. img.onload = function() {
  66. // Create an off-screen canvas.
  67. const canvas = document.createElement('canvas');
  68. // Set its dimension to target size.
  69. const context = canvas.getContext('2d');
  70. canvas.width = width;
  71. canvas.height = height;
  72. // Draw source image into the off-screen canvas.
  73. // TODO: keep aspect ratio and implement object-fit: cover.
  74. context?.drawImage(img as any, 0, 0, width, height);
  75. // Encode image to data-uri with base64 version of compressed image.
  76. resolve(canvas.toDataURL('image/jpeg', 0.5));
  77. };
  78. img.src = base64image;
  79. });
  80. }
  81. /**
  82. * Creating a wrapper for promises on a specific time interval.
  83. *
  84. * @param {number} milliseconds - The number of milliseconds to wait the specified
  85. * {@code promise} to settle before automatically rejecting the returned
  86. * {@code Promise}.
  87. * @param {Promise} promise - The {@code Promise} for which automatic rejecting
  88. * after the specified timeout is to be implemented.
  89. * @returns {Promise}
  90. */
  91. export function timeout(milliseconds: number, promise: Promise<any>): Promise<Object> {
  92. return new Promise((resolve, reject) => {
  93. setTimeout(() => {
  94. reject(new Error('408'));
  95. return;
  96. }, milliseconds);
  97. promise.then(resolve, reject);
  98. });
  99. }