Guest User

Untitled

a guest
Nov 28th, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. /**
  2. * Full Background Video
  3. *
  4. * More info on Audio/Video Media Events/Attributes/Methods
  5. * - https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events
  6. * - http://www.w3schools.com/tags/ref_av_dom.asp
  7. */
  8.  
  9. (function (global) {
  10. "use strict";
  11.  
  12. // Define Bideo constructor on the global object
  13. global.Bideo = function () {
  14.  
  15. // Plugin options
  16. this.opt = null;
  17. // The Video element
  18. this.videoEl = null;
  19.  
  20. // Approximate Loading Rate
  21. //
  22. // The value will be a number like 0.8
  23. // which means to load 4 seconds of the video
  24. // it takes 5 seconds. If the number is super low
  25. // like 0.2 (regular 3g connections) then you can
  26. // decide whether to play the video or not.
  27. // This behaviour will be controller with
  28. // the `acceptableLoadingRate` option.
  29. this.approxLoadingRate = null;
  30.  
  31. // Methods to which `this` will be bound
  32. this._resize = null;
  33. this._progress = null;
  34.  
  35. // Time at which video is initialized
  36. this.startTime = null;
  37.  
  38. this.onLoadCalled = false;
  39.  
  40. // Initialize and setup the video in DOM`
  41. this.init = function (opt) {
  42. // If not set then set to an empty object
  43. this.opt = opt = opt || {};
  44.  
  45. var self = this;
  46.  
  47. self._resize = self.resize.bind(this);
  48.  
  49. // Video element
  50. self.videoEl = opt.videoEl;
  51.  
  52. // Meta data event
  53. self.videoEl.addEventListener('loadedmetadata', self._resize, false);
  54.  
  55. // Fired when enough has been buffered to begin the video
  56. // self.videoEl.readyState === 4 (HAVE_ENOUGH_DATA)
  57. self.videoEl.addEventListener('canplay', function () {
  58. // Play the video when enough has been buffered
  59. if (!self.opt.isMobile) {
  60. self.opt.onLoad && self.opt.onLoad();
  61. if (self.opt.autoplay !== false) self.videoEl.play();
  62. }
  63. });
  64.  
  65. // If resizing is required (resize video as window/container resizes)
  66. if (self.opt.resize) {
  67. global.addEventListener('resize', self._resize, false);
  68. }
  69.  
  70. // Start time of video initialization
  71. this.startTime = (new Date()).getTime();
  72.  
  73. // Create `source` for video
  74. this.opt.src.forEach(function (srcOb, i, arr) {
  75. var key
  76. , val
  77. , source = document.createElement('source');
  78.  
  79. // Set all the attribute key=val supplied in `src` option
  80. for (key in srcOb) {
  81. if (srcOb.hasOwnProperty(key)) {
  82. val = srcOb[key];
  83.  
  84. source.setAttribute(key, val);
  85. }
  86. }
  87.  
  88. self.videoEl.appendChild(source);
  89. });
  90.  
  91. if (self.opt.isMobile) {
  92. if (self.opt.playButton) {
  93. self.opt.videoEl.addEventListener('timeupdate', function () {
  94. if (!self.onLoadCalled) {
  95. self.opt.onLoad && self.opt.onLoad();
  96. self.onLoadCalled = true;
  97. }
  98. });
  99.  
  100.  
  101. self.opt.playButton.addEventListener('click', function () {
  102. self.opt.pauseButton.style.display = 'inline-block';
  103. this.style.display = 'none';
  104.  
  105. self.videoEl.play();
  106. }, false);
  107.  
  108. self.opt.pauseButton.addEventListener('click', function () {
  109. this.style.display = 'none';
  110. self.opt.playButton.style.display = 'inline-block';
  111.  
  112. self.videoEl.pause();
  113. }, false);
  114. }
  115. }
  116.  
  117. return;
  118. };
  119.  
  120. // Called once video metadata is available
  121. //
  122. // Also called when window/container is resized
  123. this.resize = function () {
  124. // IE/Edge still don't support object-fit: cover
  125. if ('object-fit' in document.body.style) return;
  126.  
  127. // Video's intrinsic dimensions
  128. var w = this.videoEl.videoWidth
  129. , h = this.videoEl.videoHeight;
  130.  
  131. // Intrinsic ratio
  132. // Will be more than 1 if W > H and less if H > W
  133. var videoRatio = (w / h).toFixed(2);
  134.  
  135. // Get the container DOM element and its styles
  136. //
  137. // Also calculate the min dimensions required (this will be
  138. // the container dimentions)
  139. var container = this.opt.container
  140. , containerStyles = global.getComputedStyle(container)
  141. , minW = parseInt( containerStyles.getPropertyValue('width') )
  142. , minH = parseInt( containerStyles.getPropertyValue('height') );
  143.  
  144. // If !border-box then add paddings to width and height
  145. if (containerStyles.getPropertyValue('box-sizing') !== 'border-box') {
  146. var paddingTop = containerStyles.getPropertyValue('padding-top')
  147. , paddingBottom = containerStyles.getPropertyValue('padding-bottom')
  148. , paddingLeft = containerStyles.getPropertyValue('padding-left')
  149. , paddingRight = containerStyles.getPropertyValue('padding-right');
  150.  
  151. paddingTop = parseInt(paddingTop);
  152. paddingBottom = parseInt(paddingBottom);
  153. paddingLeft = parseInt(paddingLeft);
  154. paddingRight = parseInt(paddingRight);
  155.  
  156. minW += paddingLeft + paddingRight;
  157. minH += paddingTop + paddingBottom;
  158. }
  159.  
  160. // What's the min:intrinsic dimensions
  161. //
  162. // The idea is to get which of the container dimension
  163. // has a higher value when compared with the equivalents
  164. // of the video. Imagine a 1200x700 container and
  165. // 1000x500 video. Then in order to find the right balance
  166. // and do minimum scaling, we have to find the dimension
  167. // with higher ratio.
  168. //
  169. // Ex: 1200/1000 = 1.2 and 700/500 = 1.4 - So it is best to
  170. // scale 500 to 700 and then calculate what should be the
  171. // right width. If we scale 1000 to 1200 then the height
  172. // will become 600 proportionately.
  173. var widthRatio = minW / w;
  174. var heightRatio = minH / h;
  175.  
  176. // Whichever ratio is more, the scaling
  177. // has to be done over that dimension
  178. if (widthRatio > heightRatio) {
  179. var new_width = minW;
  180. var new_height = Math.ceil( new_width / videoRatio );
  181. } else {
  182. var new_height = minH;
  183. var new_width = Math.ceil( new_height * videoRatio );
  184. }
  185.  
  186. this.videoEl.style.width = new_width + 'px';
  187. this.videoEl.style.height = new_height + 'px';
  188. };
  189.  
  190. };
  191.  
  192. }(window));
Add Comment
Please, Sign In to add comment