123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
-
- const Plugin = {
-
- id: 'zoom',
-
- init: function( reveal ) {
-
- reveal.getRevealElement().addEventListener( 'mousedown', function( event ) {
- var defaultModifier = /Linux/.test( window.navigator.platform ) ? 'ctrl' : 'alt';
-
- var modifier = ( reveal.getConfig().zoomKey ? reveal.getConfig().zoomKey : defaultModifier ) + 'Key';
- var zoomLevel = ( reveal.getConfig().zoomLevel ? reveal.getConfig().zoomLevel : 2 );
-
- if( event[ modifier ] && !reveal.isOverview() ) {
- event.preventDefault();
-
- zoom.to({
- x: event.clientX,
- y: event.clientY,
- scale: zoomLevel,
- pan: false
- });
- }
- } );
-
- },
-
- destroy: () => {
-
- zoom.reset();
-
- }
-
- };
-
- export default () => Plugin;
-
-
- var zoom = (function(){
-
-
- var level = 1;
-
-
- var mouseX = 0,
- mouseY = 0;
-
-
- var panEngageTimeout = -1,
- panUpdateInterval = -1;
-
-
- var supportsTransforms = 'transform' in document.body.style;
-
- if( supportsTransforms ) {
-
- document.body.style.transition = 'transform 0.8s ease';
- }
-
-
- document.addEventListener( 'keyup', function( event ) {
- if( level !== 1 && event.keyCode === 27 ) {
- zoom.out();
- }
- } );
-
-
- document.addEventListener( 'mousemove', function( event ) {
- if( level !== 1 ) {
- mouseX = event.clientX;
- mouseY = event.clientY;
- }
- } );
-
-
-
- function magnify( rect, scale ) {
-
- var scrollOffset = getScrollOffset();
-
-
- rect.width = rect.width || 1;
- rect.height = rect.height || 1;
-
-
- rect.x -= ( window.innerWidth - ( rect.width * scale ) ) / 2;
- rect.y -= ( window.innerHeight - ( rect.height * scale ) ) / 2;
-
- if( supportsTransforms ) {
-
- if( scale === 1 ) {
- document.body.style.transform = '';
- }
-
- 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.transform = transform;
- }
- }
- else {
-
- 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 = '';
- }
-
- else {
- document.body.style.position = 'relative';
- 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( document.documentElement.classList ) {
- if( level !== 1 ) {
- document.documentElement.classList.add( 'zoomed' );
- }
- else {
- document.documentElement.classList.remove( 'zoomed' );
- }
- }
- }
-
-
-
- function pan() {
- var range = 0.12,
- rangeX = window.innerWidth * range,
- rangeY = window.innerHeight * range,
- scrollOffset = getScrollOffset();
-
-
- if( mouseY < rangeY ) {
- window.scroll( scrollOffset.x, scrollOffset.y - ( 1 - ( mouseY / rangeY ) ) * ( 14 / level ) );
- }
-
- else if( mouseY > window.innerHeight - rangeY ) {
- window.scroll( scrollOffset.x, scrollOffset.y + ( 1 - ( window.innerHeight - mouseY ) / rangeY ) * ( 14 / level ) );
- }
-
-
- if( mouseX < rangeX ) {
- window.scroll( scrollOffset.x - ( 1 - ( mouseX / rangeX ) ) * ( 14 / level ), scrollOffset.y );
- }
-
- 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.pageYOffset
- }
- }
-
- return {
-
-
- to: function( options ) {
-
-
-
- if( level !== 1 ) {
- zoom.out();
- }
- else {
- options.x = options.x || 0;
- options.y = options.y || 0;
-
-
- if( !!options.element ) {
-
- var padding = 20;
- var bounds = options.element.getBoundingClientRect();
-
- options.x = bounds.left - padding;
- options.y = bounds.top - padding;
- options.width = bounds.width + ( padding * 2 );
- options.height = bounds.height + ( padding * 2 );
- }
-
-
- 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;
-
- magnify( options, options.scale );
-
- if( options.pan !== false ) {
-
-
-
- panEngageTimeout = setTimeout( function() {
- panUpdateInterval = setInterval( pan, 1000 / 60 );
- }, 800 );
-
- }
- }
- }
- },
-
-
-
- out: function() {
- clearTimeout( panEngageTimeout );
- clearInterval( panUpdateInterval );
-
- magnify( { x: 0, y: 0 }, 1 );
-
- level = 1;
- },
-
-
- magnify: function( options ) { this.to( options ) },
- reset: function() { this.out() },
-
- zoomLevel: function() {
- return level;
- }
- }
-
- })();
|