zoom.js 7.4 KB

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