zoom.js 7.7 KB

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