zoom.js 7.6 KB

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