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