Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

functions.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. */
  59. export function resizeImage(base64image: any, width = 1920, height = 1080): Promise<string> {
  60. // In order to work on Firefox browser we need to handle the asynchronous nature of image loading; We need to use
  61. // a promise mechanism. The reason why it 'works' without this mechanism in Chrome is actually 'by accident' because
  62. // the image happens to be in the cache and the browser is able to deliver the uncompressed/decoded image
  63. // before using the image in the drawImage call.
  64. return new Promise(resolve => {
  65. const img = document.createElement('img');
  66. img.onload = function() {
  67. // Create an off-screen canvas.
  68. const canvas = document.createElement('canvas');
  69. // Set its dimension to target size.
  70. const context = canvas.getContext('2d');
  71. canvas.width = width;
  72. canvas.height = height;
  73. // Draw source image into the off-screen canvas.
  74. // TODO: keep aspect ratio and implement object-fit: cover.
  75. context?.drawImage(img as any, 0, 0, width, height);
  76. // Encode image to data-uri with base64 version of compressed image.
  77. resolve(canvas.toDataURL('image/jpeg', 0.5));
  78. };
  79. img.src = base64image;
  80. });
  81. }
  82. /**
  83. * Creating a wrapper for promises on a specific time interval.
  84. *
  85. * @param {number} milliseconds - The number of milliseconds to wait the specified
  86. * {@code promise} to settle before automatically rejecting the returned
  87. * {@code Promise}.
  88. * @param {Promise} promise - The {@code Promise} for which automatic rejecting
  89. * after the specified timeout is to be implemented.
  90. * @returns {Promise}
  91. */
  92. export function timeout(milliseconds: number, promise: Promise<any>): Promise<Object> {
  93. return new Promise((resolve, reject) => {
  94. setTimeout(() => {
  95. reject(new Error('408'));
  96. return;
  97. }, milliseconds);
  98. promise.then(resolve, reject);
  99. });
  100. }