zoom.js 7.5 KB

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