zoom.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. }
  101. /**
  102. * Pan the document when the mosue cursor approaches the edges
  103. * of the window.
  104. */
  105. function pan() {
  106. var range = 0.12,
  107. rangeX = window.innerWidth * range,
  108. rangeY = window.innerHeight * range,
  109. scrollOffset = getScrollOffset();
  110. // Up
  111. if( mouseY < rangeY ) {
  112. window.scroll( scrollOffset.x, scrollOffset.y - ( 1 - ( mouseY / rangeY ) ) * ( 14 / level ) );
  113. }
  114. // Down
  115. else if( mouseY > window.innerHeight - rangeY ) {
  116. window.scroll( scrollOffset.x, scrollOffset.y + ( 1 - ( window.innerHeight - mouseY ) / rangeY ) * ( 14 / level ) );
  117. }
  118. // Left
  119. if( mouseX < rangeX ) {
  120. window.scroll( scrollOffset.x - ( 1 - ( mouseX / rangeX ) ) * ( 14 / level ), scrollOffset.y );
  121. }
  122. // Right
  123. else if( mouseX > window.innerWidth - rangeX ) {
  124. window.scroll( scrollOffset.x + ( 1 - ( window.innerWidth - mouseX ) / rangeX ) * ( 14 / level ), scrollOffset.y );
  125. }
  126. }
  127. function getScrollOffset() {
  128. return {
  129. x: window.scrollX !== undefined ? window.scrollX : window.pageXOffset,
  130. y: window.scrollY !== undefined ? window.scrollY : window.pageXYffset
  131. }
  132. }
  133. return {
  134. /**
  135. * Zooms in on either a rectangle or HTML element.
  136. *
  137. * @param {Object} options
  138. * - element: HTML element to zoom in on
  139. * OR
  140. * - x/y: coordinates in non-transformed space to zoom in on
  141. * - width/height: the portion of the screen to zoom in on
  142. * - scale: can be used instead of width/height to explicitly set scale
  143. */
  144. to: function( options ) {
  145. // Due to an implementation limitation we can't zoom in
  146. // to another element without zooming out first
  147. if( level !== 1 ) {
  148. zoom.out();
  149. }
  150. else {
  151. options.x = options.x || 0;
  152. options.y = options.y || 0;
  153. // If an element is set, that takes precedence
  154. if( !!options.element ) {
  155. // Space around the zoomed in element to leave on screen
  156. var padding = 20;
  157. options.width = options.element.getBoundingClientRect().width + ( padding * 2 );
  158. options.height = options.element.getBoundingClientRect().height + ( padding * 2 );
  159. options.x = options.element.getBoundingClientRect().left - padding;
  160. options.y = options.element.getBoundingClientRect().top - padding;
  161. }
  162. // If width/height values are set, calculate scale from those values
  163. if( options.width !== undefined && options.height !== undefined ) {
  164. options.scale = Math.max( Math.min( window.innerWidth / options.width, window.innerHeight / options.height ), 1 );
  165. }
  166. if( options.scale > 1 ) {
  167. options.x *= options.scale;
  168. options.y *= options.scale;
  169. var scrollOffset = getScrollOffset();
  170. if( options.element ) {
  171. scrollOffset.x -= ( window.innerWidth - ( options.width * options.scale ) ) / 2;
  172. }
  173. magnify( scrollOffset.x, scrollOffset.y, options.x, options.y, options.scale );
  174. if( options.pan !== false ) {
  175. // Wait with engaging panning as it may conflict with the
  176. // zoom transition
  177. panEngageTimeout = setTimeout( function() {
  178. panUpdateInterval = setInterval( pan, 1000 / 60 );
  179. }, 800 );
  180. }
  181. }
  182. currentOptions = options;
  183. }
  184. },
  185. /**
  186. * Resets the document zoom state to its default.
  187. */
  188. out: function() {
  189. clearTimeout( panEngageTimeout );
  190. clearInterval( panUpdateInterval );
  191. var scrollOffset = getScrollOffset();
  192. if( currentOptions && currentOptions.element ) {
  193. scrollOffset.x -= ( window.innerWidth - ( currentOptions.width * currentOptions.scale ) ) / 2;
  194. }
  195. magnify( scrollOffset.x, scrollOffset.y, 0, 0, 1 );
  196. level = 1;
  197. },
  198. // Alias
  199. magnify: function( options ) { this.to( options ) },
  200. reset: function() { this.out() },
  201. zoomLevel: function() {
  202. return level;
  203. }
  204. }
  205. })();