zoom.min.js 8.5 KB

1
  1. !function(o,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(o="undefined"!=typeof globalThis?globalThis:o||self).Zoom=t()}(this,function(){"use strict";var o=[{key:"zoomTo",name:"zoomTo"}].map(function(o){return{key:o.key,sourceKey:"plugins.zoom."+o.name}}),r="undefined"!=typeof window,t=r&&navigator.userAgent.toLowerCase();t&&/wechatdevtools/.test(t),t&&t.indexOf("android"),function(){if("string"==typeof t){var o=/os (\d\d?_\d(_\d)?)/.exec(t);if(!o)return;o=o[1].split("_").map(function(o){return parseInt(o,10)});13===o[0]&&o[1]}}();if(r)try{var e={};Object.defineProperty(e,"passive",{get:function(){}}),window.addEventListener("test-passive",function(){},e)}catch(o){}function p(){return window.performance&&window.performance.now&&window.performance.timing?window.performance.now()+window.performance.timing.navigationStart:+new Date}function m(o,t,e){return o<t?t:e<o?e:o}var n=r&&document.createElement("div").style,i=function(){if(!r)return!1;for(var o=0,t=[{key:"standard",value:"transform"},{key:"webkit",value:"webkitTransform"},{key:"Moz",value:"MozTransform"},{key:"O",value:"OTransform"},{key:"ms",value:"msTransform"}];o<t.length;o++){var e=t[o];if(void 0!==n[e.value])return e.key}return!1}();function s(o){return!1===i?o:"standard"===i?"transitionEnd"===o?"transitionend":o:i+o.charAt(0).toUpperCase()+o.substr(1)}i&&"standard"!==i&&i.toLowerCase();s("transform"),s("transition");r&&s("perspective")in n;s("transitionTimingFunction"),s("transitionDuration"),s("transitionDelay");var a=s("transformOrigin");s("transitionEnd"),s("transitionProperty");function c(o){if(o instanceof window.SVGElement){var t=o.getBoundingClientRect();return{top:t.top,left:t.left,width:t.width,height:t.height}}return{top:o.offsetTop,left:o.offsetLeft,width:o.offsetWidth,height:o.offsetHeight}}var g={style:"cubic-bezier(0.165, 0.84, 0.44, 1)",fn:function(o){return 1- --o*o*o*o}},l=r&&window;function h(){}var d=r?l.requestAnimationFrame||l.webkitRequestAnimationFrame||l.mozRequestAnimationFrame||l.oRequestAnimationFrame||function(o){return window.setTimeout(o,o.interval||1e3/60)}:h,v=r?l.cancelAnimationFrame||l.webkitCancelAnimationFrame||l.mozCancelAnimationFrame||l.oCancelAnimationFrame||function(o){window.clearTimeout(o)}:h;function u(o){this.scroll=o,this.scale=1,this.prevScale=1,this.init()}return u.prototype.init=function(){this.handleBScroll(),this.handleOptions(),this.handleHooks(),this.tryInitialZoomTo(this.zoomOpt)},u.prototype.zoomTo=function(o,t,e,r){e=this.resolveOrigin(t,e),e={x:e.originX,y:e.originY,baseScale:this.scale};this._doZoomTo(o,e,r,!0)},u.prototype.handleBScroll=function(){this.scroll.proxy(o),this.scroll.registerType(["beforeZoomStart","zoomStart","zooming","zoomEnd"])},u.prototype.handleOptions=function(){var o=!0===this.scroll.options.zoom?{}:this.scroll.options.zoom;this.zoomOpt=function(o,t){for(var e in t)o[e]=t[e];return o}({start:1,min:1,max:4,initialOrigin:[0,0],minimalZoomDistance:5,bounceTime:800},o)},u.prototype.handleHooks=function(){var e=this,o=this.scroll,t=this.scroll.scroller;this.wrapper=this.scroll.scroller.wrapper,this.setTransformOrigin(this.scroll.scroller.content);var r=t.scrollBehaviorX,n=t.scrollBehaviorY;this.hooksFn=[],this.registerHooks(o.hooks,o.hooks.eventTypes.contentChanged,function(o){e.setTransformOrigin(o),e.scale=1,e.tryInitialZoomTo(e.zoomOpt)}),this.registerHooks(o.hooks,o.hooks.eventTypes.beforeInitialScrollTo,function(){if(1!==e.zoomOpt.start)return!0}),this.registerHooks(r.hooks,r.hooks.eventTypes.beforeComputeBoundary,function(){var o=c(e.scroll.scroller.content);r.contentSize=Math.floor(o.width*e.scale)}),this.registerHooks(n.hooks,n.hooks.eventTypes.beforeComputeBoundary,function(){var o=c(e.scroll.scroller.content);n.contentSize=Math.floor(o.height*e.scale)}),this.registerHooks(t.actions.hooks,t.actions.hooks.eventTypes.start,function(o){var t=o.touches&&o.touches.length||0;e.fingersOperation(t),2===t&&e.zoomStart(o)}),this.registerHooks(t.actions.hooks,t.actions.hooks.eventTypes.beforeMove,function(o){var t=o.touches&&o.touches.length||0;if(e.fingersOperation(t),2===t)return e.zoom(o),!0}),this.registerHooks(t.actions.hooks,t.actions.hooks.eventTypes.beforeEnd,function(o){if(2===e.fingersOperation())return e.zoomEnd(),!0}),this.registerHooks(t.translater.hooks,t.translater.hooks.eventTypes.beforeTranslate,function(o,t){t=t.scale||e.prevScale;e.prevScale=t,o.push("scale("+t+")")}),this.registerHooks(t.hooks,t.hooks.eventTypes.scrollEnd,function(){2===e.fingersOperation()&&e.scroll.trigger(e.scroll.eventTypes.zoomEnd,{scale:e.scale})}),this.registerHooks(this.scroll.hooks,"destroy",this.destroy)},u.prototype.setTransformOrigin=function(o){o.style[a]="0 0"},u.prototype.tryInitialZoomTo=function(o){var t=o.start,e=o.initialOrigin,r=this.scroll.scroller,o=r.scrollBehaviorX,r=r.scrollBehaviorY;1!==t&&(this.resetBoundaries([o,r]),this.zoomTo(t,e[0],e[1],0))},u.prototype.fingersOperation=function(o){if("number"!=typeof o)return this.numberOfFingers;this.numberOfFingers=o},u.prototype._doZoomTo=function(o,t,e,r){var n=this;void 0===e&&(e=this.zoomOpt.bounceTime),void 0===r&&(r=!1);var i,s,a,c,l=this.zoomOpt,h=l.min,l=l.max,u=this.scale,f=m(o,h,l);0!==e?0<e&&(s=p(),a=s+e,(c=function(){var o=p();if(a<=o)return n.scroll.trigger(n.scroll.eventTypes.zooming,{scale:f}),void v(i);o=g.fn((o-s)/e);n.scroll.trigger(n.scroll.eventTypes.zooming,{scale:o*(f-u)+u}),i=d(c)})()):n.scroll.trigger(n.scroll.eventTypes.zooming,{scale:f}),this.fingersOperation(2),this._zoomTo(f,u,t,e,r)},u.prototype._zoomTo=function(o,t,e,r,n){void 0===n&&(n=!1);var i=o/e.baseScale;this.setScale(o);var s=this.scroll.scroller,a=s.scrollBehaviorX,c=s.scrollBehaviorY;this.resetBoundaries([a,c]);var l=this.getNewPos(e.x,i,a,!0,n),n=this.getNewPos(e.y,i,c,!0,n);a.currentPos===Math.round(l)&&c.currentPos===Math.round(n)&&o===t||s.scrollTo(l,n,r,g,{start:{scale:t},end:{scale:o}})},u.prototype.resolveOrigin=function(o,t){var e=this.scroll.scroller,r=e.scrollBehaviorX,n=e.scrollBehaviorY,e={left:function(){return 0},top:function(){return 0},right:function(){return r.contentSize},bottom:function(){return n.contentSize},center:function(o){return(0===o?r:n).contentSize/2}};return{originX:"number"==typeof o?o:e[o](0),originY:"number"==typeof t?t:e[t](1)}},u.prototype.zoomStart=function(o){var t=o.touches[0],e=o.touches[1];this.startDistance=this.getFingerDistance(o),this.startScale=this.scale;var r,o={left:-((r=(r=this.wrapper).getBoundingClientRect()).left+window.pageXOffset),top:-(r.top+window.pageYOffset)},r=o.left,o=o.top;this.origin={x:Math.abs(t.pageX+e.pageX)/2+r-this.scroll.x,y:Math.abs(t.pageY+e.pageY)/2+o-this.scroll.y,baseScale:this.startScale},this.scroll.trigger(this.scroll.eventTypes.beforeZoomStart)},u.prototype.zoom=function(o){var t,e,r,n=this.getFingerDistance(o);!this.zoomed&&Math.abs(n-this.startDistance)<this.zoomOpt.minimalZoomDistance||(e=(t=this.dampingScale(n/this.startDistance*this.startScale))/this.startScale,this.setScale(t),this.zoomed||(this.zoomed=!0,this.scroll.trigger(this.scroll.eventTypes.zoomStart)),o=(r=this.scroll.scroller).scrollBehaviorX,n=r.scrollBehaviorY,o=this.getNewPos(this.origin.x,e,o,!1,!1),n=this.getNewPos(this.origin.y,e,n,!1,!1),this.scroll.trigger(this.scroll.eventTypes.zooming,{scale:this.scale}),r.translater.translate({x:o,y:n,scale:t}))},u.prototype.zoomEnd=function(){this.zoomed&&(this.shouldRebound()?this._doZoomTo(this.scale,this.origin,this.zoomOpt.bounceTime):this.scroll.trigger(this.scroll.eventTypes.zoomEnd,{scale:this.scale}))},u.prototype.getFingerDistance=function(o){var t=o.touches[0],e=o.touches[1],o=Math.abs(t.pageX-e.pageX),e=Math.abs(t.pageY-e.pageY);return Math.sqrt(o*o+e*e)},u.prototype.shouldRebound=function(){var o=this.zoomOpt,t=o.min,e=o.max,o=this.scale;if(o!==m(o,t,e))return!0;t=this.scroll.scroller,e=t.scrollBehaviorX,t=t.scrollBehaviorY;this.resetBoundaries([e,t]);t=e.checkInBoundary().inBoundary,e=e.checkInBoundary().inBoundary;return!(t&&e)},u.prototype.dampingScale=function(o){var t=this.zoomOpt,e=t.min,t=t.max;return o<e?o=.5*e*Math.pow(2,o/e):t<o&&(o=2*t*Math.pow(.5,t/o)),o},u.prototype.setScale=function(o){this.scale=o},u.prototype.resetBoundaries=function(o){o.forEach(function(o){return o.computeBoundary()})},u.prototype.getNewPos=function(o,t,e,r,n){n=o-o*t+((n=void 0===n?!1:n)?e.currentPos:e.startPos);return 0<(n=r?m(n,e.maxScrollPos,e.minScrollPos):n)?Math.floor(n):Math.ceil(n)},u.prototype.registerHooks=function(o,t,e){o.on(t,e,this),this.hooksFn.push([o,t,e])},u.prototype.destroy=function(){this.hooksFn.forEach(function(o){var t=o[0],e=o[1],o=o[2];t.off(e,o)}),this.hooksFn.length=0},u.pluginName="zoom",u});