zoom.js 7.5 KB

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