123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710 |
- /**
- * impress.js
- *
- * impress.js is a presentation tool based on the power of CSS3 transforms and transitions
- * in modern browsers and inspired by the idea behind prezi.com.
- *
- *
- * Copyright 2011-2012 Bartek Szopka (@bartaz)
- *
- * Released under the MIT and GPL Licenses.
- *
- * ------------------------------------------------
- * author: Bartek Szopka
- * version: 0.5.3
- * url: http://bartaz.github.com/impress.js/
- * source: http://github.com/bartaz/impress.js/
- */
- /*jshint bitwise:true, curly:true, eqeqeq:true, forin:true, latedef:true, newcap:true,
- noarg:true, noempty:true, undef:true, strict:true, browser:true */
- // You are one of those who like to know how things work inside?
- // Let me show you the cogs that make impress.js run...
- (function( factory, document, window )
- {
- if ( typeof define === "function" && define.amd ) {
- define( factory );
- } else {
- window.impress = factory();
- }
- }(function() {
- "use strict";
- // HELPER FUNCTIONS
- // `pfx` is a function that takes a standard CSS property name as a parameter
- // and returns it's prefixed version valid for current browser it runs in.
- // The code is heavily inspired by Modernizr http://www.modernizr.com/
- var pfx = ( function() {
- var style = document.createElement( "dummy" ).style,
- prefixes = "Webkit Moz O ms Khtml".split( " " ),
- memory = {};
- return function( prop ) {
- if ( typeof memory[ prop ] === "undefined" ) {
- var ucProp = prop.charAt( 0 ).toUpperCase() + prop.substr( 1 ),
- props = ( prop + " " + prefixes.join( ucProp + " " ) + ucProp ).split( " " );
- memory[ prop ] = null;
- for ( var i in props ) {
- if ( style[ props[ i ] ] !== undefined ) {
- memory[ prop ] = props[ i ];
- break;
- }
- }
- }
- return memory[ prop ];
- };
- } )();
- // `arraify` takes an array-like object and turns it into real Array
- // to make all the Array.prototype goodness available.
- var arrayify = function( a ) {
- return [].slice.call( a );
- };
- // `css` function applies the styles given in `props` object to the element
- // given as `el`. It runs all property names through `pfx` function to make
- // sure proper prefixed version of the property is used.
- var css = function( el, props ) {
- var key, pkey;
- for ( key in props ) {
- if ( props.hasOwnProperty( key ) ) {
- pkey = pfx( key );
- if ( pkey !== null ) {
- el.style[ pkey ] = props[ key ];
- }
- }
- }
- return el;
- };
- // `toNumber` takes a value given as `numeric` parameter and tries to turn
- // it into a number. If it is not possible it returns 0 (or other value
- // given as `fallback`).
- var toNumber = function( numeric, fallback ) {
- return isNaN( numeric ) ? ( fallback || 0 ) : Number( numeric );
- };
- // `byId` returns element with given `id` - you probably have guessed that
- var byId = function( id ) {
- return document.getElementById( id );
- };
- // `$` returns first element for given CSS `selector` in the `context` of
- // the given element or whole document.
- var $ = function( selector, context ) {
- context = context || document;
- return context.querySelector( selector );
- };
- // `$$` return an array of elements for given CSS `selector` in the `context` of
- // the given element or whole document.
- var $$ = function( selector, context ) {
- context = context || document;
- return arrayify( context.querySelectorAll( selector ) );
- };
- // `triggerEvent` builds a custom DOM event with given `eventName` and `detail` data
- // and triggers it on element given as `el`.
- var triggerEvent = function( el, eventName, detail ) {
- var event = document.createEvent( "CustomEvent" );
- event.initCustomEvent( eventName, true, true, detail );
- el.dispatchEvent( event );
- };
- // `translate` builds a translate transform string for given data.
- var translate = function( t ) {
- return " translate3d(" + t.x + "px," + t.y + "px," + t.z + "px) ";
- };
- // `rotate` builds a rotate transform string for given data.
- // By default the rotations are in X Y Z order that can be reverted by passing `true`
- // as second parameter.
- var rotate = function( r, revert ) {
- var rX = " rotateX(" + r.x + "deg) ",
- rY = " rotateY(" + r.y + "deg) ",
- rZ = " rotateZ(" + r.z + "deg) ";
- return revert ? rZ + rY + rX : rX + rY + rZ;
- };
- // `scale` builds a scale transform string for given data.
- var scale = function( s ) {
- return " scale(" + s + ") ";
- };
- // `perspective` builds a perspective transform string for given data.
- var perspective = function( p ) {
- return " perspective(" + p + "px) ";
- };
- // `getElementFromHash` returns an element located by id from hash part of
- // window location.
- var getElementFromHash = function() {
- // Get id from url # by removing `#` or `#/` from the beginning,
- // so both "fallback" `#slide-id` and "enhanced" `#/slide-id` will work
- return byId( window.location.hash.replace( /^#\/?/, "" ) );
- };
- // `computeWindowScale` counts the scale factor between window size and size
- // defined for the presentation in the config.
- var computeWindowScale = function( config ) {
- var hScale = window.innerHeight / config.height,
- wScale = window.innerWidth / config.width,
- scale = hScale > wScale ? wScale : hScale;
- if ( config.maxScale && scale > config.maxScale ) {
- scale = config.maxScale;
- }
- if ( config.minScale && scale < config.minScale ) {
- scale = config.minScale;
- }
- return scale;
- };
- // CHECK SUPPORT
- var body = document.body;
- var ua = navigator.userAgent.toLowerCase();
- var impressSupported =
- // Browser should support CSS 3D transtorms
- ( pfx( "perspective" ) !== null ) &&
- // Browser should support `classList` and `dataset` APIs
- ( body.classList ) &&
- ( body.dataset ) &&
- // But some mobile devices need to be blacklisted,
- // because their CSS 3D support or hardware is not
- // good enough to run impress.js properly, sorry...
- ( ua.search( /(iphone)|(ipod)|(android)/ ) === -1 );
- if ( !impressSupported ) {
- // We can't be sure that `classList` is supported
- body.className += " impress-not-supported ";
- } else {
- body.classList.remove( "impress-not-supported" );
- body.classList.add( "impress-supported" );
- }
- // GLOBALS AND DEFAULTS
- // This is where the root elements of all impress.js instances will be kept.
- // Yes, this means you can have more than one instance on a page, but I'm not
- // sure if it makes any sense in practice ;)
- // var roots = {};
- // Some default config values.
- var defaults = {
- width: 1024,
- height: 768,
- maxScale: 1,
- minScale: 0,
- perspective: 1000,
- transitionDuration: 1000
- };
- // It's just an empty function ... and a useless comment.
- var empty = function() { return false; };
- // IMPRESS.JS API
- // And that's where interesting things will start to happen.
- // It's the core `impress` function that returns the impress.js API
- // for a presentation based on the element with given id ('impress'
- // by default).
- var impress = function( rootId ) {
- // If impress.js is not supported by the browser return a dummy API
- // it may not be a perfect solution but we return early and avoid
- // running code that may use features not implemented in the browser.
- if ( !impressSupported ) {
- return {
- init: empty,
- goto: empty,
- prev: empty,
- next: empty
- };
- }
- //
- rootId = rootId || "impress";
- // If given root is already initialized just return the API
- // if ( roots[ "impress-root-" + rootId ] ) {
- // return roots[ "impress-root-" + rootId ];
- // }
- // Data of all presentation steps
- var stepsData = {};
- // Element of currently active step
- var activeStep = null;
- // Current state (position, rotation and scale) of the presentation
- var currentState = null;
- // Array of step elements
- var steps = null;
- // Configuration options
- var config = null;
- // Scale factor of the browser window
- var windowScale = null;
- // Root presentation elements
- var root = byId( rootId );
- var canvas = document.createElement( "div" );
- var initialized = false;
- // STEP EVENTS
- //
- // There are currently two step events triggered by impress.js
- // `impress:stepenter` is triggered when the step is shown on the
- // screen (the transition from the previous one is finished) and
- // `impress:stepleave` is triggered when the step is left (the
- // transition to next step just starts).
- // Reference to last entered step
- var lastEntered = null;
- // `onStepEnter` is called whenever the step element is entered
- // but the event is triggered only if the step is different than
- // last entered step.
- var onStepEnter = function( step ) {
- if ( lastEntered !== step ) {
- triggerEvent( step, "impress:stepenter" );
- lastEntered = step;
- }
- };
- // `onStepLeave` is called whenever the step element is left
- // but the event is triggered only if the step is the same as
- // last entered step.
- var onStepLeave = function( step ) {
- if ( lastEntered === step ) {
- triggerEvent( step, "impress:stepleave" );
- lastEntered = null;
- }
- };
- // `initStep` initializes given step element by reading data from its
- // data attributes and setting correct styles.
- var initStep = function( el, idx ) {
- var data = el.dataset,
- step = {
- translate: {
- x: toNumber( data.x ),
- y: toNumber( data.y ),
- z: toNumber( data.z )
- },
- rotate: {
- x: toNumber( data.rotateX ),
- y: toNumber( data.rotateY ),
- z: toNumber( data.rotateZ || data.rotate )
- },
- scale: toNumber( data.scale, 1 ),
- el: el
- };
- if ( !el.id ) {
- el.id = "step-" + ( idx + 1 );
- }
- stepsData[ "impress-" + el.id ] = step;
- css( el, {
- position: "absolute",
- transform: "translate(-50%,-50%)" +
- translate( step.translate ) +
- rotate( step.rotate ) +
- scale( step.scale ),
- transformStyle: "preserve-3d"
- } );
- };
- // `init` API function that initializes (and runs) the presentation.
- var init = function() {
- if ( initialized ) { return; }
- // First we set up the viewport for mobile devices.
- // For some reason iPad goes nuts when it is not done properly.
- var meta = $( "meta[name='viewport']" ) || document.createElement( "meta" );
- meta.content = "width=device-width, minimum-scale=1, maximum-scale=1, user-scalable=no";
- if ( meta.parentNode !== document.head ) {
- meta.name = "viewport";
- document.head.appendChild( meta );
- }
- // Initialize configuration object
- var rootData = root.dataset;
- config = {
- width: toNumber( rootData.width, defaults.width ),
- height: toNumber( rootData.height, defaults.height ),
- maxScale: toNumber( rootData.maxScale, defaults.maxScale ),
- minScale: toNumber( rootData.minScale, defaults.minScale ),
- perspective: toNumber( rootData.perspective, defaults.perspective ),
- transitionDuration: toNumber(
- rootData.transitionDuration, defaults.transitionDuration
- )
- };
- windowScale = computeWindowScale( config );
- // Wrap steps with "canvas" element
- arrayify( root.childNodes ).forEach( function( el ) {
- canvas.appendChild( el );
- } );
- root.appendChild( canvas );
- // Set initial styles
- document.documentElement.style.height = "100%";
- css( body, {
- height: "100%",
- overflow: "hidden"
- } );
- var rootStyles = {
- position: "absolute",
- transformOrigin: "top left",
- transition: "all 0s ease-in-out",
- transformStyle: "preserve-3d"
- };
- css( root, rootStyles );
- css( root, {
- top: "50%",
- left: "50%",
- transform: perspective( config.perspective / windowScale ) + scale( windowScale )
- } );
- css( canvas, rootStyles );
- body.classList.remove( "impress-disabled" );
- body.classList.add( "impress-enabled" );
- // Get and init steps
- steps = $$( ".step", root );
- steps.forEach( initStep );
- // Set a default initial state of the canvas
- currentState = {
- translate: { x: 0, y: 0, z: 0 },
- rotate: { x: 0, y: 0, z: 0 },
- scale: 1
- };
- initialized = true;
- triggerEvent( root, "impress:init", { /*api: roots[ "impress-root-" + rootId ] */} );
- };
- var addStep = function(selector, insertBefore) {
- var customStep = $$( selector, root );
- customStep.forEach( initStep );
-
- var index = steps.length;
-
- var afterStep = getStep(insertBefore)
- if( afterStep !== null) {
- index = steps.indexOf(afterStep);
- }
- // customStep is an array and we have to insert the actual elements and not the array itself.
- // apply will expand the provided array to individual objects.
- Array.prototype.splice.apply(steps, [index, 0].concat(customStep));
- };
- var removeStep = function(selector) {
- var toRemove = getStep(selector);
- var index = steps.indexOf(toRemove);
- steps.splice(index, 1);
- };
- // `getStep` is a helper function that returns a step element defined by parameter.
- // If a number is given, step with index given by the number is returned, if a string
- // is given step element with such id is returned, if DOM element is given it is returned
- // if it is a correct step element.
- var getStep = function( step ) {
- if ( typeof step === "number" ) {
- step = step < 0 ? steps[ steps.length + step ] : steps[ step ];
- } else if ( typeof step === "string" ) {
- step = byId( step );
- }
- return ( step && step.id && stepsData[ "impress-" + step.id ] ) ? step : null;
- };
- // Used to reset timeout for `impress:stepenter` event
- var stepEnterTimeout = null;
- // `goto` API function that moves to step given with `el` parameter
- // (by index, id or element), with a transition `duration` optionally
- // given as second parameter.
- var goto = function( el, duration ) {
- if ( !initialized || !( el = getStep( el ) ) ) {
- // Presentation not initialized or given element is not a step
- return false;
- }
- // Sometimes it's possible to trigger focus on first link with some keyboard action.
- // Browser in such a case tries to scroll the page to make this element visible
- // (even that body overflow is set to hidden) and it breaks our careful positioning.
- //
- // So, as a lousy (and lazy) workaround we will make the page scroll back to the top
- // whenever slide is selected
- //
- // If you are reading this and know any better way to handle it, I'll be glad to hear
- // about it!
- window.scrollTo( 0, 0 );
- var step = stepsData[ "impress-" + el.id ];
- if ( activeStep ) {
- activeStep.classList.remove( "active" );
- body.classList.remove( "impress-on-" + activeStep.id );
- }
- el.classList.add( "active" );
- body.classList.add( "impress-on-" + el.id );
- // Compute target state of the canvas based on given step
- var target = {
- rotate: {
- x: -step.rotate.x,
- y: -step.rotate.y,
- z: -step.rotate.z
- },
- translate: {
- x: -step.translate.x,
- y: -step.translate.y,
- z: -step.translate.z
- },
- scale: 1 / step.scale
- };
- // Check if the transition is zooming in or not.
- //
- // This information is used to alter the transition style:
- // when we are zooming in - we start with move and rotate transition
- // and the scaling is delayed, but when we are zooming out we start
- // with scaling down and move and rotation are delayed.
- var zoomin = target.scale >= currentState.scale;
- duration = toNumber( duration, config.transitionDuration );
- var delay = ( duration / 2 );
- // If the same step is re-selected, force computing window scaling,
- // because it is likely to be caused by window resize
- if ( el === activeStep ) {
- windowScale = computeWindowScale( config );
- }
- var targetScale = target.scale * windowScale;
- // Trigger leave of currently active element (if it's not the same step again)
- if ( activeStep && activeStep !== el ) {
- onStepLeave( activeStep );
- }
- // Now we alter transforms of `root` and `canvas` to trigger transitions.
- //
- // And here is why there are two elements: `root` and `canvas` - they are
- // being animated separately:
- // `root` is used for scaling and `canvas` for translate and rotations.
- // Transitions on them are triggered with different delays (to make
- // visually nice and 'natural' looking transitions), so we need to know
- // that both of them are finished.
- css( root, {
- // To keep the perspective look similar for different scales
- // we need to 'scale' the perspective, too
- transform: perspective( config.perspective / targetScale ) + scale( targetScale ),
- transitionDuration: duration + "ms",
- transitionDelay: ( zoomin ? delay : 0 ) + "ms"
- } );
- css( canvas, {
- transform: rotate( target.rotate, true ) + translate( target.translate ),
- transitionDuration: duration + "ms",
- transitionDelay: ( zoomin ? 0 : delay ) + "ms"
- } );
- // Here is a tricky part...
- //
- // If there is no change in scale or no change in rotation and translation, it means
- // there was actually no delay - because there was no transition on `root` or `canvas`
- // elements. We want to trigger `impress:stepenter` event in the correct moment, so
- // here we compare the current and target values to check if delay should be taken into
- // account.
- //
- // I know that this `if` statement looks scary, but it's pretty simple when you know
- // what is going on
- // - it's simply comparing all the values.
- if ( currentState.scale === target.scale ||
- ( currentState.rotate.x === target.rotate.x &&
- currentState.rotate.y === target.rotate.y &&
- currentState.rotate.z === target.rotate.z &&
- currentState.translate.x === target.translate.x &&
- currentState.translate.y === target.translate.y &&
- currentState.translate.z === target.translate.z ) ) {
- delay = 0;
- }
- // Store current state
- currentState = target;
- activeStep = el;
- // And here is where we trigger `impress:stepenter` event.
- // We simply set up a timeout to fire it taking transition duration
- // (and possible delay) into account.
- //
- // I really wanted to make it in more elegant way. The `transitionend` event seemed to
- // be the best way to do it, but the fact that I'm using transitions on two separate
- // elements and that the `transitionend` event is only triggered when there was a
- // transition (change in the values) caused some bugs and made the code really
- // complicated, cause I had to handle all the conditions separately. And it still
- // needed a `setTimeout` fallback for the situations when there is no transition at
- // all.
- // So I decided that I'd rather make the code simpler than use shiny new
- // `transitionend`.
- //
- // If you want learn something interesting and see how it was done with `transitionend`
- // go back to
- // version 0.5.2 of impress.js:
- // http://github.com/bartaz/impress.js/blob/0.5.2/js/impress.js
- window.clearTimeout( stepEnterTimeout );
- stepEnterTimeout = window.setTimeout( function() {
- onStepEnter( activeStep );
- }, duration + delay );
- return el;
- };
- // `prev` API function goes to previous step (in document order)
- var prev = function() {
- var prev = steps.indexOf( activeStep ) - 1;
- prev = prev >= 0 ? steps[ prev ] : steps[ steps.length - 1 ];
- return goto( prev );
- };
- // `next` API function goes to next step (in document order)
- var next = function() {
- var next = steps.indexOf( activeStep ) + 1;
- next = next < steps.length ? steps[ next ] : steps[ 0 ];
- return goto( next );
- };
- // Adding some useful classes to step elements.
- //
- // All the steps that have not been shown yet are given `future` class.
- // When the step is entered the `future` class is removed and the `present`
- // class is given. When the step is left `present` class is replaced with
- // `past` class.
- //
- // So every step element is always in one of three possible states:
- // `future`, `present` and `past`.
- //
- // There classes can be used in CSS to style different types of steps.
- // For example the `present` class can be used to trigger some custom
- // animations when step is shown.
- root.addEventListener( "impress:init", function() {
- // STEP CLASSES
- steps.forEach( function( step ) {
- step.classList.add( "future" );
- } );
- root.addEventListener( "impress:stepenter", function( event ) {
- event.target.classList.remove( "past" );
- event.target.classList.remove( "future" );
- event.target.classList.add( "present" );
- }, false );
- root.addEventListener( "impress:stepleave", function( event ) {
- event.target.classList.remove( "present" );
- event.target.classList.add( "past" );
- }, false );
- }, false );
- // Adding hash change support.
- root.addEventListener( "impress:init", function() {
- // Last hash detected
- var lastHash = "";
- // `#/step-id` is used instead of `#step-id` to prevent default browser
- // scrolling to element in hash.
- //
- // And it has to be set after animation finishes, because in Chrome it
- // makes transtion laggy.
- // BUG: http://code.google.com/p/chromium/issues/detail?id=62820
- root.addEventListener( "impress:stepenter", function( event ) {
- window.location.hash = lastHash = "#/" + event.target.id;
- }, false );
- window.addEventListener( "hashchange", function() {
- // When the step is entered hash in the location is updated
- // (just few lines above from here), so the hash change is
- // triggered and we would call `goto` again on the same element.
- //
- // To avoid this we store last entered hash and compare.
- if ( window.location.hash !== lastHash ) {
- goto( getElementFromHash() );
- }
- }, false );
- // START
- // by selecting step defined in url or first step of the presentation
- goto( getElementFromHash() || steps[ 0 ], 0 );
- }, false );
- body.classList.add( "impress-disabled" );
- // Store and return API for given impress.js root element
- return ( /*roots[ "impress-root-" + rootId ] = */ {
- init: init,
- goto: goto,
- next: next,
- prev: prev,
- addStep: addStep,
- removeStep: removeStep,
- } );
- };
- // Flag that can be used in JS to check if browser have passed the support test
- impress.supported = impressSupported;
- return impress;
- }, document, window));
- // THAT'S ALL FOLKS!
- //
- // Thanks for reading it all.
- // Or thanks for scrolling down and reading the last part.
- //
- // I've learnt a lot when building impress.js and I hope this code and comments
- // will help somebody learn at least some part of it.
|