Path: blob/main/src/resources/formats/revealjs/reveal/plugin/zoom/plugin.js
12923 views
/*!1* reveal.js Zoom plugin2*/3const Plugin = {45id: 'zoom',67init: function( reveal ) {89reveal.getRevealElement().addEventListener( 'mousedown', function( event ) {10var defaultModifier = /Linux/.test( window.navigator.platform ) ? 'ctrl' : 'alt';1112var modifier = ( reveal.getConfig().zoomKey ? reveal.getConfig().zoomKey : defaultModifier ) + 'Key';13var zoomLevel = ( reveal.getConfig().zoomLevel ? reveal.getConfig().zoomLevel : 2 );1415if( event[ modifier ] && !reveal.isOverview() ) {16event.preventDefault();1718zoom.to({19x: event.clientX,20y: event.clientY,21scale: zoomLevel,22pan: false23});24}25} );2627},2829destroy: () => {3031zoom.reset();3233}3435};3637export default () => Plugin;3839/*!40* zoom.js 0.3 (modified for use with reveal.js)41* http://lab.hakim.se/zoom-js42* MIT licensed43*44* Copyright (C) 2011-2014 Hakim El Hattab, http://hakim.se45*/46var zoom = (function(){4748// The current zoom level (scale)49var level = 1;5051// The current mouse position, used for panning52var mouseX = 0,53mouseY = 0;5455// Timeout before pan is activated56var panEngageTimeout = -1,57panUpdateInterval = -1;5859// Check for transform support so that we can fallback otherwise60var supportsTransforms = 'transform' in document.body.style;6162if( supportsTransforms ) {63// The easing that will be applied when we zoom in/out64document.body.style.transition = 'transform 0.8s ease';65}6667// Zoom out if the user hits escape68document.addEventListener( 'keyup', function( event ) {69if( level !== 1 && event.keyCode === 27 ) {70zoom.out();71}72} );7374// Monitor mouse movement for panning75document.addEventListener( 'mousemove', function( event ) {76if( level !== 1 ) {77mouseX = event.clientX;78mouseY = event.clientY;79}80} );8182/**83* Applies the CSS required to zoom in, prefers the use of CSS384* transforms but falls back on zoom for IE.85*86* @param {Object} rect87* @param {Number} scale88*/89function magnify( rect, scale ) {9091var scrollOffset = getScrollOffset();9293// Ensure a width/height is set94rect.width = rect.width || 1;95rect.height = rect.height || 1;9697// Center the rect within the zoomed viewport98rect.x -= ( window.innerWidth - ( rect.width * scale ) ) / 2;99rect.y -= ( window.innerHeight - ( rect.height * scale ) ) / 2;100101if( supportsTransforms ) {102// Reset103if( scale === 1 ) {104document.body.style.transform = '';105}106// Scale107else {108var origin = scrollOffset.x +'px '+ scrollOffset.y +'px',109transform = 'translate('+ -rect.x +'px,'+ -rect.y +'px) scale('+ scale +')';110111document.body.style.transformOrigin = origin;112document.body.style.transform = transform;113}114}115else {116// Reset117if( scale === 1 ) {118document.body.style.position = '';119document.body.style.left = '';120document.body.style.top = '';121document.body.style.width = '';122document.body.style.height = '';123document.body.style.zoom = '';124}125// Scale126else {127document.body.style.position = 'relative';128document.body.style.left = ( - ( scrollOffset.x + rect.x ) / scale ) + 'px';129document.body.style.top = ( - ( scrollOffset.y + rect.y ) / scale ) + 'px';130document.body.style.width = ( scale * 100 ) + '%';131document.body.style.height = ( scale * 100 ) + '%';132document.body.style.zoom = scale;133}134}135136level = scale;137138if( document.documentElement.classList ) {139if( level !== 1 ) {140document.documentElement.classList.add( 'zoomed' );141}142else {143document.documentElement.classList.remove( 'zoomed' );144}145}146}147148/**149* Pan the document when the mosue cursor approaches the edges150* of the window.151*/152function pan() {153var range = 0.12,154rangeX = window.innerWidth * range,155rangeY = window.innerHeight * range,156scrollOffset = getScrollOffset();157158// Up159if( mouseY < rangeY ) {160window.scroll( scrollOffset.x, scrollOffset.y - ( 1 - ( mouseY / rangeY ) ) * ( 14 / level ) );161}162// Down163else if( mouseY > window.innerHeight - rangeY ) {164window.scroll( scrollOffset.x, scrollOffset.y + ( 1 - ( window.innerHeight - mouseY ) / rangeY ) * ( 14 / level ) );165}166167// Left168if( mouseX < rangeX ) {169window.scroll( scrollOffset.x - ( 1 - ( mouseX / rangeX ) ) * ( 14 / level ), scrollOffset.y );170}171// Right172else if( mouseX > window.innerWidth - rangeX ) {173window.scroll( scrollOffset.x + ( 1 - ( window.innerWidth - mouseX ) / rangeX ) * ( 14 / level ), scrollOffset.y );174}175}176177function getScrollOffset() {178return {179x: window.scrollX !== undefined ? window.scrollX : window.pageXOffset,180y: window.scrollY !== undefined ? window.scrollY : window.pageYOffset181}182}183184return {185/**186* Zooms in on either a rectangle or HTML element.187*188* @param {Object} options189* - element: HTML element to zoom in on190* OR191* - x/y: coordinates in non-transformed space to zoom in on192* - width/height: the portion of the screen to zoom in on193* - scale: can be used instead of width/height to explicitly set scale194*/195to: function( options ) {196197// Due to an implementation limitation we can't zoom in198// to another element without zooming out first199if( level !== 1 ) {200zoom.out();201}202else {203options.x = options.x || 0;204options.y = options.y || 0;205206// If an element is set, that takes precedence207if( !!options.element ) {208// Space around the zoomed in element to leave on screen209var padding = 20;210var bounds = options.element.getBoundingClientRect();211212options.x = bounds.left - padding;213options.y = bounds.top - padding;214options.width = bounds.width + ( padding * 2 );215options.height = bounds.height + ( padding * 2 );216}217218// If width/height values are set, calculate scale from those values219if( options.width !== undefined && options.height !== undefined ) {220options.scale = Math.max( Math.min( window.innerWidth / options.width, window.innerHeight / options.height ), 1 );221}222223if( options.scale > 1 ) {224options.x *= options.scale;225options.y *= options.scale;226227magnify( options, options.scale );228229if( options.pan !== false ) {230231// Wait with engaging panning as it may conflict with the232// zoom transition233panEngageTimeout = setTimeout( function() {234panUpdateInterval = setInterval( pan, 1000 / 60 );235}, 800 );236237}238}239}240},241242/**243* Resets the document zoom state to its default.244*/245out: function() {246clearTimeout( panEngageTimeout );247clearInterval( panUpdateInterval );248249magnify( { x: 0, y: 0 }, 1 );250251level = 1;252},253254// Alias255magnify: function( options ) { this.to( options ) },256reset: function() { this.out() },257258zoomLevel: function() {259return level;260}261}262263})();264265266