zoom.js 7.4 KB

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