assets/reveal.js/plugin/zoom-js/zoom.js in slippery-0.4.1 vs assets/reveal.js/plugin/zoom-js/zoom.js in slippery-0.4.2

- old
+ new

@@ -1,28 +1,40 @@ // Custom reveal.js integration (function(){ var isEnabled = true; - document.querySelector( '.reveal' ).addEventListener( 'mousedown', function( event ) { + document.querySelector( '.reveal .slides' ).addEventListener( 'mousedown', function( event ) { var modifier = ( Reveal.getConfig().zoomKey ? Reveal.getConfig().zoomKey : 'alt' ) + 'Key'; + var zoomPadding = 20; + var revealScale = Reveal.getScale(); + if( event[ modifier ] && isEnabled ) { event.preventDefault(); - zoom.to({ element: event.target, pan: false }); + + var bounds = event.target.getBoundingClientRect(); + + zoom.to({ + x: ( bounds.left * revealScale ) - zoomPadding, + y: ( bounds.top * revealScale ) - zoomPadding, + width: ( bounds.width * revealScale ) + ( zoomPadding * 2 ), + height: ( bounds.height * revealScale ) + ( zoomPadding * 2 ), + pan: false + }); } } ); Reveal.addEventListener( 'overviewshown', function() { isEnabled = false; } ); Reveal.addEventListener( 'overviewhidden', function() { isEnabled = true; } ); })(); /*! - * zoom.js 0.2 (modified version for use with reveal.js) + * zoom.js 0.3 (modified for use with reveal.js) * http://lab.hakim.se/zoom-js * MIT licensed * - * Copyright (C) 2011-2012 Hakim El Hattab, http://hakim.se + * Copyright (C) 2011-2014 Hakim El Hattab, http://hakim.se */ var zoom = (function(){ // The current zoom level (scale) var level = 1; @@ -33,12 +45,10 @@ // Timeout before pan is activated var panEngageTimeout = -1, panUpdateInterval = -1; - var currentOptions = null; - // Check for transform support so that we can fallback otherwise var supportsTransforms = 'WebkitTransform' in document.body.style || 'MozTransform' in document.body.style || 'msTransform' in document.body.style || 'OTransform' in document.body.style || @@ -56,77 +66,97 @@ // Zoom out if the user hits escape document.addEventListener( 'keyup', function( event ) { if( level !== 1 && event.keyCode === 27 ) { zoom.out(); } - }, false ); + } ); // Monitor mouse movement for panning document.addEventListener( 'mousemove', function( event ) { if( level !== 1 ) { mouseX = event.clientX; mouseY = event.clientY; } - }, false ); + } ); /** - * Applies the CSS required to zoom in, prioritizes use of CSS3 + * Applies the CSS required to zoom in, prefers the use of CSS3 * transforms but falls back on zoom for IE. * - * @param {Number} pageOffsetX - * @param {Number} pageOffsetY - * @param {Number} elementOffsetX - * @param {Number} elementOffsetY + * @param {Object} rect * @param {Number} scale */ - function magnify( pageOffsetX, pageOffsetY, elementOffsetX, elementOffsetY, scale ) { + function magnify( rect, scale ) { + var scrollOffset = getScrollOffset(); + + // Ensure a width/height is set + rect.width = rect.width || 1; + rect.height = rect.height || 1; + + // Center the rect within the zoomed viewport + rect.x -= ( window.innerWidth - ( rect.width * scale ) ) / 2; + rect.y -= ( window.innerHeight - ( rect.height * scale ) ) / 2; + if( supportsTransforms ) { - var origin = pageOffsetX +'px '+ pageOffsetY +'px', - transform = 'translate('+ -elementOffsetX +'px,'+ -elementOffsetY +'px) scale('+ scale +')'; + // Reset + if( scale === 1 ) { + document.body.style.transform = ''; + document.body.style.OTransform = ''; + document.body.style.msTransform = ''; + document.body.style.MozTransform = ''; + document.body.style.WebkitTransform = ''; + } + // Scale + else { + var origin = scrollOffset.x +'px '+ scrollOffset.y +'px', + transform = 'translate('+ -rect.x +'px,'+ -rect.y +'px) scale('+ scale +')'; - document.body.style.transformOrigin = origin; - document.body.style.OTransformOrigin = origin; - document.body.style.msTransformOrigin = origin; - document.body.style.MozTransformOrigin = origin; - document.body.style.WebkitTransformOrigin = origin; + document.body.style.transformOrigin = origin; + document.body.style.OTransformOrigin = origin; + document.body.style.msTransformOrigin = origin; + document.body.style.MozTransformOrigin = origin; + document.body.style.WebkitTransformOrigin = origin; - document.body.style.transform = transform; - document.body.style.OTransform = transform; - document.body.style.msTransform = transform; - document.body.style.MozTransform = transform; - document.body.style.WebkitTransform = transform; + document.body.style.transform = transform; + document.body.style.OTransform = transform; + document.body.style.msTransform = transform; + document.body.style.MozTransform = transform; + document.body.style.WebkitTransform = transform; + } } else { - // Reset all values + // Reset if( scale === 1 ) { document.body.style.position = ''; document.body.style.left = ''; document.body.style.top = ''; document.body.style.width = ''; document.body.style.height = ''; document.body.style.zoom = ''; } - // Apply scale + // Scale else { document.body.style.position = 'relative'; - document.body.style.left = ( - ( pageOffsetX + elementOffsetX ) / scale ) + 'px'; - document.body.style.top = ( - ( pageOffsetY + elementOffsetY ) / scale ) + 'px'; + document.body.style.left = ( - ( scrollOffset.x + rect.x ) / scale ) + 'px'; + document.body.style.top = ( - ( scrollOffset.y + rect.y ) / scale ) + 'px'; document.body.style.width = ( scale * 100 ) + '%'; document.body.style.height = ( scale * 100 ) + '%'; document.body.style.zoom = scale; } } level = scale; - if( level !== 1 && document.documentElement.classList ) { - document.documentElement.classList.add( 'zoomed' ); + if( document.documentElement.classList ) { + if( level !== 1 ) { + document.documentElement.classList.add( 'zoomed' ); + } + else { + document.documentElement.classList.remove( 'zoomed' ); + } } - else { - document.documentElement.classList.remove( 'zoomed' ); - } } /** * Pan the document when the mosue cursor approaches the edges * of the window. @@ -157,11 +187,11 @@ } function getScrollOffset() { return { x: window.scrollX !== undefined ? window.scrollX : window.pageXOffset, - y: window.scrollY !== undefined ? window.scrollY : window.pageXYffset + y: window.scrollY !== undefined ? window.scrollY : window.pageYOffset } } return { /** @@ -173,10 +203,11 @@ * - x/y: coordinates in non-transformed space to zoom in on * - width/height: the portion of the screen to zoom in on * - scale: can be used instead of width/height to explicitly set scale */ to: function( options ) { + // Due to an implementation limitation we can't zoom in // to another element without zooming out first if( level !== 1 ) { zoom.out(); } @@ -186,15 +217,16 @@ // If an element is set, that takes precedence if( !!options.element ) { // Space around the zoomed in element to leave on screen var padding = 20; + var bounds = options.element.getBoundingClientRect(); - options.width = options.element.getBoundingClientRect().width + ( padding * 2 ); - options.height = options.element.getBoundingClientRect().height + ( padding * 2 ); - options.x = options.element.getBoundingClientRect().left - padding; - options.y = options.element.getBoundingClientRect().top - padding; + options.x = bounds.left - padding; + options.y = bounds.top - padding; + options.width = bounds.width + ( padding * 2 ); + options.height = bounds.height + ( padding * 2 ); } // If width/height values are set, calculate scale from those values if( options.width !== undefined && options.height !== undefined ) { options.scale = Math.max( Math.min( window.innerWidth / options.width, window.innerHeight / options.height ), 1 ); @@ -202,48 +234,34 @@ if( options.scale > 1 ) { options.x *= options.scale; options.y *= options.scale; - var scrollOffset = getScrollOffset(); + magnify( options, options.scale ); - if( options.element ) { - scrollOffset.x -= ( window.innerWidth - ( options.width * options.scale ) ) / 2; - } - - magnify( scrollOffset.x, scrollOffset.y, options.x, options.y, options.scale ); - if( options.pan !== false ) { // Wait with engaging panning as it may conflict with the // zoom transition panEngageTimeout = setTimeout( function() { panUpdateInterval = setInterval( pan, 1000 / 60 ); }, 800 ); } } - - currentOptions = options; } }, /** * Resets the document zoom state to its default. */ out: function() { clearTimeout( panEngageTimeout ); clearInterval( panUpdateInterval ); - var scrollOffset = getScrollOffset(); + magnify( { x: 0, y: 0 }, 1 ); - if( currentOptions && currentOptions.element ) { - scrollOffset.x -= ( window.innerWidth - ( currentOptions.width * currentOptions.scale ) ) / 2; - } - - magnify( scrollOffset.x, scrollOffset.y, 0, 0, 1 ); - level = 1; }, // Alias magnify: function( options ) { this.to( options ) }, @@ -253,6 +271,8 @@ return level; } } })(); + +