zoom.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Custom reveal.js integration
  2. (function(){
  3. var isEnabled = true;
  4. document.querySelector( '.reveal .slides' ).addEventListener( 'mousedown', function( event ) {
  5. var modifier = ( Reveal.getConfig().zoomKey ? Reveal.getConfig().zoomKey : 'alt' ) + 'Key';
  6. var zoomPadding = 20;
  7. var revealScale = Reveal.getScale();
  8. if( event[ modifier ] && isEnabled ) {
  9. event.preventDefault();
  10. var bounds;
  11. var originalDisplay = event.target.style.display;
  12. // Get the bounding rect of the contents, not the containing box
  13. if( window.getComputedStyle( event.target ).display === 'block' ) {
  14. event.target.style.display = 'inline-block';
  15. bounds = event.target.getBoundingClientRect();
  16. event.target.style.display = originalDisplay;
  17. } else {
  18. bounds = event.target.getBoundingClientRect();
  19. }
  20. zoom.to({
  21. x: ( bounds.left * revealScale ) - zoomPadding,
  22. y: ( bounds.top * revealScale ) - zoomPadding,
  23. width: ( bounds.width * revealScale ) + ( zoomPadding * 2 ),
  24. height: ( bounds.height * revealScale ) + ( zoomPadding * 2 ),
  25. pan: false
  26. });
  27. }
  28. } );
  29. Reveal.addEventListener( 'overviewshown', function() { isEnabled = false; } );
  30. Reveal.addEventListener( 'overviewhidden', function() { isEnabled = true; } );
  31. })();
  32. /*!
  33. * zoom.js 0.3 (modified for use with reveal.js)
  34. * http://lab.hakim.se/zoom-js
  35. * MIT licensed
  36. *
  37. * Copyright (C) 2011-2014 Hakim El Hattab, http://hakim.se
  38. */
  39. var zoom = (function(){
  40. // The current zoom level (scale)
  41. var level = 1;
  42. // The current mouse position, used for panning
  43. var mouseX = 0,
  44. mouseY = 0;
  45. // Timeout before pan is activated
  46. var panEngageTimeout = -1,
  47. panUpdateInterval = -1;
  48. // Check for transform support so that we can fallback otherwise
  49. var supportsTransforms = 'WebkitTransform' in document.body.style ||
  50. 'MozTransform' in document.body.style ||
  51. 'msTransform' in document.body.style ||
  52. 'OTransform' in document.body.style ||
  53. 'transform' in document.body.style;
  54. if( supportsTransforms ) {
  55. // The easing that will be applied when we zoom in/out
  56. document.body.style.transition = 'transform 0.8s ease';
  57. document.body.style.OTransition = '-o-transform 0.8s ease';
  58. document.body.style.msTransition = '-ms-transform 0.8s ease';
  59. document.body.style.MozTransition = '-moz-transform 0.8s ease';
  60. document.body.style.WebkitTransition = '-webkit-transform 0.8s ease';
  61. }
  62. // Zoom out if the user hits escape
  63. document.addEventListener( 'keyup', function( event ) {
  64. if( level !== 1 && event.keyCode === 27 ) {
  65. zoom.out();
  66. }
  67. } );
  68. // Monitor mouse movement for panning
  69. document.addEventListener( 'mousemove', function( event ) {
  70. if( level !== 1 ) {
  71. mouseX = event.clientX;
  72. mouseY = event.clientY;
  73. }
  74. } );
  75. /**
  76. * Applies the CSS required to zoom in, prefers the use of CSS3
  77. * transforms but falls back on zoom for IE.
  78. *
  79. * @param {Object} rect
  80. * @param {Number} scale
  81. */
  82. function magnify( rect, scale ) {
  83. var scrollOffset = getScrollOffset();
  84. // Ensure a width/height is set
  85. rect.width = rect.width || 1;
  86. rect.height = rect.height || 1;
  87. // Center the rect within the zoomed viewport
  88. rect.x -= ( window.innerWidth - ( rect.width * scale ) ) / 2;
  89. rect.y -= ( window.innerHeight - ( rect.height * scale ) ) / 2;
  90. if( supportsTransforms ) {
  91. // Reset
  92. if( scale === 1 ) {
  93. document.body.style.transform = '';
  94. document.body.style.OTransform = '';
  95. document.body.style.msTransform = '';
  96. document.body.style.MozTransform = '';
  97. document.body.style.WebkitTransform = '';
  98. }
  99. // Scale
  100. else {
  101. var origin = scrollOffset.x +'px '+ scrollOffset.y +'px',
  102. transform = 'translate('+ -rect.x +'px,'+ -rect.y +'px) scale('+ scale +')';
  103. document.body.style.transformOrigin = origin;
  104. document.body.style.OTransformOrigin = origin;
  105. document.body.style.msTransformOrigin = origin;
  106. document.body.style.MozTransformOrigin = origin;
  107. document.body.style.WebkitTransformOrigin = origin;
  108. document.body.style.transform = transform;
  109. document.body.style.OTransform = transform;
  110. document.body.style.msTransform = transform;
  111. document.body.style.MozTransform = transform;
  112. document.body.style.WebkitTransform = transform;
  113. }
  114. }
  115. else {
  116. // Reset
  117. if( scale === 1 ) {
  118. document.body.style.position = '';
  119. document.body.style.left = '';
  120. document.body.style.top = '';
  121. document.body.style.width = '';
  122. document.body.style.height = '';
  123. document.body.style.zoom = '';
  124. }
  125. // Scale
  126. else {
  127. document.body.style.position = 'relative';
  128. document.body.style.left = ( - ( scrollOffset.x + rect.x ) / scale ) + 'px';
  129. document.body.style.top = ( - ( scrollOffset.y + rect.y ) / scale ) + 'px';
  130. document.body.style.width = ( scale * 100 ) + '%';
  131. document.body.style.height = ( scale * 100 ) + '%';
  132. document.body.style.zoom = scale;
  133. }
  134. }
  135. level = scale;
  136. if( document.documentElement.classList ) {
  137. if( level !== 1 ) {
  138. document.documentElement.classList.add( 'zoomed' );
  139. }
  140. else {
  141. document.documentElement.classList.remove( 'zoomed' );
  142. }
  143. }
  144. }
  145. /**
  146. * Pan the document when the mosue cursor approaches the edges
  147. * of the window.
  148. */
  149. function pan() {
  150. var range = 0.12,
  151. rangeX = window.innerWidth * range,
  152. rangeY = window.innerHeight * range,
  153. scrollOffset = getScrollOffset();
  154. // Up
  155. if( mouseY < rangeY ) {
  156. window.scroll( scrollOffset.x, scrollOffset.y - ( 1 - ( mouseY / rangeY ) ) * ( 14 / level ) );
  157. }
  158. // Down
  159. else if( mouseY > window.innerHeight - rangeY ) {
  160. window.scroll( scrollOffset.x, scrollOffset.y + ( 1 - ( window.innerHeight - mouseY ) / rangeY ) * ( 14 / level ) );
  161. }
  162. // Left
  163. if( mouseX < rangeX ) {
  164. window.scroll( scrollOffset.x - ( 1 - ( mouseX / rangeX ) ) * ( 14 / level ), scrollOffset.y );
  165. }
  166. // Right
  167. else if( mouseX > window.innerWidth - rangeX ) {
  168. window.scroll( scrollOffset.x + ( 1 - ( window.innerWidth - mouseX ) / rangeX ) * ( 14 / level ), scrollOffset.y );
  169. }
  170. }
  171. function getScrollOffset() {
  172. return {
  173. x: window.scrollX !== undefined ? window.scrollX : window.pageXOffset,
  174. y: window.scrollY !== undefined ? window.scrollY : window.pageYOffset
  175. }
  176. }
  177. return {
  178. /**
  179. * Zooms in on either a rectangle or HTML element.
  180. *
  181. * @param {Object} options
  182. * - element: HTML element to zoom in on
  183. * OR
  184. * - x/y: coordinates in non-transformed space to zoom in on
  185. * - width/height: the portion of the screen to zoom in on
  186. * - scale: can be used instead of width/height to explicitly set scale
  187. */
  188. to: function( options ) {
  189. // Due to an implementation limitation we can't zoom in
  190. // to another element without zooming out first
  191. if( level !== 1 ) {
  192. zoom.out();
  193. }
  194. else {
  195. options.x = options.x || 0;
  196. options.y = options.y || 0;
  197. // If an element is set, that takes precedence
  198. if( !!options.element ) {
  199. // Space around the zoomed in element to leave on screen
  200. var padding = 20;
  201. var bounds = options.element.getBoundingClientRect();
  202. options.x = bounds.left - padding;
  203. options.y = bounds.top - padding;
  204. options.width = bounds.width + ( padding * 2 );
  205. options.height = bounds.height + ( padding * 2 );
  206. }
  207. // If width/height values are set, calculate scale from those values
  208. if( options.width !== undefined && options.height !== undefined ) {
  209. options.scale = Math.max( Math.min( window.innerWidth / options.width, window.innerHeight / options.height ), 1 );
  210. }
  211. if( options.scale > 1 ) {
  212. options.x *= options.scale;
  213. options.y *= options.scale;
  214. magnify( options, options.scale );
  215. if( options.pan !== false ) {
  216. // Wait with engaging panning as it may conflict with the
  217. // zoom transition
  218. panEngageTimeout = setTimeout( function() {
  219. panUpdateInterval = setInterval( pan, 1000 / 60 );
  220. }, 800 );
  221. }
  222. }
  223. }
  224. },
  225. /**
  226. * Resets the document zoom state to its default.
  227. */
  228. out: function() {
  229. clearTimeout( panEngageTimeout );
  230. clearInterval( panUpdateInterval );
  231. magnify( { x: 0, y: 0 }, 1 );
  232. level = 1;
  233. },
  234. // Alias
  235. magnify: function( options ) { this.to( options ) },
  236. reset: function() { this.out() },
  237. zoomLevel: function() {
  238. return level;
  239. }
  240. }
  241. })();