plugin.js 7.6 KB

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