backgrounds.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. import { toArray } from '../utils/util.js'
  2. import { colorToRgb, colorBrightness } from '../utils/color.js'
  3. /**
  4. * Creates and updates slide backgrounds.
  5. */
  6. export default class Backgrounds {
  7. constructor( Reveal ) {
  8. this.Reveal = Reveal;
  9. }
  10. /**
  11. * Creates the slide background elements and appends them
  12. * to the background container. One element is created per
  13. * slide no matter if the given slide has visible background.
  14. */
  15. create() {
  16. let printMode = this.Reveal.isPrintingPDF();
  17. let backgroundElement = this.Reveal.getBackgroundsElement();
  18. // Clear prior backgrounds
  19. backgroundElement.innerHTML = '';
  20. backgroundElement.classList.add( 'no-transition' );
  21. // Iterate over all horizontal slides
  22. this.Reveal.getHorizontalSlides().forEach( slideh => {
  23. let backgroundStack = this.createBackground( slideh, backgroundElement );
  24. // Iterate over all vertical slides
  25. toArray( slideh.querySelectorAll( 'section' ) ).forEach( slidev => {
  26. this.createBackground( slidev, backgroundStack );
  27. backgroundStack.classList.add( 'stack' );
  28. } );
  29. } );
  30. // Add parallax background if specified
  31. if( this.Reveal.getConfig().parallaxBackgroundImage ) {
  32. backgroundElement.style.backgroundImage = 'url("' + this.Reveal.getConfig().parallaxBackgroundImage + '")';
  33. backgroundElement.style.backgroundSize = this.Reveal.getConfig().parallaxBackgroundSize;
  34. backgroundElement.style.backgroundRepeat = this.Reveal.getConfig().parallaxBackgroundRepeat;
  35. backgroundElement.style.backgroundPosition = this.Reveal.getConfig().parallaxBackgroundPosition;
  36. // Make sure the below properties are set on the element - these properties are
  37. // needed for proper transitions to be set on the element via CSS. To remove
  38. // annoying background slide-in effect when the presentation starts, apply
  39. // these properties after short time delay
  40. setTimeout( () => {
  41. this.Reveal.getRevealElement().classList.add( 'has-parallax-background' );
  42. }, 1 );
  43. }
  44. else {
  45. backgroundElement.style.backgroundImage = '';
  46. this.Reveal.getRevealElement().classList.remove( 'has-parallax-background' );
  47. }
  48. }
  49. /**
  50. * Creates a background for the given slide.
  51. *
  52. * @param {HTMLElement} slide
  53. * @param {HTMLElement} container The element that the background
  54. * should be appended to
  55. * @return {HTMLElement} New background div
  56. */
  57. createBackground( slide, container ) {
  58. // Main slide background element
  59. let element = document.createElement( 'div' );
  60. element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' );
  61. // Inner background element that wraps images/videos/iframes
  62. let contentElement = document.createElement( 'div' );
  63. contentElement.className = 'slide-background-content';
  64. element.appendChild( contentElement );
  65. container.appendChild( element );
  66. slide.slideBackgroundElement = element;
  67. slide.slideBackgroundContentElement = contentElement;
  68. // Syncs the background to reflect all current background settings
  69. this.sync( slide );
  70. return element;
  71. }
  72. /**
  73. * Renders all of the visual properties of a slide background
  74. * based on the various background attributes.
  75. *
  76. * @param {HTMLElement} slide
  77. */
  78. sync( slide ) {
  79. let element = slide.slideBackgroundElement,
  80. contentElement = slide.slideBackgroundContentElement;
  81. // Reset the prior background state in case this is not the
  82. // initial sync
  83. slide.classList.remove( 'has-dark-background' );
  84. slide.classList.remove( 'has-light-background' );
  85. element.removeAttribute( 'data-loaded' );
  86. element.removeAttribute( 'data-background-hash' );
  87. element.removeAttribute( 'data-background-size' );
  88. element.removeAttribute( 'data-background-transition' );
  89. element.style.backgroundColor = '';
  90. contentElement.style.backgroundSize = '';
  91. contentElement.style.backgroundRepeat = '';
  92. contentElement.style.backgroundPosition = '';
  93. contentElement.style.backgroundImage = '';
  94. contentElement.style.opacity = '';
  95. contentElement.innerHTML = '';
  96. let data = {
  97. background: slide.getAttribute( 'data-background' ),
  98. backgroundSize: slide.getAttribute( 'data-background-size' ),
  99. backgroundImage: slide.getAttribute( 'data-background-image' ),
  100. backgroundVideo: slide.getAttribute( 'data-background-video' ),
  101. backgroundIframe: slide.getAttribute( 'data-background-iframe' ),
  102. backgroundColor: slide.getAttribute( 'data-background-color' ),
  103. backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
  104. backgroundPosition: slide.getAttribute( 'data-background-position' ),
  105. backgroundTransition: slide.getAttribute( 'data-background-transition' ),
  106. backgroundOpacity: slide.getAttribute( 'data-background-opacity' )
  107. };
  108. if( data.background ) {
  109. // Auto-wrap image urls in url(...)
  110. if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(svg|png|jpg|jpeg|gif|bmp)([?#\s]|$)/gi.test( data.background ) ) {
  111. slide.setAttribute( 'data-background-image', data.background );
  112. }
  113. else {
  114. element.style.background = data.background;
  115. }
  116. }
  117. // Create a hash for this combination of background settings.
  118. // This is used to determine when two slide backgrounds are
  119. // the same.
  120. if( data.background || data.backgroundColor || data.backgroundImage || data.backgroundVideo || data.backgroundIframe ) {
  121. element.setAttribute( 'data-background-hash', data.background +
  122. data.backgroundSize +
  123. data.backgroundImage +
  124. data.backgroundVideo +
  125. data.backgroundIframe +
  126. data.backgroundColor +
  127. data.backgroundRepeat +
  128. data.backgroundPosition +
  129. data.backgroundTransition +
  130. data.backgroundOpacity );
  131. }
  132. // Additional and optional background properties
  133. if( data.backgroundSize ) element.setAttribute( 'data-background-size', data.backgroundSize );
  134. if( data.backgroundColor ) element.style.backgroundColor = data.backgroundColor;
  135. if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition );
  136. if( slide.hasAttribute( 'data-preload' ) ) element.setAttribute( 'data-preload', '' );
  137. // Background image options are set on the content wrapper
  138. if( data.backgroundSize ) contentElement.style.backgroundSize = data.backgroundSize;
  139. if( data.backgroundRepeat ) contentElement.style.backgroundRepeat = data.backgroundRepeat;
  140. if( data.backgroundPosition ) contentElement.style.backgroundPosition = data.backgroundPosition;
  141. if( data.backgroundOpacity ) contentElement.style.opacity = data.backgroundOpacity;
  142. // If this slide has a background color, we add a class that
  143. // signals if it is light or dark. If the slide has no background
  144. // color, no class will be added
  145. let contrastColor = data.backgroundColor;
  146. // If no bg color was found, check the computed background
  147. if( !contrastColor ) {
  148. let computedBackgroundStyle = window.getComputedStyle( element );
  149. if( computedBackgroundStyle && computedBackgroundStyle.backgroundColor ) {
  150. contrastColor = computedBackgroundStyle.backgroundColor;
  151. }
  152. }
  153. if( contrastColor ) {
  154. let rgb = colorToRgb( contrastColor );
  155. // Ignore fully transparent backgrounds. Some browsers return
  156. // rgba(0,0,0,0) when reading the computed background color of
  157. // an element with no background
  158. if( rgb && rgb.a !== 0 ) {
  159. if( colorBrightness( contrastColor ) < 128 ) {
  160. slide.classList.add( 'has-dark-background' );
  161. }
  162. else {
  163. slide.classList.add( 'has-light-background' );
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * Updates the background elements to reflect the current
  170. * slide.
  171. *
  172. * @param {boolean} includeAll If true, the backgrounds of
  173. * all vertical slides (not just the present) will be updated.
  174. */
  175. update( includeAll = false ) {
  176. let currentSlide = this.Reveal.getCurrentSlide();
  177. let backgroundElement = this.Reveal.getBackgroundsElement();
  178. let indices = this.Reveal.getIndices();
  179. let currentBackground = null;
  180. // Reverse past/future classes when in RTL mode
  181. let horizontalPast = this.Reveal.getConfig().rtl ? 'future' : 'past',
  182. horizontalFuture = this.Reveal.getConfig().rtl ? 'past' : 'future';
  183. // Update the classes of all backgrounds to match the
  184. // states of their slides (past/present/future)
  185. toArray( backgroundElement.childNodes ).forEach( ( backgroundh, h ) => {
  186. backgroundh.classList.remove( 'past', 'present', 'future' );
  187. if( h < indices.h ) {
  188. backgroundh.classList.add( horizontalPast );
  189. }
  190. else if ( h > indices.h ) {
  191. backgroundh.classList.add( horizontalFuture );
  192. }
  193. else {
  194. backgroundh.classList.add( 'present' );
  195. // Store a reference to the current background element
  196. currentBackground = backgroundh;
  197. }
  198. if( includeAll || h === indices.h ) {
  199. toArray( backgroundh.querySelectorAll( '.slide-background' ) ).forEach( ( backgroundv, v ) => {
  200. backgroundv.classList.remove( 'past', 'present', 'future' );
  201. if( v < indices.v ) {
  202. backgroundv.classList.add( 'past' );
  203. }
  204. else if ( v > indices.v ) {
  205. backgroundv.classList.add( 'future' );
  206. }
  207. else {
  208. backgroundv.classList.add( 'present' );
  209. // Only if this is the present horizontal and vertical slide
  210. if( h === indices.h ) currentBackground = backgroundv;
  211. }
  212. } );
  213. }
  214. } );
  215. // Stop content inside of previous backgrounds
  216. if( this.previousBackground ) {
  217. this.Reveal.slideContent.stopEmbeddedContent( this.previousBackground, { unloadIframes: !this.Reveal.slideContent.shouldPreload( this.previousBackground ) } );
  218. }
  219. // Start content in the current background
  220. if( currentBackground ) {
  221. this.Reveal.slideContent.startEmbeddedContent( currentBackground );
  222. let currentBackgroundContent = currentBackground.querySelector( '.slide-background-content' );
  223. if( currentBackgroundContent ) {
  224. let backgroundImageURL = currentBackgroundContent.style.backgroundImage || '';
  225. // Restart GIFs (doesn't work in Firefox)
  226. if( /\.gif/i.test( backgroundImageURL ) ) {
  227. currentBackgroundContent.style.backgroundImage = '';
  228. window.getComputedStyle( currentBackgroundContent ).opacity;
  229. currentBackgroundContent.style.backgroundImage = backgroundImageURL;
  230. }
  231. }
  232. // Don't transition between identical backgrounds. This
  233. // prevents unwanted flicker.
  234. let previousBackgroundHash = this.previousBackground ? this.previousBackground.getAttribute( 'data-background-hash' ) : null;
  235. let currentBackgroundHash = currentBackground.getAttribute( 'data-background-hash' );
  236. if( currentBackgroundHash && currentBackgroundHash === previousBackgroundHash && currentBackground !== this.previousBackground ) {
  237. backgroundElement.classList.add( 'no-transition' );
  238. }
  239. this.previousBackground = currentBackground;
  240. }
  241. // If there's a background brightness flag for this slide,
  242. // bubble it to the .reveal container
  243. if( currentSlide ) {
  244. [ 'has-light-background', 'has-dark-background' ].forEach( classToBubble => {
  245. if( currentSlide.classList.contains( classToBubble ) ) {
  246. this.Reveal.getRevealElement().classList.add( classToBubble );
  247. }
  248. else {
  249. this.Reveal.getRevealElement().classList.remove( classToBubble );
  250. }
  251. }, this );
  252. }
  253. // Allow the first background to apply without transition
  254. setTimeout( () => {
  255. backgroundElement.classList.remove( 'no-transition' );
  256. }, 1 );
  257. }
  258. /**
  259. * Updates the position of the parallax background based
  260. * on the current slide index.
  261. */
  262. updateParallax() {
  263. let backgroundElement = this.Reveal.getBackgroundsElement();
  264. let indices = this.Reveal.getIndices();
  265. if( this.Reveal.getConfig().parallaxBackgroundImage ) {
  266. let horizontalSlides = this.Reveal.getHorizontalSlides(),
  267. verticalSlides = this.Reveal.getVerticalSlides();
  268. let backgroundSize = backgroundElement.style.backgroundSize.split( ' ' ),
  269. backgroundWidth, backgroundHeight;
  270. if( backgroundSize.length === 1 ) {
  271. backgroundWidth = backgroundHeight = parseInt( backgroundSize[0], 10 );
  272. }
  273. else {
  274. backgroundWidth = parseInt( backgroundSize[0], 10 );
  275. backgroundHeight = parseInt( backgroundSize[1], 10 );
  276. }
  277. let slideWidth = backgroundElement.offsetWidth,
  278. horizontalSlideCount = horizontalSlides.length,
  279. horizontalOffsetMultiplier,
  280. horizontalOffset;
  281. if( typeof this.Reveal.getConfig().parallaxBackgroundHorizontal === 'number' ) {
  282. horizontalOffsetMultiplier = this.Reveal.getConfig().parallaxBackgroundHorizontal;
  283. }
  284. else {
  285. horizontalOffsetMultiplier = horizontalSlideCount > 1 ? ( backgroundWidth - slideWidth ) / ( horizontalSlideCount-1 ) : 0;
  286. }
  287. horizontalOffset = horizontalOffsetMultiplier * indices.h * -1;
  288. let slideHeight = backgroundElement.offsetHeight,
  289. verticalSlideCount = verticalSlides.length,
  290. verticalOffsetMultiplier,
  291. verticalOffset;
  292. if( typeof this.Reveal.getConfig().parallaxBackgroundVertical === 'number' ) {
  293. verticalOffsetMultiplier = this.Reveal.getConfig().parallaxBackgroundVertical;
  294. }
  295. else {
  296. verticalOffsetMultiplier = ( backgroundHeight - slideHeight ) / ( verticalSlideCount-1 );
  297. }
  298. verticalOffset = verticalSlideCount > 0 ? verticalOffsetMultiplier * indices.v : 0;
  299. backgroundElement.style.backgroundPosition = horizontalOffset + 'px ' + -verticalOffset + 'px';
  300. }
  301. }
  302. }