zoom.js 7.6 KB

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