zoom.js 7.6 KB

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