您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AudioLevelIndicator.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. /**
  4. * The number of dots to display in AudioLevelIndicator.
  5. *
  6. * IMPORTANT: AudioLevelIndicator assumes that this is an odd number.
  7. */
  8. const AUDIO_LEVEL_DOTS = 5;
  9. /**
  10. * The index of the dot that is at the direct middle of all other dots.
  11. */
  12. const CENTER_DOT_INDEX = Math.floor(AUDIO_LEVEL_DOTS / 2);
  13. /**
  14. * The type of the React {@code Component} props of {@link AudioLevelIndicator}.
  15. */
  16. type Props = {
  17. /**
  18. * The current audio level to display. The value should be a number between
  19. * 0 and 1.
  20. */
  21. audioLevel: number
  22. };
  23. /**
  24. * Creates a ReactElement responsible for drawing audio levels.
  25. *
  26. * @extends {Component}
  27. */
  28. class AudioLevelIndicator extends Component<Props> {
  29. /**
  30. * Implements React's {@link Component#render()}.
  31. *
  32. * @inheritdoc
  33. * @returns {ReactElement}
  34. */
  35. render() {
  36. // First make sure we are sensitive enough.
  37. const audioLevel = Math.min(this.props.audioLevel * 1.2, 1);
  38. // Let's now stretch the audio level over the number of dots we have.
  39. const stretchedAudioLevel = AUDIO_LEVEL_DOTS * audioLevel;
  40. const audioLevelDots = [];
  41. for (let i = 0; i < AUDIO_LEVEL_DOTS; i++) {
  42. const distanceFromCenter = CENTER_DOT_INDEX - i;
  43. const audioLevelFromCenter
  44. = stretchedAudioLevel - Math.abs(distanceFromCenter);
  45. const cappedOpacity = Math.min(
  46. 1, Math.max(0, audioLevelFromCenter));
  47. let className;
  48. if (distanceFromCenter === 0) {
  49. className = 'audiodot-middle';
  50. } else if (distanceFromCenter < 0) {
  51. className = 'audiodot-top';
  52. } else {
  53. className = 'audiodot-bottom';
  54. }
  55. audioLevelDots.push(
  56. <span
  57. className = { className }
  58. key = { i }
  59. style = {{ opacity: cappedOpacity }} />
  60. );
  61. }
  62. return (
  63. <span className = 'audioindicator in-react'>
  64. { audioLevelDots }
  65. </span>
  66. );
  67. }
  68. }
  69. export default AudioLevelIndicator;