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.

LargeVideoBackgroundCanvas.web.js 6.1KB

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