UNPKG

@whee/js-motion

Version:
7 lines (6 loc) 4.7 kB
/*! * @whee/js-motion * v1.1.0 * by mirages */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self)["@whee/js-motion"]=e()}(this,(function(){"use strict";var t=function(){};return function(){function e(e){void 0===e&&(e={}),this.mainFinger=0,this.trendData=[],this.trendLength=4,this.prevData={},this.renderData=null,this.frameId=0,this.rendering=!1,this.accumulation=6,this.tmThreshold=50,this.touchstartHandler=t,this.touchmoveHandler=t,this.touchendHandler=t,this.el=e.target?this.getEl(e.target):null,this.mode=e.mode||"realtime",this.direction=e.direction||"xy",this.coordinate=e.coordinate||"page",this.initEvent()}return e.prototype.getEl=function(t){return"string"==typeof t?document.querySelector(t):t},e.prototype.initEvent=function(){var t=this;this.el&&(this.el.addEventListener("touchstart",(function(e){t.touchstart(e),t.touchstartHandler(e)}),!!e.isSupportPassive&&{passive:!1,capture:!0}),this.el.addEventListener("touchmove",(function(e){e.preventDefault(),t.touchmove(e,(function(i){t.touchmoveHandler(i,e)}))}),!!e.isSupportPassive&&{passive:!1,capture:!0}),this.el.addEventListener("touchend",(function(e){t.touchend(e,(function(i){t.touchendHandler(i,e)}))})))},e.prototype.createData=function(t,e){void 0===e&&(e="targetTouches");var i=t[e][0],n=t[e][1],a=this.coordinate+"X",r=this.coordinate+"Y",o={x:i[a],y:i[r],t:Date.now(),l:0,a:0};return n&&(o.l=Math.sqrt(Math.pow(i[a]-n[r],2)+Math.pow(i[a]-n[r],2)),o.a=180*Math.atan((n[r]-i[r])/(n[a]-i[a]))/Math.PI),this.setTrendData(o),o},e.prototype.setTrendData=function(t){if(this.trendData.length<1)this.trendData.push(t);else{var e=this.trendData[this.trendData.length-1],i=t.t-e.t,n=t.x-e.x,a=t.y-e.y;i>this.tmThreshold&&Math.sqrt(Math.pow(n,2)+Math.pow(a,2))/i<.3?this.trendData=[]:this.trendData.push(t),this.trendData.length>this.trendLength&&this.trendData.shift()}},e.prototype.getMoveData=function(t){var e=this.prevData,i={x:t.x-e.x,y:t.y-e.y,t:t.t-e.t,scale:0===t.l||0===e.l?1:t.l/e.l,angle:e.a>0&&t.a<-80?t.a-e.a+180:e.a<0&&t.a>80?t.a-e.a-180:t.a-e.a};return this.prevData=t,i},e.prototype.isNeedInertiaScroll=function(){return this.trendData.length>1},e.prototype.inertiaScroll=function(t){var e=this,i=this.trendData[0],n=this.trendData[this.trendData.length-1],a={x:n.x-i.x,y:n.y-i.y,t:n.t-i.t},r=this.getMoveStep(a.x,a.t),o=this.getMoveStep(a.y,a.t);"x"===this.direction?o=function(){return 0}:"y"===this.direction&&(r=function(){return 0});var s=function(){var i=r(),n=o();0===i&&0===n||(e.frameId=requestAnimationFrame(s)),t({x:i,y:n,angle:0,scale:1})};s()},e.prototype.getMoveStep=function(t,e){var i=this,n=t/e,a=n/e/10,r=n,o=.06*-a;return function(){var t=r/n>.4?o+.02*a:o-.015*a,e=r-o*i.accumulation,s=(r+e)/2*i.accumulation;return i.isMoveStop(r,e)?(s=0,r=0,o=0):(r=e,o=t),s}},e.prototype.isMoveStop=function(t,e){return t===1/0||t===-1/0||0===t||t/e<0},e.prototype.moveFrame=function(t,e){var i=this,n=this.createData(t);this.renderData=n,this.rendering||(this.rendering=!0,this.frameId=requestAnimationFrame((function(){var t=i.getMoveData(i.renderData),n={x:"y"!==i.direction?t.x:0,y:"x"!==i.direction?t.y:0,scale:t.scale,angle:t.angle};e(n),i.rendering=!1})))},e.prototype.moveRealtime=function(t,e){var i=this.createData(t),n=this.getMoveData(i);e({x:"y"!==this.direction?n.x:0,y:"x"!==this.direction?n.y:0,scale:n.scale,angle:n.angle})},e.prototype.onTouchstart=function(e){void 0===e&&(e=t),this.touchstartHandler=e},e.prototype.onTouchmove=function(e){void 0===e&&(e=t),this.touchmoveHandler=e},e.prototype.onTouchend=function(e){void 0===e&&(e=t),this.touchendHandler=e},e.prototype.touchstart=function(t){var e=t.targetTouches,i=t.changedTouches;if(e.length===i.length){var n=t.changedTouches[0];this.trendData=[],this.mainFinger=n.identifier,this.clearInertiaScroll()}this.prevData=this.createData(t)},e.prototype.touchmove=function(e,i){void 0===i&&(i=t),e.targetTouches[0].identifier===this.mainFinger&&("frame"===this.mode?this.moveFrame(e,i):this.moveRealtime(e,i))},e.prototype.touchend=function(e,i){void 0===i&&(i=t);var n=e.targetTouches;if(n.length>0){var a=n[0];this.prevData=this.createData(e),this.mainFinger!==a.identifier&&(this.trendData=[],this.mainFinger=a.identifier)}else this.createData(e,"changedTouches"),this.isNeedInertiaScroll()?this.inertiaScroll(i):i({x:0,y:0,angle:0,scale:1})},e.prototype.clearInertiaScroll=function(){cancelAnimationFrame(this.frameId)},e.isSupportPassive=function(){var e=!1;try{document.createElement("div").addEventListener("testPassive",t,{get passive(){return e=!0,!1}})}catch(t){}return e}(),e}()}));