zoom.js integration via alt+click
authorHakim El Hattab <hakim.elhattab@gmail.com>
Tue, 9 Oct 2012 04:06:34 +0000 (00:06 -0400)
committerHakim El Hattab <hakim.elhattab@gmail.com>
Tue, 9 Oct 2012 04:06:34 +0000 (00:06 -0400)
index.html
lib/js/zoom.js [new file with mode: 0644]

index ce10815ce39e11b49772372a0da49b54d43595ae..56060255a86b1b7327549c70f97917ed4f0a049a 100644 (file)
@@ -90,9 +90,9 @@
                                </section>
 
                                <section>
-                                       <h2>Holistic Overview</h2>
+                                       <h2>Point of View</h2>
                                        <p>
-                                               Press <strong>ESC</strong> to enter the slide overview!
+                                               Press <strong>ESC</strong> to enter the slide overview. Hold down alt and click on any element to zoom in on it using <a href="http://lab.hakim.se/zoom-js">zoom.js</a>.
                                        </p>
                                </section>
 
@@ -263,8 +263,8 @@ function linkify( selector ) {
                                
                                <section>
                                        <h2>Spectacular image!</h2>
-                                       <a class="image" href="http://hakim.se/experiments/html5/breakdom/" target="_blank">
-                                               <img width="320" height="412" src="https://s3.amazonaws.com/hakim-static/reveal-js/breakdom.png" alt="BreakDOM game screenshot">
+                                       <a class="image" href="http://lab.hakim.se/meny/" target="_blank">
+                                               <img width="320" height="299" src="http://s3.amazonaws.com/hakim-static/portfolio/images/meny.png" alt="Meny">
                                        </a>
                                </section>
                                
@@ -342,6 +342,7 @@ function linkify( selector ) {
                                        { src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
                                        { src: 'lib/js/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
                                        { src: 'lib/js/data-markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
+                                       { src: 'lib/js/zoom.js', condition: function() { return !!document.body.classList; } },
                                        { src: '/socket.io/socket.io.js', async: true, condition: function() { return window.location.host === 'localhost:1947'; } },
                                        { src: 'plugin/speakernotes/client.js', async: true, condition: function() { return window.location.host === 'localhost:1947'; } }
                                ]
diff --git a/lib/js/zoom.js b/lib/js/zoom.js
new file mode 100644 (file)
index 0000000..7fba523
--- /dev/null
@@ -0,0 +1,257 @@
+/*!
+ * zoom.js 0.2 (modified version for use with reveal.js)
+ * http://lab.hakim.se/zoom-js
+ * MIT licensed
+ * 
+ * Copyright (C) 2011-2012 Hakim El Hattab, http://hakim.se
+ */
+var zoom = (function(){
+
+       // The current zoom level (scale)
+       var level = 1;
+       
+       // The current mouse position, used for panning
+       var mouseX = 0,
+               mouseY = 0;
+       
+       // 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 ||
+                                                               'transform' in document.body.style;
+    
+       if( supportsTransforms ) {
+               // The easing that will be applied when we zoom in/out
+               document.body.style.transition = 'transform 0.8s ease';
+               document.body.style.OTransition = '-o-transform 0.8s ease';
+               document.body.style.msTransition = '-ms-transform 0.8s ease';
+               document.body.style.MozTransition = '-moz-transform 0.8s ease';
+               document.body.style.WebkitTransition = '-webkit-transform 0.8s ease';
+       }
+       
+       // Zoom out if the user hits escape
+       document.addEventListener( 'keyup', function( event ) {
+               if( level !== 1 && event.keyCode === 27 ) {
+                       zoom.out();
+               }
+       } );
+
+       // Monitor mouse movement for panning
+       document.addEventListener( 'mousemove', function( event ) {
+               if( level !== 1 ) {
+                       mouseX = event.clientX;
+                       mouseY = event.clientY;
+               }
+       } );
+
+       /**
+        * Applies the CSS required to zoom in, prioritizes use of CSS3 
+        * transforms but falls back on zoom for IE.
+        * 
+        * @param {Number} pageOffsetX 
+        * @param {Number} pageOffsetY 
+        * @param {Number} elementOffsetX 
+        * @param {Number} elementOffsetY 
+        * @param {Number} scale 
+        */
+       function magnify( pageOffsetX, pageOffsetY, elementOffsetX, elementOffsetY, scale ) {
+
+               if( supportsTransforms ) {
+                       var origin = pageOffsetX +'px '+ pageOffsetY +'px',
+                               transform = 'translate('+ -elementOffsetX +'px,'+ -elementOffsetY +'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.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
+                       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
+                       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.width = ( scale * 100 ) + '%';
+                               document.body.style.height = ( scale * 100 ) + '%';
+                               document.body.style.zoom = scale;
+                       }
+               }
+
+               level = scale;
+       }
+
+       /**
+        * Pan the document when the mosue cursor approaches the edges 
+        * of the window.
+        */
+       function pan() {
+               var range = 0.12,
+                       rangeX = window.innerWidth * range,
+                       rangeY = window.innerHeight * range,
+                       scrollOffset = getScrollOffset();
+               
+               // Up
+               if( mouseY < rangeY ) {
+                       window.scroll( scrollOffset.x, scrollOffset.y - ( 1 - ( mouseY / rangeY ) ) * ( 14 / level ) );
+               }
+               // Down
+               else if( mouseY > window.innerHeight - rangeY ) {
+                       window.scroll( scrollOffset.x, scrollOffset.y + ( 1 - ( window.innerHeight - mouseY ) / rangeY ) * ( 14 / level ) );
+               }
+
+               // Left
+               if( mouseX < rangeX ) {
+                       window.scroll( scrollOffset.x - ( 1 - ( mouseX / rangeX ) ) * ( 14 / level ), scrollOffset.y );
+               }
+               // Right
+               else if( mouseX > window.innerWidth - rangeX ) {
+                       window.scroll( scrollOffset.x + ( 1 - ( window.innerWidth - mouseX ) / rangeX ) * ( 14 / level ), scrollOffset.y );
+               }
+       }
+
+       function getScrollOffset() {
+               return {
+                       x: window.scrollX !== undefined ? window.scrollX : window.pageXOffset,
+                       y: window.scrollY !== undefined ? window.scrollY : window.pageXYffset
+               }
+       }
+
+       return {
+               /**
+                * Zooms in on either a rectangle or HTML element.
+                * 
+                * @param {Object} options
+                *   - element: HTML element to zoom in on
+                *   OR
+                *   - 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();
+                       }
+                       else {
+                               options.x = options.x || 0;
+                               options.y = options.y || 0;
+
+                               // If an element is set, that takes precedence
+                               if( !!options.element ) {
+                                       // Space around the zoomed in element to leave on screen
+                                       var padding = 20;
+
+                                       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;
+                               }
+
+                               // 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 );
+                               }
+
+                               if( options.scale > 1 ) {
+                                       options.x *= options.scale;
+                                       options.y *= options.scale;
+
+                                       var scrollOffset = getScrollOffset();
+
+                                       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();
+
+                       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 ) },
+               reset: function() { this.out() },
+               
+               zoomLevel: function() {
+                       return level;
+               }
+       }
+       
+})();
+
+
+
+
+// Custom reveal.js integration
+(function(){
+       var modifierIsActive = false;
+
+       function updateKeyFlag( event ) {
+               modifierIsActive = event.altKey;
+       }
+
+       document.addEventListener( 'keypress', updateKeyFlag, false );
+       document.addEventListener( 'keydown', updateKeyFlag, false );
+       document.addEventListener( 'keyup', updateKeyFlag, false );
+
+       document.querySelector( '.reveal' ).addEventListener( 'click', function( event ) {
+               if( modifierIsActive ) {
+                       event.preventDefault();
+                       zoom.to({ element: event.target, pan: false });
+               }
+       } );
+})();
+