-
Notifications
You must be signed in to change notification settings - Fork 526
/
index.esm.min.js
1 lines (1 loc) · 8.54 KB
/
index.esm.min.js
1
Date.now||(Date.now=function(){return new Date().getTime()});for(var vendors=["webkit","moz"],i=0;i<vendors.length&&!window.requestAnimationFrame;++i){var t=vendors[i];window.requestAnimationFrame=window[t+"RequestAnimationFrame"],window.cancelAnimationFrame=window[t+"CancelAnimationFrame"]||window[t+"CancelRequestAnimationFrame"]}if(/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent)||!window.requestAnimationFrame||!window.cancelAnimationFrame){var e=0;window.requestAnimationFrame=function(t){var s=Date.now(),n=Math.max(e+16,s);return setTimeout(function(){t(e=n)},n-s)},window.cancelAnimationFrame=clearTimeout}function on(t,e,s,n){var h=n||!1;return t.addEventListener(e,s,h),function n(){t.removeEventListener(e,s,h)}}function ease(t){return Math.sqrt(1-Math.pow(t-1,2))}function reverseEase(t){return 1-Math.sqrt(1-t*t)}function preventDefaultTest(t,e){for(var s in e)if(e[s].test(t[s]))return!0;return!1}function getEvents(){let t=["down","move","up","cancel"];return"PointerEvent"in window||window.navigator&&"msPointerEnabled"in window.navigator?t.map(t=>"pointer"+t):"ontouchstart"in window||navigator.maxTouchPoints>0||navigator.msMaxTouchPoints>0?["touchstart","touchmove","touchend","touchcancel"]:t.map(t=>"mouse"+t)}var events=getEvents(),noop=function(){},G=function(t,e){return void 0===t?e:t};export var PhyTouch=function(t){this.reverse=G(t.reverse,!1),this.element="string"==typeof t.touch?document.querySelector(t.touch):t.touch,this.target=G(t.target,this.element);var e=G(t.followers,[]);this.followers=e.map(function(t){return{element:"string"==typeof t.element?document.querySelector(t.element):t.element,offset:t.offset}}),this.vertical=G(t.vertical,!0),this.property=t.property,this.tickID=0,this.value=G(t.value,this.target[this.property]),this.target[this.property]=this.value,this.followers.forEach((function(t){t.element[this.property]=this.value+t.offset}).bind(this)),this.fixed=G(t.fixed,!1),this.sensitivity=G(t.sensitivity,1),this.moveFactor=G(t.moveFactor,1),this.factor=G(t.factor,1),this.outFactor=G(t.outFactor,.3),this.min=t.min,this.max=t.max,this.deceleration=G(t.deceleration,6e-4),this.maxRegion=G(t.maxRegion,600),this.springMaxRegion=G(t.springMaxRegion,60),this.maxSpeed=t.maxSpeed,this.hasMaxSpeed=void 0!==this.maxSpeed,this.lockDirection=G(t.lockDirection,!0);var s=function(){return!0};this.change=t.change||noop,this.touchEnd=t.touchEnd||noop,this.touchStart=t.touchStart||noop,this.touchMove=t.touchMove||noop,this.touchCancel=t.touchCancel||noop,this.reboundEnd=t.reboundEnd||noop,this.animationEnd=t.animationEnd||noop,this.correctionEnd=t.correctionEnd||noop,this.tap=t.tap||noop,this.pressMove=t.pressMove||noop,this.shouldRebound=t.shouldRebound||s,this.preventDefault=G(t.preventDefault,!0),this.preventDefaultException={tagName:/^(INPUT|TEXTAREA|BUTTON|SELECT)$/},this.hasMin=void 0!==this.min,this.hasMax=void 0!==this.max,this.isTouchStart=!1,this.step=t.step,this.inertia=G(t.inertia,!0),this._calculateIndex(),this.eventTarget=window,t.bindSelf&&(this.eventTarget=this.element);var n=this;(function t(e){return n[e]=n[e].bind(n),t})("_start")("_end")("_cancel")("_move");var h=on(this.element,events[0],this._start),a=on(this.eventTarget,events[1],this._move,{passive:!1,capture:!1}),r=on(this.eventTarget,events[2],this._end),o=on(this.eventTarget,events[3],this._cancel);this.destory=function(){h(),a(),r(),o(),n.followers=n.element=n.target=null,cancelAnimationFrame(n.tickID)},this.x1=this.x2=this.y1=this.y2=null};PhyTouch.prototype={_events:getEvents(),isAtMax:function(){return this.hasMax&&this.target[this.property]>=this.max},isAtMin:function(){return this.hasMin&&this.target[this.property]<=this.min},stop:function(){cancelAnimationFrame(this.tickID),this._calculateIndex()},_start:function(t){var e=t.touches?t.touches[0]:t;this.isTouchStart=!0,this.touchStart.call(this,t,this.target[this.property]),cancelAnimationFrame(this.tickID),this._calculateIndex(),this.startTime=new Date().getTime(),this.x1=this.preX=e.pageX,this.y1=this.preY=e.pageY,this.start=this.vertical?this.preY:this.preX,this._firstTouchMove=!0,this._preventMove=!1},_move:function(t){if(this.isTouchStart){var e=t.touches?t.touches[0]:t,s=t.touches?t.touches.length:1,n=e.pageX,h=e.pageY;if(this._firstTouchMove&&this.lockDirection){var a=Math.abs(n-this.x1)-Math.abs(h-this.y1);a>0&&this.vertical?this._preventMove=!0:a<0&&!this.vertical&&(this._preventMove=!0),this._firstTouchMove=!1}if(!this._preventMove){var r=(this.vertical?h-this.preY:n-this.preX)*this.sensitivity,o=this.moveFactor;if(this.isAtMax()&&(this.reverse?-r:r)>0?o=this.outFactor:this.isAtMin()&&(this.reverse?-r:r)<0&&(o=this.outFactor),r*=o,this.preX=n,this.preY=h,!this.fixed){var c=this.reverse?-r:r;this.target[this.property]+=c,this.followers.forEach((function(t){t.element[this.property]+=c}).bind(this))}this.change.call(this,this.target[this.property]);var u=new Date().getTime();u-this.startTime>300&&(this.startTime=u,this.start=this.vertical?this.preY:this.preX),this.touchMove.call(this,t,this.target[this.property])}this.preventDefault&&!preventDefaultTest(t.target,this.preventDefaultException)&&t.preventDefault(),1===s&&(null!==this.x2?(t.deltaX=n-this.x2,t.deltaY=h-this.y2):(t.deltaX=0,t.deltaY=0),this.pressMove.call(this,t,this.target[this.property])),this.x2=n,this.y2=h}},_cancel:function(t){var e=this.target[this.property];this.touchCancel.call(this,t,e),this._end(t)},to:function(t,e,s,n){this._to(t,G(e,600),s||ease,this.change,(function(t){this._calculateIndex(),this.reboundEnd.call(this,t),this.animationEnd.call(this,t),n&&n.call(this,t)}).bind(this))},_calculateIndex:function(){this.hasMax&&this.hasMin&&(this.currentPage=Math.round((this.max-this.target[this.property])/this.step))},_end:function(t){var e=t.changedTouches?t.changedTouches[0]:t;if(this.isTouchStart){this.isTouchStart=!1;var s=e.pageX,n=e.pageY,h=this,a=this.target[this.property],r=30>Math.abs(s-this.x1)&&30>Math.abs(n-this.y1);if(r&&this.tap.call(this,t,a),!1===this.touchEnd.call(this,t,a,this.currentPage))return;if(this.hasMax&&a>this.max){if(!this.shouldRebound(a))return;this._to(this.max,200,ease,this.change,(function(t){this.reboundEnd.call(this,t),this.animationEnd.call(this,t)}).bind(this))}else if(this.hasMin&&a<this.min){if(!this.shouldRebound(a))return;this._to(this.min,200,ease,this.change,(function(t){this.reboundEnd.call(this,t),this.animationEnd.call(this,t)}).bind(this))}else if(!this.inertia||r||this._preventMove||this.fixed)h._correction();else{var o=new Date().getTime()-this.startTime;if(o<300){var c=((this.vertical?n:s)-this.start)*this.sensitivity,u=Math.abs(c)/o,l=this.factor*u;this.hasMaxSpeed&&l>this.maxSpeed&&(l=this.maxSpeed);var p=c<0?-1:1;this.reverse&&(p=-p);var v=a+l*l/(2*this.deceleration)*p,m=1;v<this.min?v<this.min-this.maxRegion?(m=reverseEase((a-this.min+this.springMaxRegion)/(a-v)),v=this.min-this.springMaxRegion):(m=reverseEase((a-this.min+this.springMaxRegion*(this.min-v)/this.maxRegion)/(a-v)),v=this.min-this.springMaxRegion*(this.min-v)/this.maxRegion):v>this.max&&(v>this.max+this.maxRegion?(m=reverseEase((this.max+this.springMaxRegion-a)/(v-a)),v=this.max+this.springMaxRegion):(m=reverseEase((this.max+this.springMaxRegion*(v-this.max)/this.maxRegion-a)/(v-a)),v=this.max+this.springMaxRegion*(v-this.max)/this.maxRegion));var d=Math.round(u/h.deceleration)*m;isNaN(v)||h._to(Math.round(v),d,ease,h.change,function(t){if(h.hasMax&&h.target[h.property]>h.max){if(!this.shouldRebound(h.target[h.property]))return;cancelAnimationFrame(h.tickID),h._to(h.max,600,ease,h.change,h.animationEnd)}else if(h.hasMin&&h.target[h.property]<h.min){if(!this.shouldRebound(h.target[h.property]))return;cancelAnimationFrame(h.tickID),h._to(h.min,600,ease,h.change,h.animationEnd)}else h.step?h._correction():h.animationEnd.call(h,t);h.change.call(this,t)})}else h._correction()}}this.x1=this.x2=this.y1=this.y2=null},_to:function(t,e,s,n,h){var a=this.target,r=this.property,o=this.followers,c=a[r],u=t-c,l=+new Date,p=this,v=function(){var m=+new Date-l;if(m>=e){a[r]=t,n&&n.call(p,t),h&&h.call(p,t);return}var d=u*s(m/e)+c;a[r]=d,o.forEach(function(t){t.element[r]=d+t.offset}),p.tickID=requestAnimationFrame(v),n&&n.call(p,a[r])};v()},_correction:function(){if(void 0!==this.step){var t,e=this.target[this.property],s=Math.floor(Math.abs(e/this.step));Math.abs(e%this.step)>this.step/2?this._to((e<0?-1:1)*(s+1)*this.step,400,ease,this.change,(function(t){this._calculateIndex(),this.correctionEnd.call(this,t),this.animationEnd.call(this,t)}).bind(this)):this._to((e<0?-1:1)*s*this.step,400,ease,this.change,(function(t){this._calculateIndex(),this.correctionEnd.call(this,t),this.animationEnd.call(this,t)}).bind(this))}},destory:noop};export default PhyTouch;