You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
13KB

  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. let printMode = this.Reveal.isPrintingPDF();
  22. // Clear prior backgrounds
  23. this.element.innerHTML = '';
  24. this.element.classList.add( 'no-transition' );
  25. // Iterate over all horizontal slides
  26. this.Reveal.getHorizontalSlides().forEach( slideh => {
  27. let backgroundStack = this.createBackground( slideh, this.element );
  28. // Iterate over all vertical slides
  29. queryAll( slideh, 'section' ).forEach( slidev => {
  30. this.createBackground( slidev, backgroundStack );
  31. backgroundStack.classList.add( 'stack' );
  32. } );
  33. } );
  34. // Add parallax background if specified
  35. if( this.Reveal.getConfig().parallaxBackgroundImage ) {
  36. this.element.style.backgroundImage = 'url("' + this.Reveal.getConfig().parallaxBackgroundImage + '")';
  37. this.element.style.backgroundSize = this.Reveal.getConfig().parallaxBackgroundSize;
  38. this.element.style.backgroundRepeat = this.Reveal.getConfig().parallaxBackgroundRepeat;
  39. this.element.style.backgroundPosition = this.Reveal.getConfig().parallaxBackgroundPosition;
  40. // Make sure the below properties are set on the element - these properties are
  41. // needed for proper transitions to be set on the element via CSS. To remove
  42. // annoying background slide-in effect when the presentation starts, apply
  43. // these properties after short time delay
  44. setTimeout( () => {
  45. this.Reveal.getRevealElement().classList.add( 'has-parallax-background' );
  46. }, 1 );
  47. }
  48. else {
  49. this.element.style.backgroundImage = '';
  50. this.Reveal.getRevealElement().classList.remove( 'has-parallax-background' );
  51. }
  52. }
  53. /**
  54. * Creates a background for the given slide.
  55. *
  56. * @param {HTMLElement} slide
  57. * @param {HTMLElement} container The element that the background
  58. * should be appended to
  59. * @return {HTMLElement} New background div
  60. */
  61. createBackground( slide, container ) {
  62. // Main slide background element
  63. let element = document.createElement( 'div' );
  64. element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' );
  65. // Inner background element that wraps images/videos/iframes
  66. let contentElement = document.createElement( 'div' );
  67. contentElement.className = 'slide-background-content';
  68. element.appendChild( contentElement );
  69. container.appendChild( element );
  70. slide.slideBackgroundElement = element;
  71. slide.slideBackgroundContentElement = contentElement;
  72. // Syncs the background to reflect all current background settings
  73. this.sync( slide );
  74. return element;
  75. }
  76. /**
  77. * Renders all of the visual properties of a slide background
  78. * based on the various background attributes.
  79. *
  80. * @param {HTMLElement} slide
  81. */
  82. sync( slide ) {
  83. let element = slide.slideBackgroundElement,
  84. contentElement = slide.slideBackgroundContentElement;
  85. // Reset the prior background state in case this is not the
  86. // initial sync
  87. slide.classList.remove( 'has-dark-background' );
  88. slide.classList.remove( 'has-light-background' );
  89. element.removeAttribute( 'data-loaded' );
  90. element.removeAttribute( 'data-background-hash' );
  91. element.removeAttribute( 'data-background-size' );
  92. element.removeAttribute( 'data-background-transition' );
  93. element.style.backgroundColor = '';
  94. contentElement.style.backgroundSize = '';
  95. contentElement.style.backgroundRepeat = '';
  96. contentElement.style.backgroundPosition = '';
  97. contentElement.style.backgroundImage = '';
  98. contentElement.style.opacity = '';
  99. contentElement.innerHTML = '';
  100. let data = {
  101. background: slide.getAttribute( 'data-background' ),
  102. backgroundSize: slide.getAttribute( 'data-background-size' ),
  103. backgroundImage: slide.getAttribute( 'data-background-image' ),
  104. backgroundVideo: slide.getAttribute( 'data-background-video' ),
  105. backgroundIframe: slide.getAttribute( 'data-background-iframe' ),
  106. backgroundColor: slide.getAttribute( 'data-background-color' ),
  107. backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
  108. backgroundPosition: slide.getAttribute( 'data-background-position' ),
  109. backgroundTransition: slide.getAttribute( 'data-background-transition' ),
  110. backgroundOpacity: slide.getAttribute( 'data-background-opacity' )
  111. };
  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( slide.hasAttribute( 'data-preload' ) ) 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, check the computed background
  151. if( !contrastColor ) {
  152. let computedBackgroundStyle = window.getComputedStyle( element );
  153. if( computedBackgroundStyle && computedBackgroundStyle.backgroundColor ) {
  154. contrastColor = computedBackgroundStyle.backgroundColor;
  155. }
  156. }
  157. if( contrastColor ) {
  158. let 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. }