torque/dist/torque.js

3 lines
66 KiB
JavaScript
Raw Normal View History

2014-08-15 00:03:12 +08:00
(function(e){function r(e,t){if(!t.steps)throw new Error("steps option missing");this.options=t,this.running=!1,this._tick=this._tick.bind(this),this._t0=+(new Date),this.callback=e,this._time=0,_.defaults(this.options,{animationDelay:0,maxDelta:.2,loop:!0}),this.rescale()}e.torque=e.torque||{};var t=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame||function(e){setTimeout(e,16)},n=window.requestAnimationFrame||window.mozCancelAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame||function(e){};r.prototype={start:function(){this.running=!0,t(this._tick)},isRunning:function(){return this.running},stop:function(){this.pause(),this.time(0)},time:function(e){if(!arguments.length)return this._time;this._time=e;var t=this.range(this.domain(this._time));this.callback(t)},toggle:function(){this.running?this.pause():this.start()},rescale:function(){return this.domainInv=torque.math.linear(this.options.animationDelay,this.options.animationDelay+this.options.animationDuration),this.domain=this.domainInv.invert(),this.range=torque.math.linear(0,this.options.steps),this.rangeInv=this.range.invert(),this.time(this._time),this},duration:function(e){return arguments.length?(this.options.animationDuration=e,this.time()>e&&this.time(0),this.rescale(),this):this.options.animationDuration},steps:function(e){return this.options.steps=e,this.rescale()},step:function(e){if(arguments.length===0)return this.range(this.domain(this._time));this._time=this.domainInv(this.rangeInv(e))},pause:function(){this.running=!1,n(this._tick)},_tick:function(){var e=+(new Date),n=(e-this._t0)*.001;n=Math.min(this.options.maxDelta,n),this._t0=e,this._time+=n,this.time(this._time),this.step()>=this.options.steps&&(this._time=0),this.running&&t(this._tick)}},e.torque.Animator=r})(typeof exports=="undefined"?this:exports),function(e){e.torque=e.torque||{};var t={version:"1.0.0",style:{"comp-op":{css:"comp-op","default-value":"src-over","default-meaning":"add the current layer on top of other layers",doc:"Composite operation. This defines how this layer should behave relative to layers atop or below it.",type:["src","src-over","dst-over","src-in","dst-in","src-out","dst-out","src-atop","dst-atop","xor","darken","lighten"]}},layer:{"buffer-size":{"default-value":"0",type:"float","default-meaning":"No buffer will be used",doc:"Extra tolerance around the Layer extent (in pixels) used to when querying and (potentially) clipping the layer data during rendering"},"-torque-frame-count":{css:"-torque-frame-count","default-value":"128",type:"number","default-meaning":"the data is broken into 128 time frames",doc:"Number of animation steps/frames used in the animation. If the data contains a fewere number of total frames, the lesser value will be used."},"-torque-resolution":{css:"-torque-resolution","default-value":"2",type:"number","default-meaning":"",doc:"Spatial resolution in pixels. A resolution of 1 means no spatial aggregation of the data. Any other resolution of N results in spatial aggregation into cells of NxN pixels. The value N must be power of 2"},"-torque-animation-duration":{css:"-torque-animation-duration","default-value":"30",type:"number","default-meaning":"the animation lasts 30 seconds",doc:"Animation duration in seconds"},"-torque-aggregation-function":{css:"-torque-aggregation-function","default-value":"count(cartodb_id)",type:"string","default-meaning":"the value for each cell is the count of points in that cell",doc:"A function used to calculate a value from the aggregate data for each cell. See -torque-resolution"},"-torque-time-attribute":{css:"-torque-time-attribute","default-value":"time",type:"string","default-meaning":"the data column in your table that is of a time based type",doc:"The table column that contains the time information used create the animation"},"-torque-data-aggregation":{css:"-torque-data-aggregation","default-value":"linear",type:["cumulative"],"default-meaning":"previous values are discarded",doc:"A linear animation
.cumulative?"cumulative":"linear"},t="Map{";for(var n in e)t+=n+":"+e[n]+";";return t+"}"},_fetchMap:function(e){var n=this,r={},i=this._tilerHost()+"/api/v1/map",s=this.options.named_map;s?i=this._tilerHost()+"/api/v1/map/named/"+s.name+"/jsonp":r={version:"1.0.1",stat_tag:this.options.stat_tag||"torque",layers:[{type:"torque",options:{cartocss_version:"1.0.0",cartocss:this._generateCartoCSS(),sql:this.getSQL()}}]};var o=this._extraParams();o&&(o=o.replace("api_key=","map_key=")),i=i+"?config="+encodeURIComponent(JSON.stringify(r))+"&callback=?"+(o?"&"+o:"");var u=Profiler.metric("torque.provider.windshaft.layergroup.time").start();t.net.jsonp(i,function(e){u.end();if(e){var t=Object.keys(e.metadata.torque)[0],r=e.metadata.torque[t];for(var i in r)n.options[i]=r[i];if(e.cdn_url){var s=n.options.cdn_url=n.options.cdn_url||{};s.http=e.cdn_url.http||s.http,s.https=e.cdn_url.https||s.https}n.templateUrl=n.url()+"/api/v1/map/"+e.layergroupid+"/"+t+"/{z}/{x}/{y}.json.torque",n._setReady(!0)}else Profiler.metric("torque.provider.windshaft.layergroup.error").inc()},{callbackName:n.options.instanciateCallback})}},t.providers.windshaft=u}(typeof exports=="undefined"?this:exports),function(e){function r(e,t,n){function o(){r.removeChild(i),clearTimeout(u),delete window[s]}n=n||{},n.timeout=n.timeout===undefined?1e4:n.timeout;var r=document.getElementsByTagName("head")[0],i=document.createElement("script"),s=n.callbackName||"torque_"+Date.now();window[s]=function(){o(),t.apply(window,arguments)};var u=setTimeout(function(){o(),t.call(window,null)},n.timeout);e=e.replace("callback=?","callback="+s),i.type="text/javascript",i.src=e,i.async=!0,setTimeout(function(){r.appendChild(i)},0)}function i(e,t,r){function o(){var e=s.status,n,i=r.responseType==="arraybuffer"?s.response:s.responseText;!e&&i||e>=200&&e<300||e===304?t(s):t(null)}r=r||{method:"GET",data:null,responseType:"text"},n={url:e,callback:t};var i=XMLHttpRequest;window.XDomainRequest&&!("withCredentials"in i)&&/^(http(s)?:)?\/\//.test(e)&&(i=XDomainRequest);var s=new i;return s.open(r.method,e,!0),"onload"in s?s.onload=s.onerror=o:s.onreadystatechange=function(){s.readyState>3&&o()},s.onprogress=function(){},s.responseType=r.responseType,r.data&&(s.setRequestHeader("Content-type","application/json"),s.setRequestHeader("Accept","*")),s.send(r.data),s}function s(e,t,n){return i(e,n,{data:t,method:"POST"})}var t=e.torque=e.torque||{};t.net=t.net||{};var n=null;t.net={get:i,post:s,jsonp:r,lastCall:function(){return n}}}(typeof exports=="undefined"?this:exports),function(e){function n(e,n){e.fillStyle=n.fillStyle,e.strokStyle=n.strokStyle;var r=n["point-radius"];e.beginPath(),e.arc(0,0,r,0,t,!0,!0),e.closePath(),n.fillStyle&&(n.fillOpacity&&(e.globalAlpha=n.fillOpacity),e.fill()),e.globalAlpha=1,n.strokeStyle&&n.lineWidth&&(n.strokeOpacity&&(e.globalAlpha=n.strokeOpacity),n.lineWidth&&(e.lineWidth=n.lineWidth),e.strokeStyle=n.strokeStyle,e.globalAlpha>0&&e.stroke())}function r(e,t){e.fillStyle=t.fillStyle,e.strokStyle=t.strokStyle;var n=t["point-radius"],r=n*2;t.fillStyle&&t.fillOpacity&&(e.globalAlpha=t.fillOpacity),e.fillRect(-n,-n,r,r),e.globalAlpha=1,t.strokeStyle&&t.lineWidth&&(t.strokeOpacity&&(e.globalAlpha=t.strokeOpacity),t.lineWidth&&(e.lineWidth=t.lineWidth),e.strokeStyle=t.strokeStyle,e.globalAlpha>0&&e.strokeRect(-n,-n,r,r))}function i(e,t){var n=t["point-file"]||t["marker-file"],r=n.height/n.width,i=t["point-radius"]||n.width,s=t["point-radius"]||t["marker-height"]||i*r;e.drawImage(n,0,0,i,s)}e.torque=e.torque||{};var t=Math.PI*2;e.torque.cartocss=e.torque.cartocss||{},e.torque.cartocss={renderPoint:n,renderSprite:i,renderRectangle:r}}(typeof exports=="undefined"?this:exports),function(e){function r(e,t){if(!e)throw new Error("canvas can't be undefined");this.options=t,this._canvas=e,this._ctx=e.getContext("2d"),this._sprites=[],this._shader=null,this.setCartoCSS(this.options.cartocss||n)}e.torque=e.torque||{},e.torque.renderer=e.torque.renderer||{};var t=Math.PI*2,n=["#layer {"," marker-fill: #662506;"," marker-width: 4;"," [value > 1] { marker-fill: #FEE391;
.provider.setSQL)throw new Error("this provider does not support SQL");return this.provider.setSQL(e),this._reloadTiles(),this},setBlendMode:function(e){this.renderer.setBlendMode(e),this.redraw()},setSteps:function(e){this.provider.setSteps(e),this._reloadTiles()},setColumn:function(e,t){this.provider.setColumn(e,t),this._reloadTiles()},getTimeBounds:function(){return this.provider&&this.provider.getKeySpan()},clear:function(){var e=this.getCanvas();e.width=e.width},render:function(){if(this.hidden)return;var e,t,n,r=this.getCanvas();r.width=r.width;var i=r.getContext("2d");for(e in this._tiles)t=this._tiles[e],t&&(n=this.getTilePos(t.coord),i.setTransform(1,0,0,1,n.x,n.y),this.renderer.renderTile(t,this.key,n.x,n.y))},setKey:function(e,t){this.key=e,this.animator.step(e),this.redraw(t&&t.direct),this.fire("change:time",{time:this.getTime(),step:this.key})},setStep:function(e){if(e===undefined||e.length!==undefined)throw new Error("setTime only accept scalars");this.setKey(e)},stepToTime:function(e){var t=this.provider.getKeySpan(),n=t.start+(t.end-t.start)*(e/this.provider.getSteps());return new Date(n)},getStep:function(){return this.key},getTime:function(){return this.stepToTime(this.key)},getTimeSpan:function(){var e=this.provider.getKeySpan()},setCartoCSS:function(e){if(!this.renderer)throw new Error("renderer is not valid");var t=(new carto.RendererJS).render(e);this.renderer.setShader(t);var n=torque.common.TorqueLayer.optionsFromLayer(t.findLayer({name:"Map"}));return this.provider.setCartoCSS&&this.provider.setCartoCSS(e),this.provider.setOptions(n)&&this._reloadTiles(),_.extend(this.options,n),n.animationDuration&&this.animator.duration(n.animationDuration),this.redraw(),this},getActivePointsBBox:function(e){var t=[];for(var n in this._tiles){var r=this._tiles[n];t=t.concat(this.renderer.getActivePointsBBox(r,e))}return t}}));