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.

LargeVideoBackground.web.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // @flow
  2. import React, { Component } from 'react';
  3. /**
  4. * Constants to describe the dimensions of the video. Landscape videos
  5. * are wider than they are taller and portrait videos are taller than they
  6. * are wider. The dimensions will determine how {@code LargeVideoBackground}
  7. * will strech to fill its container.
  8. *
  9. * @type {Object}
  10. */
  11. export const ORIENTATION = {
  12. LANDSCAPE: 'landscape',
  13. PORTRAIT: 'portrait'
  14. };
  15. /**
  16. * A mapping of orientations to a class that should fit the
  17. * {@code LargeVideoBackground} into its container.
  18. *
  19. * @private
  20. * @type {Object}
  21. */
  22. const ORIENTATION_TO_CLASS = {
  23. [ORIENTATION.LANDSCAPE]: 'fit-full-width',
  24. [ORIENTATION.PORTRAIT]: 'fit-full-height'
  25. };
  26. /**
  27. * The type of the React {@code Component} props of
  28. * {@link LargeVideoBackgroundCanvas}.
  29. */
  30. type Props = {
  31. /**
  32. * Additional CSS class names to add to the root of the component.
  33. */
  34. className: String,
  35. /**
  36. * Whether or not the background should have its visibility hidden.
  37. */
  38. hidden: boolean,
  39. /**
  40. * Whether or not the video should display flipped horizontally, so left
  41. * becomes right and right becomes left.
  42. */
  43. mirror: boolean,
  44. /**
  45. * Whether the component should ensure full width of the video is displayed
  46. * (landscape) or full height (portrait).
  47. */
  48. orientationFit: string,
  49. /**
  50. * Whether or not to display a filter on the video to visually indicate a
  51. * problem with the video being displayed.
  52. */
  53. showLocalProblemFilter: boolean,
  54. /**
  55. * Whether or not to display a filter on the video to visually indicate a
  56. * problem with the video being displayed.
  57. */
  58. showRemoteProblemFilter: boolean,
  59. /**
  60. * The video stream to display.
  61. */
  62. videoElement: Object
  63. };
  64. /**
  65. * Implements a React Component which shows a video element intended to be used
  66. * as a background to fill the empty space of container with another video.
  67. *
  68. * @extends Component
  69. */
  70. export class LargeVideoBackground extends Component<Props> {
  71. _canvasEl: Object;
  72. _updateCanvasInterval: *;
  73. /**
  74. * Initializes new {@code LargeVideoBackground} instance.
  75. *
  76. * @param {*} props - The read-only properties with which the new instance
  77. * is to be initialized.
  78. */
  79. constructor(props: Props) {
  80. super(props);
  81. // Bind event handlers so they are only bound once per instance.
  82. this._setCanvasEl = this._setCanvasEl.bind(this);
  83. this._updateCanvas = this._updateCanvas.bind(this);
  84. }
  85. /**
  86. * If the canvas is not hidden, sets the initial interval to update the
  87. * image displayed in the canvas.
  88. *
  89. * @inheritdoc
  90. * @returns {void}
  91. */
  92. componentDidMount() {
  93. if (this.props.videoElement && !this.props.hidden) {
  94. this._updateCanvas();
  95. this._setUpdateCanvasInterval();
  96. }
  97. }
  98. /**
  99. * Starts or stops the interval to update the image displayed in the canvas.
  100. *
  101. * @inheritdoc
  102. */
  103. componentDidUpdate(prevProps: Props) {
  104. if (prevProps.hidden && !this.props.hidden) {
  105. this._clearCanvas();
  106. this._setUpdateCanvasInterval();
  107. }
  108. if ((!prevProps.hidden && this.props.hidden)
  109. || !this.props.videoElement) {
  110. this._clearCanvas();
  111. this._clearUpdateCanvasInterval();
  112. }
  113. }
  114. /**
  115. * Clears the interval for updating the image displayed in the canvas.
  116. *
  117. * @inheritdoc
  118. */
  119. componentWillUnmount() {
  120. this._clearUpdateCanvasInterval();
  121. }
  122. /**
  123. * Implements React's {@link Component#render()}.
  124. *
  125. * @inheritdoc
  126. * @returns {ReactElement}
  127. */
  128. render() {
  129. const {
  130. hidden,
  131. mirror,
  132. orientationFit,
  133. showLocalProblemFilter,
  134. showRemoteProblemFilter
  135. } = this.props;
  136. const orientationClass = orientationFit
  137. ? ORIENTATION_TO_CLASS[orientationFit] : '';
  138. const classNames = `large-video-background ${mirror ? 'flip-x' : ''} ${
  139. hidden ? 'invisible' : ''} ${orientationClass} ${
  140. showLocalProblemFilter ? 'videoProblemFilter' : ''} ${
  141. showRemoteProblemFilter ? 'remoteVideoProblemFilter' : ''}`;
  142. return (
  143. <div className = { classNames }>
  144. <canvas
  145. id = 'largeVideoBackground'
  146. ref = { this._setCanvasEl } />
  147. </div>
  148. );
  149. }
  150. /**
  151. * Removes any image displayed on the canvas.
  152. *
  153. * @private
  154. * @returns {void}
  155. */
  156. _clearCanvas() {
  157. const cavnasContext = this._canvasEl.getContext('2d');
  158. cavnasContext.clearRect(
  159. 0, 0, this._canvasEl.width, this._canvasEl.height);
  160. }
  161. /**
  162. * Clears the interval for updating the image displayed in the canvas.
  163. *
  164. * @private
  165. * @returns {void}
  166. */
  167. _clearUpdateCanvasInterval() {
  168. clearInterval(this._updateCanvasInterval);
  169. }
  170. _setCanvasEl: () => void;
  171. /**
  172. * Sets the instance variable for the component's canvas element so it can
  173. * be accessed directly for drawing on.
  174. *
  175. * @param {Object} element - The DOM element for the component's canvas.
  176. * @private
  177. * @returns {void}
  178. */
  179. _setCanvasEl(element) {
  180. this._canvasEl = element;
  181. }
  182. /**
  183. * Starts the interval for updating the image displayed in the canvas.
  184. *
  185. * @private
  186. * @returns {void}
  187. */
  188. _setUpdateCanvasInterval() {
  189. this._clearUpdateCanvasInterval();
  190. this._updateCanvasInterval = setInterval(this._updateCanvas, 200);
  191. }
  192. _updateCanvas: () => void;
  193. /**
  194. * Draws the current frame of the passed in video element onto the canvas.
  195. *
  196. * @private
  197. * @returns {void}
  198. */
  199. _updateCanvas() {
  200. const { videoElement } = this.props;
  201. const { videoWidth, videoHeight } = videoElement;
  202. const {
  203. height: canvasHeight,
  204. width: canvasWidth
  205. } = this._canvasEl;
  206. const cavnasContext = this._canvasEl.getContext('2d');
  207. if (this.props.orientationFit === ORIENTATION.LANDSCAPE) {
  208. const heightScaledToFit = (canvasWidth / videoWidth) * videoHeight;
  209. cavnasContext.drawImage(
  210. videoElement, 0, 0, canvasWidth, heightScaledToFit);
  211. } else {
  212. const widthScaledToFit = (canvasHeight / videoHeight) * videoWidth;
  213. cavnasContext.drawImage(
  214. videoElement, 0, 0, widthScaledToFit, canvasHeight);
  215. }
  216. }
  217. }