backgrounds.js 13 KB

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