zoom.js 7.3 KB

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