torque/dist/torque.js

2 lines
41 KiB
JavaScript
Raw Normal View History

2013-09-30 19:31:27 +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.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()}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){cancelTimeout(e)};r.prototype={start:function(){this.running=!0,t(this._tick)},stop:function(){this.pause(),this._time=0;var e=this.range(this.domain(this._time));this.callback(e)},toggle:function(){this.running?this.pause():this.start()},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;var r=this.range(this.domain(this._time));this.callback(r),r>=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"]}},symbolizers:{"*":{"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"]},opacity:{css:"opacity",type:"float",doc:"An alpha value for the style (which means an alpha applied to all features in separate buffer and then composited back to main buffer)","default-value":1,"default-meaning":"no separate buffer will be used and no alpha will be applied to the style after rendering"}},trail:{steps:{css:"trail-steps",type:"float","default-value":1,"default-meaning":"no trail steps",doc:"How many steps of trails are going to be rendered"}},polygon:{fill:{css:"polygon-fill",type:"color","default-value":"rgba(128,128,128,1)","default-meaning":"gray and fully opaque (alpha = 1), same as rgb(128,128,128)",doc:"Fill color to assign to a polygon"},"fill-opacity":{css:"polygon-opacity",type:"float",doc:"The opacity of the polygon","default-value":1,"default-meaning":"opaque"}},line:{stroke:{css:"line-color","default-value":"rgba(0,0,0,1)",type:"color","default-meaning":"black and fully opaque (alpha = 1), same as rgb(0,0,0)",doc:"The color of a drawn line"},"stroke-width":{css:"line-width","default-value":1,type:"float",doc:"The width of a line in pixels"},"stroke-opacity":{css:"line-opacity","default-value":1,type:"float","default-meaning":"opaque",doc:"The opacity of a line"},"stroke-linejoin":{css:"line-join","default-value":"miter",type:["miter","round","bevel"],doc:"The behavior of lines when joining"},"stroke-linecap":{css:"line-cap","default-value":"butt",type:["butt","round","square"],doc:"The display of line endings"}},markers:{file:{css:"marker-file",doc:"An SVG file that this marker shows at each placement. If no file is given, the marker will show an ellipse.","default-value":"","default-meaning":"An ellipse or circle, if width equals height",type:"uri"},opacity:{css:"marker-opacity",doc:"The overall opacity of
(e)}if(typeof google=="undefined"||typeof google.maps=="undefined")return;t.prototype=_.extend({},CanvasLayer.prototype,torque.GMapsTileLoader.prototype,torque.Event,{providers:{sql_api:torque.providers.json,url_template:torque.providers.jsonarray},renderers:{point:torque.renderer.Point,pixel:torque.renderer.Rectangle},initialize:function(){var e=this;this.onTileAdded=this.onTileAdded.bind(this),this.options.ready=function(){e.fire("change:bounds",{bounds:e.provider.getBounds()})},this.provider=new this.providers[this.options.provider](this.options),this.renderer=new this.renderers[this.options.renderer](this.getCanvas(),this.options),this._initTileLoader(this.options.map,this.getProjection()),this.cartocss&&this.renderer.setCartoCSS(this.cartocss)},getCanvas:function(){return this.canvas},onTileAdded:function(e){var t=this;this.provider.getTileData(e,e.zoom,function(n){t._tileLoaded(e,n),t.redraw()})},render:function(){var e,t,n,r=this.canvas;r.width=r.width;var i=r.getContext("2d");for(e in this._tiles)t=this._tiles[e],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){this.key=e,this.animator.step(e),this.redraw(),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){if(!this.provider)return 0;var t=this.provider.getKeySpan(),n=t.start+(t.end-t.start)*(e/this.options.steps);return new Date(n*1e3)},getTime:function(){return this.stepToTime(this.key)},setCartoCSS:function(e){return this.renderer?(this.renderer.setCartoCSS(e),this.redraw(),this):(this.cartocss=e,this)},redraw:function(){this.scheduleUpdate()}}),n.prototype=_.extend({},CanvasTileLayer.prototype,{providers:{sql_api:torque.providers.json,url_template:torque.providers.JsonArray},renderers:{point:torque.renderer.Point,pixel:torque.renderer.Rectangle},initialize:function(e){var t=this;this.key=0,this.options.renderer=this.options.renderer||"pixel",this.options.provider=this.options.provider||"sql_api",this.provider=new this.providers[this.options.provider](e),this.renderer=new this.renderers[this.options.renderer](null,e)},_tileLoaded:function(e,t){e.data=t,this.drawTile(e)},_loadTile:function(e,t,n){var r=this,i=1<<n,s={x:(t.x%i+i)%i,y:t.y};this.provider.getTileData(s,n,function(t){r._tileLoaded(e,t)})},drawTile:function(e){var t=e.canvas;if(!e.data)return;t.width=t.width,this.renderer.setCanvas(t);var n=this.renderer.accumulate(e.data,this.key);this.renderer.renderTileAccum(n,0,0)},setKey:function(e){this.key=e,this.redraw()},setCartoCSS:function(e){if(!this.renderer)throw new Error("renderer is not valid");return this.renderer.setCartoCSS(e)}}),e.torque.GMapsTiledTorqueLayer=n,e.torque.GMapsTorqueLayer=t}(typeof exports=="undefined"?this:exports),L.Mixin.TileLoader={_initTileLoader:function(){this._tiles={},this._tilesToLoad=0,this._map.on({moveend:this._updateTiles},this),this._updateTiles()},_removeTileLoader:function(){map.off({moveend:this._updateTiles},this),this._removeTiles()},_updateTiles:function(){if(!this._map)return;var e=this._map.getPixelBounds(),t=this._map.getZoom(),n=this.options.tileSize;if(t>this.options.maxZoom||t<this.options.minZoom)return;var r=new L.Point(Math.floor(e.min.x/n),Math.floor(e.min.y/n)),i=new L.Point(Math.floor(e.max.x/n),Math.floor(e.max.y/n)),s=new L.Bounds(r,i);this._addTilesFromCenterOut(s),this._removeOtherTiles(s)},_removeTiles:function(e){for(var t in this._tiles)this._removeTile(t)},_removeOtherTiles:function(e){var t,n,r,i,s,o=this._map.getZoom();for(s in this._tiles)this._tiles.hasOwnProperty(s)&&(t=s.split(":"),n=parseInt(t[0],10),r=parseInt(t[1],10),i=parseInt(t[2],10),(o!==i||n<e.min.x||n>e.max.x||r<e.min.y||r>e.max.y)&&this._removeTile(s))},_removeTile:function(e){this.fire("tileRemoved",this._tiles[e]),delete this._tiles[e]},_tileShouldBeLoaded:function(e){return!(e.x+":"+e.y+":"+e.zoom in this._tiles)},_tileLoaded:function(e,t){this._tilesToLoad--,this._tiles[e.x+":"+e.y+":"+e.zoom]