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

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