playback.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Constructor for the playback component, which displays
  3. * play/pause/progress controls.
  4. *
  5. * @param {HTMLElement} container The component will append
  6. * itself to this
  7. * @param {function} progressCheck A method which will be
  8. * called frequently to get the current progress on a range
  9. * of 0-1
  10. */
  11. export default class Playback {
  12. constructor( container, progressCheck ) {
  13. // Cosmetics
  14. this.diameter = 100;
  15. this.diameter2 = this.diameter/2;
  16. this.thickness = 6;
  17. // Flags if we are currently playing
  18. this.playing = false;
  19. // Current progress on a 0-1 range
  20. this.progress = 0;
  21. // Used to loop the animation smoothly
  22. this.progressOffset = 1;
  23. this.container = container;
  24. this.progressCheck = progressCheck;
  25. this.canvas = document.createElement( 'canvas' );
  26. this.canvas.className = 'playback';
  27. this.canvas.width = this.diameter;
  28. this.canvas.height = this.diameter;
  29. this.canvas.style.width = this.diameter2 + 'px';
  30. this.canvas.style.height = this.diameter2 + 'px';
  31. this.context = this.canvas.getContext( '2d' );
  32. this.container.appendChild( this.canvas );
  33. this.render();
  34. }
  35. setPlaying( value ) {
  36. const wasPlaying = this.playing;
  37. this.playing = value;
  38. // Start repainting if we weren't already
  39. if( !wasPlaying && this.playing ) {
  40. this.animate();
  41. }
  42. else {
  43. this.render();
  44. }
  45. }
  46. animate() {
  47. const progressBefore = this.progress;
  48. this.progress = this.progressCheck();
  49. // When we loop, offset the progress so that it eases
  50. // smoothly rather than immediately resetting
  51. if( progressBefore > 0.8 && this.progress < 0.2 ) {
  52. this.progressOffset = this.progress;
  53. }
  54. this.render();
  55. if( this.playing ) {
  56. requestAnimationFrame( this.animate.bind( this ) );
  57. }
  58. }
  59. /**
  60. * Renders the current progress and playback state.
  61. */
  62. render() {
  63. let progress = this.playing ? this.progress : 0,
  64. radius = ( this.diameter2 ) - this.thickness,
  65. x = this.diameter2,
  66. y = this.diameter2,
  67. iconSize = 28;
  68. // Ease towards 1
  69. this.progressOffset += ( 1 - this.progressOffset ) * 0.1;
  70. const endAngle = ( - Math.PI / 2 ) + ( progress * ( Math.PI * 2 ) );
  71. const startAngle = ( - Math.PI / 2 ) + ( this.progressOffset * ( Math.PI * 2 ) );
  72. this.context.save();
  73. this.context.clearRect( 0, 0, this.diameter, this.diameter );
  74. // Solid background color
  75. this.context.beginPath();
  76. this.context.arc( x, y, radius + 4, 0, Math.PI * 2, false );
  77. this.context.fillStyle = 'rgba( 0, 0, 0, 0.4 )';
  78. this.context.fill();
  79. // Draw progress track
  80. this.context.beginPath();
  81. this.context.arc( x, y, radius, 0, Math.PI * 2, false );
  82. this.context.lineWidth = this.thickness;
  83. this.context.strokeStyle = 'rgba( 255, 255, 255, 0.2 )';
  84. this.context.stroke();
  85. if( this.playing ) {
  86. // Draw progress on top of track
  87. this.context.beginPath();
  88. this.context.arc( x, y, radius, startAngle, endAngle, false );
  89. this.context.lineWidth = this.thickness;
  90. this.context.strokeStyle = '#fff';
  91. this.context.stroke();
  92. }
  93. this.context.translate( x - ( iconSize / 2 ), y - ( iconSize / 2 ) );
  94. // Draw play/pause icons
  95. if( this.playing ) {
  96. this.context.fillStyle = '#fff';
  97. this.context.fillRect( 0, 0, iconSize / 2 - 4, iconSize );
  98. this.context.fillRect( iconSize / 2 + 4, 0, iconSize / 2 - 4, iconSize );
  99. }
  100. else {
  101. this.context.beginPath();
  102. this.context.translate( 4, 0 );
  103. this.context.moveTo( 0, 0 );
  104. this.context.lineTo( iconSize - 4, iconSize / 2 );
  105. this.context.lineTo( 0, iconSize );
  106. this.context.fillStyle = '#fff';
  107. this.context.fill();
  108. }
  109. this.context.restore();
  110. }
  111. on( type, listener ) {
  112. this.canvas.addEventListener( type, listener, false );
  113. }
  114. off( type, listener ) {
  115. this.canvas.removeEventListener( type, listener, false );
  116. }
  117. destroy() {
  118. this.playing = false;
  119. if( this.canvas.parentNode ) {
  120. this.container.removeChild( this.canvas );
  121. }
  122. }
  123. }