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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * @param {Object} nextProps - The read-only React {@code Component} props
  103. * with which the new instance is to be initialized.
  104. */
  105. componentWillReceiveProps(nextProps: Props) {
  106. if (this.props.hidden && !nextProps.hidden) {
  107. this._clearCanvas();
  108. this._setUpdateCanvasInterval();
  109. }
  110. if ((!this.props.hidden && nextProps.hidden)
  111. || !nextProps.videoElement) {
  112. this._clearCanvas();
  113. this._clearUpdateCanvasInterval();
  114. }
  115. }
  116. /**
  117. * Clears the interval for updating the image displayed in the canvas.
  118. *
  119. * @inheritdoc
  120. */
  121. componentWillUnmount() {
  122. this._clearUpdateCanvasInterval();
  123. }
  124. /**
  125. * Implements React's {@link Component#render()}.
  126. *
  127. * @inheritdoc
  128. * @returns {ReactElement}
  129. */
  130. render() {
  131. const {
  132. hidden,
  133. mirror,
  134. orientationFit,
  135. showLocalProblemFilter,
  136. showRemoteProblemFilter
  137. } = this.props;
  138. const orientationClass = orientationFit
  139. ? ORIENTATION_TO_CLASS[orientationFit] : '';
  140. const classNames = `large-video-background ${mirror ? 'flip-x' : ''} ${
  141. hidden ? 'invisible' : ''} ${orientationClass} ${
  142. showLocalProblemFilter ? 'videoProblemFilter' : ''} ${
  143. showRemoteProblemFilter ? 'remoteVideoProblemFilter' : ''}`;
  144. return (
  145. <div className = { classNames }>
  146. <canvas
  147. id = 'largeVideoBackground'
  148. ref = { this._setCanvasEl } />
  149. </div>
  150. );
  151. }
  152. /**
  153. * Removes any image displayed on the canvas.
  154. *
  155. * @private
  156. * @returns {void}
  157. */
  158. _clearCanvas() {
  159. const cavnasContext = this._canvasEl.getContext('2d');
  160. cavnasContext.clearRect(
  161. 0, 0, this._canvasEl.width, this._canvasEl.height);
  162. }
  163. /**
  164. * Clears the interval for updating the image displayed in the canvas.
  165. *
  166. * @private
  167. * @returns {void}
  168. */
  169. _clearUpdateCanvasInterval() {
  170. clearInterval(this._updateCanvasInterval);
  171. }
  172. _setCanvasEl: () => void;
  173. /**
  174. * Sets the instance variable for the component's canvas element so it can
  175. * be accessed directly for drawing on.
  176. *
  177. * @param {Object} element - The DOM element for the component's canvas.
  178. * @private
  179. * @returns {void}
  180. */
  181. _setCanvasEl(element) {
  182. this._canvasEl = element;
  183. }
  184. /**
  185. * Starts the interval for updating the image displayed in the canvas.
  186. *
  187. * @private
  188. * @returns {void}
  189. */
  190. _setUpdateCanvasInterval() {
  191. this._clearUpdateCanvasInterval();
  192. this._updateCanvasInterval = setInterval(this._updateCanvas, 200);
  193. }
  194. _updateCanvas: () => void;
  195. /**
  196. * Draws the current frame of the passed in video element onto the canvas.
  197. *
  198. * @private
  199. * @returns {void}
  200. */
  201. _updateCanvas() {
  202. const { videoElement } = this.props;
  203. const { videoWidth, videoHeight } = videoElement;
  204. const {
  205. height: canvasHeight,
  206. width: canvasWidth
  207. } = this._canvasEl;
  208. const cavnasContext = this._canvasEl.getContext('2d');
  209. if (this.props.orientationFit === ORIENTATION.LANDSCAPE) {
  210. const heightScaledToFit = (canvasWidth / videoWidth) * videoHeight;
  211. cavnasContext.drawImage(
  212. videoElement, 0, 0, canvasWidth, heightScaledToFit);
  213. } else {
  214. const widthScaledToFit = (canvasHeight / videoHeight) * videoWidth;
  215. cavnasContext.drawImage(
  216. videoElement, 0, 0, widthScaledToFit, canvasHeight);
  217. }
  218. }
  219. }