-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathTrackAnimation.min.js
More file actions
1 lines (1 loc) · 6.06 KB
/
Copy pathTrackAnimation.min.js
File metadata and controls
1 lines (1 loc) · 6.06 KB
1
var BMapLib=window.BMapLib=BMapLib||{};!function(){var t=!0;function i(){return window.performance&&performance.now?performance.now():Date.now()}BMapLib.TrackAnimation=function(i,e,s){i&&e&&(this._map=i,this._polyline=e,this._opts={duration:1e4,delay:0,overallView:false,followView:t,followViewSmoothing:.18,followViewEdgePadding:.28,onAnimateEnd:null},this._initOpts(s),this._status=2,this._timer=null,this._delayTimer=null,this._startSeq=0,this._startTime=0,this._pauseTime=0,this._pauseAt=0,this._speedFactor=1,this._smoothCenterPx=null,this._totalPath=this._polyline.getPath()||[],this._expandPath=[],this._last2Points=[],this._buildExpandedPath())},BMapLib.TrackAnimation.prototype._initOpts=function(i){if(i){for(var e in i)i.hasOwnProperty(e)&&(this._opts[e]=i[e]);("number"!=typeof this._opts.duration||this._opts.duration<=0)&&(this._opts.duration=1e4),("number"!=typeof this._opts.delay||this._opts.delay<0)&&(this._opts.delay=0),"boolean"!=typeof this._opts.followView&&(this._opts.followView=t),("number"!=typeof this._opts.followViewSmoothing||this._opts.followViewSmoothing<=0||this._opts.followViewSmoothing>1)&&(this._opts.followViewSmoothing=.18),void 0===this._opts.followViewEdgePadding&&void 0!==this._opts.followViewPadding&&(this._opts.followViewEdgePadding=this._opts.followViewPadding),("number"!=typeof this._opts.followViewEdgePadding||this._opts.followViewEdgePadding<0||this._opts.followViewEdgePadding>1)&&(this._opts.followViewEdgePadding=.28)}},BMapLib.TrackAnimation.prototype._buildExpandedPath=function(){var t=this._totalPath||[];if(!t||t.length<2)this._expandPath=t?t.slice(0):[];else{for(var i=Math.max(2,Math.floor(this._opts.duration/10)),e=t.length,s=[],o=0,a=1;a<e;a++){var n=this._map.getDistance(t[a-1],t[a]);s.push(n),o+=n}if(o&&isFinite(o)){for(var h=i,p=[t[0]],r=1;r<e;r++){var _;h-=_=r===e-1?Math.max(1,h):Math.max(1,Math.round(s[r-1]/o*i)),p=p.concat(this._interpolate(t[r-1],t[r],_))}this._expandPath=p}else this._expandPath=t.slice(0)}},BMapLib.TrackAnimation.prototype._interpolate=function(t,i,e){var s=[];if(!e||e<=0)return s;for(var o=1;o<=e;o++){var a=new BMap.Point((i.lng-t.lng)/e*o+t.lng,(i.lat-t.lat)/e*o+t.lat);s.push(a)}return s},BMapLib.TrackAnimation.prototype.start=function(){var t=this;if(this._polyline&&this._map){this._totalPath=this._polyline.getPath()||[],this._buildExpandedPath(),this.cancel();var e=++this._startSeq;this._delayTimer=setTimeout(function(){e===t._startSeq&&(t._polyline.setPath(t._expandPath.slice(0,1)),t._status=1,t._startTime=0,t._pauseTime=0,t._pauseAt=0,t._smoothCenterPx=null,t._step(i()))},this._opts.delay)}},BMapLib.TrackAnimation.prototype.cancel=function(){this._clearRAF(),this._delayTimer&&(clearTimeout(this._delayTimer),this._delayTimer=null),this._startSeq++,this._status=2,this._startTime=0,this._pauseTime=0,this._pauseAt=0,this._smoothCenterPx=null},BMapLib.TrackAnimation.prototype.pause=function(){1===this._status&&(this._clearRAF(),this._status=3,this._pauseAt=i())},BMapLib.TrackAnimation.prototype.continue=function(){if(3===this._status){var t=i();this._pauseTime+=t-this._pauseAt,this._pauseAt=0,this._status=1,this._step(t)}},BMapLib.TrackAnimation.prototype._step=function(t){if(2!==this._status){this._startTime||(this._startTime=t);var i=(t-this._pauseTime-this._startTime)/this._opts.duration;i<0&&(i=0),i>1&&(i=1);var e=Math.max(1,Math.round(this._expandPath.length*i)),s=this._expandPath.slice(0,e);if(this._last2Points=s.slice(-2),this._polyline.setPath(s),this._opts.followView&&this._map&&this._expandPath.length&&"function"==typeof this._map.pointToPixel&&"function"==typeof this._map.pixelToPoint){var o=s[s.length-1];if(o){var a=this._map.getSize&&this._map.getSize();if(a&&a.width&&a.height){var n=this._map.pointToPixel(o),h=this._map.getCenter&&this._map.getCenter(),p=h?this._map.pointToPixel(h):null;if(n&&p){var r=this._opts.followViewEdgePadding,_=a.width/2*(1-r),l=a.height/2*(1-r),m=n.x-p.x,u=n.y-p.y;if(Math.abs(m)>_||Math.abs(u)>l){this._smoothCenterPx||(this._smoothCenterPx={x:p.x,y:p.y});var d=p.x,f=p.y;m>_&&(d+=m-_),m<-_&&(d+=m+_),u>l&&(f+=u-l),u<-l&&(f+=u+l);var c=this._opts.followViewSmoothing;this._smoothCenterPx.x+=(d-this._smoothCenterPx.x)*c,this._smoothCenterPx.y+=(f-this._smoothCenterPx.y)*c;var w=this._map.pixelToPoint(new BMap.Pixel(this._smoothCenterPx.x,this._smoothCenterPx.y));w&&"function"==typeof this._map.setCenter&&this._map.setCenter(w)}else this._smoothCenterPx=null}}}}i<1?this._timer=this._requestFrame(this._step.bind(this)):(this._startTime=0,this._pauseTime=0,this._pauseAt=0,this._status=2,this._opts.overallView&&this._map&&this._totalPath&&this._totalPath.length>=2&&this._map.setViewport&&this._map.setViewport(this._totalPath),"function"==typeof this._opts.onAnimateEnd&&this._opts.onAnimateEnd())}else this._startTime=0},BMapLib.TrackAnimation.prototype._requestFrame=function(t){return window.requestAnimationFrame?window.requestAnimationFrame(t):window.setTimeout(function(){t(i())},16)},BMapLib.TrackAnimation.prototype._cancelFrame=function(t){window.cancelAnimationFrame?window.cancelAnimationFrame(t):window.clearTimeout(t)},BMapLib.TrackAnimation.prototype._clearRAF=function(){this._timer&&(this._cancelFrame(this._timer),this._timer=null)},BMapLib.TrackAnimation.prototype.setDuration=function(t){"number"!=typeof t||t<=0||(this._opts.duration=t,this._buildExpandedPath())},BMapLib.TrackAnimation.prototype.getDuration=function(){return this._opts.duration},BMapLib.TrackAnimation.prototype.setSpeed=function(t){if(!("number"!=typeof t||t<=0)){var e=this._opts.duration,s=e*(1/t);if((1===this._status||3===this._status)&&this._startTime){var o=(3===this._status&&this._pauseAt?this._pauseAt:i())-this._pauseTime,a=(o-this._startTime)/e;a<0&&(a=0),a>1&&(a=1),this._startTime=o-a*s}this._speedFactor=t,this.setDuration(s)}},BMapLib.TrackAnimation.prototype.getSpeed=function(){return this._speedFactor||1},BMapLib.TrackAnimation.prototype.setPolyline=function(t){t&&(this._polyline=t,this._totalPath=t.getPath()||[],this._buildExpandedPath())},BMapLib.TrackAnimation.prototype.getPolyline=function(){return this._polyline},BMapLib.TrackAnimation.prototype.getLastPoint=function(){return this._last2Points.slice(0)}}();