zoom.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.3 (modified for use with reveal.js)
  16. * http://lab.hakim.se/zoom-js
  17. * MIT licensed
  18. *
  19. * Copyright (C) 2011-2014 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. // 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. } );
  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. } );
  57. /**
  58. * Applies the CSS required to zoom in, prefers the use of CSS3
  59. * transforms but falls back on zoom for IE.
  60. *
  61. * @param {Object} rect
  62. * @param {Number} scale
  63. */
  64. function magnify( rect, scale ) {
  65. var scrollOffset = getScrollOffset();
  66. // Ensure a width/height is set
  67. rect.width = rect.width || 1;
  68. rect.height = rect.height || 1;
  69. // Center the rect within the zoomed viewport
  70. rect.x -= ( window.innerWidth - ( rect.width * scale ) ) / 2;
  71. rect.y -= ( window.innerHeight - ( rect.height * scale ) ) / 2;
  72. if( supportsTransforms ) {
  73. // Reset
  74. if( scale === 1 ) {
  75. document.body.style.transform = '';
  76. document.body.style.OTransform = '';
  77. document.body.style.msTransform = '';
  78. document.body.style.MozTransform = '';
  79. document.body.style.WebkitTransform = '';
  80. }
  81. // Scale
  82. else {
  83. var origin = scrollOffset.x +'px '+ scrollOffset.y +'px',
  84. transform = 'translate('+ -rect.x +'px,'+ -rect.y +'px) scale('+ scale +')';
  85. document.body.style.transformOrigin = origin;
  86. document.body.style.OTransformOrigin = origin;
  87. document.body.style.msTransformOrigin = origin;
  88. document.body.style.MozTransformOrigin = origin;
  89. document.body.style.WebkitTransformOrigin = origin;
  90. document.body.style.transform = transform;
  91. document.body.style.OTransform = transform;
  92. document.body.style.msTransform = transform;
  93. document.body.style.MozTransform = transform;
  94. document.body.style.WebkitTransform = transform;
  95. }
  96. }
  97. else {
  98. // Reset
  99. if( scale === 1 ) {
  100. document.body.style.position = '';
  101. document.body.style.left = '';
  102. document.body.style.top = '';
  103. document.body.style.width = '';
  104. document.body.style.height = '';
  105. document.body.style.zoom = '';
  106. }
  107. // Scale
  108. else {
  109. document.body.style.position = 'relative';
  110. document.body.style.left = ( - ( scrollOffset.x + rect.x ) / scale ) + 'px';
  111. document.body.style.top = ( - ( scrollOffset.y + rect.y ) / scale ) + 'px';
  112. document.body.style.width = ( scale * 100 ) + '%';
  113. document.body.style.height = ( scale * 100 ) + '%';
  114. document.body.style.zoom = scale;
  115. }
  116. }
  117. level = scale;
  118. if( document.documentElement.classList ) {
  119. if( level !== 1 ) {
  120. document.documentElement.classList.add( 'zoomed' );
  121. }
  122. else {
  123. document.documentElement.classList.remove( 'zoomed' );
  124. }
  125. }
  126. }
  127. /**
  128. * Pan the document when the mosue cursor approaches the edges
  129. * of the window.
  130. */
  131. function pan() {
  132. var range = 0.12,
  133. rangeX = window.innerWidth * range,
  134. rangeY = window.innerHeight * range,
  135. scrollOffset = getScrollOffset();
  136. // Up
  137. if( mouseY < rangeY ) {
  138. window.scroll( scrollOffset.x, scrollOffset.y - ( 1 - ( mouseY / rangeY ) ) * ( 14 / level ) );
  139. }
  140. // Down
  141. else if( mouseY > window.innerHeight - rangeY ) {
  142. window.scroll( scrollOffset.x, scrollOffset.y + ( 1 - ( window.innerHeight - mouseY ) / rangeY ) * ( 14 / level ) );
  143. }
  144. // Left
  145. if( mouseX < rangeX ) {
  146. window.scroll( scrollOffset.x - ( 1 - ( mouseX / rangeX ) ) * ( 14 / level ), scrollOffset.y );
  147. }
  148. // Right
  149. else if( mouseX > window.innerWidth - rangeX ) {
  150. window.scroll( scrollOffset.x + ( 1 - ( window.innerWidth - mouseX ) / rangeX ) * ( 14 / level ), scrollOffset.y );
  151. }
  152. }
  153. function getScrollOffset() {
  154. return {
  155. x: window.scrollX !== undefined ? window.scrollX : window.pageXOffset,
  156. y: window.scrollY !== undefined ? window.scrollY : window.pageYOffset
  157. }
  158. }
  159. return {
  160. /**
  161. * Zooms in on either a rectangle or HTML element.
  162. *
  163. * @param {Object} options
  164. * - element: HTML element to zoom in on
  165. * OR
  166. * - x/y: coordinates in non-transformed space to zoom in on
  167. * - width/height: the portion of the screen to zoom in on
  168. * - scale: can be used instead of width/height to explicitly set scale
  169. */
  170. to: function( options ) {
  171. // Due to an implementation limitation we can't zoom in
  172. // to another element without zooming out first
  173. if( level !== 1 ) {
  174. zoom.out();
  175. }
  176. else {
  177. options.x = options.x || 0;
  178. options.y = options.y || 0;
  179. // If an element is set, that takes precedence
  180. if( !!options.element ) {
  181. // Space around the zoomed in element to leave on screen
  182. var padding = 20;
  183. var bounds = options.element.getBoundingClientRect();
  184. options.x = bounds.left - padding;
  185. options.y = bounds.top - padding;
  186. options.width = bounds.width + ( padding * 2 );
  187. options.height = bounds.height + ( padding * 2 );
  188. }
  189. // If width/height values are set, calculate scale from those values
  190. if( options.width !== undefined && options.height !== undefined ) {
  191. options.scale = Math.max( Math.min( window.innerWidth / options.width, window.innerHeight / options.height ), 1 );
  192. }
  193. if( options.scale > 1 ) {
  194. options.x *= options.scale;
  195. options.y *= options.scale;
  196. magnify( options, options.scale );
  197. if( options.pan !== false ) {
  198. // Wait with engaging panning as it may conflict with the
  199. // zoom transition
  200. panEngageTimeout = setTimeout( function() {
  201. panUpdateInterval = setInterval( pan, 1000 / 60 );
  202. }, 800 );
  203. }
  204. }
  205. }
  206. },
  207. /**
  208. * Resets the document zoom state to its default.
  209. */
  210. out: function() {
  211. clearTimeout( panEngageTimeout );
  212. clearInterval( panUpdateInterval );
  213. magnify( { x: 0, y: 0 }, 1 );
  214. level = 1;
  215. },
  216. // Alias
  217. magnify: function( options ) { this.to( options ) },
  218. reset: function() { this.out() },
  219. zoomLevel: function() {
  220. return level;
  221. }
  222. }
  223. })();