OpenLayers OpenLayers

Ticket #110: patch-110-r5555-A0.diff

File patch-110-r5555-A0.diff, 9.3 kB (added by pgiraud, 1 year ago)

path including a new panTo method, tween and ease methods

  • lib/OpenLayers/Map.js

    old new  
    12061206        } 
    12071207 
    12081208   }, 
     1209    
     1210   /**  
     1211     * APIMethod: panTo 
     1212     * Allows user to pan to a new lonlat 
     1213     * If the new lonlat is in the current extent the map will slide smoothly 
     1214     *  
     1215     * Parameters: 
     1216     * lonlat - {<OpenLayers.Lonlat>} 
     1217     */ 
     1218    panTo: function(lonlat) { 
     1219        if (this.getExtent().containsLonLat(lonlat)) { 
     1220            if (!this.panTween) { 
     1221                this.panTween = new OpenLayers.Tween(OpenLayers.Easing.Expo.easeOut); 
     1222            } 
     1223            var center = map.getCenter(); 
     1224            var from = { 
     1225                lon: center.lon, 
     1226                lat: center.lat 
     1227            }; 
     1228            var to = { 
     1229                lon: lonlat.lon, 
     1230                lat: lonlat.lat 
     1231            }; 
     1232            this.panTween.start(from, to, 50, function(lonlat){ 
     1233                var lonlat = new OpenLayers.LonLat(lonlat.lon, lonlat.lat); 
     1234                map.setCenter(lonlat); 
     1235            }); 
     1236        } else { 
     1237            map.setCenter(lonlat); 
     1238        } 
     1239    }, 
    12091240 
    12101241    /** 
    12111242     * APIMethod: setCenter 
  • lib/OpenLayers/Tween.js

    old new  
     1/* Copyright (c) 2006-2007 MetaCarta, Inc., published under the Clear BSD 
     2 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the 
     3 * full text of the license. */ 
     4 
     5/** 
     6 * Namespace: OpenLayers.Tween 
     7 */ 
     8OpenLayers.Tween = OpenLayers.Class({ 
     9     
     10    /** 
     11     * Constant: INTERVAL 
     12     * Interval in milliseconds between 2 steps 
     13     */ 
     14    INTERVAL: 10, 
     15     
     16    /** 
     17     *  
     18     * Params: 
     19     * easing - {<OpenLayers.Easing>(Function)} easing function method to use 
     20     */  
     21    initialize: function(easing) { 
     22        this.easing = easing; 
     23    }, 
     24     
     25    /** 
     26     * APIMethod: start 
     27     * Plays the Tween, and calls the callback method on each step 
     28     *  
     29     * begin - {Object} begin value  
     30     * finish - {Object} finish value (should have the same properties as begin) 
     31     * duration - {int} duration of the tween (number of steps) 
     32     * callback - {Function} callback called on each step 
     33     */ 
     34    start: function(begin, finish, duration, callback) { 
     35        this.begin = begin; 
     36        this.finish = finish; 
     37        this.duration = duration; 
     38        this.time = 0; 
     39        if (this.interval) { 
     40            window.clearInterval(this.interval); 
     41            this.interval = null; 
     42        } 
     43        this.interval = window.setInterval( 
     44            OpenLayers.Function.bind(this.ease, this, callback), this.INTERVAL); 
     45    }, 
     46     
     47    /** 
     48     * Method: ease 
     49     * Calls the appropriate easing method 
     50     *  
     51     * callback - {Function} callback 
     52     */ 
     53    ease: function(callback) { 
     54        if (this.time >= this.duration) { 
     55            window.clearInterval(this.interval); 
     56            this.interval = null; 
     57        } 
     58         
     59        var value = {}; 
     60        for (var i in this.begin) { 
     61            var b = this.begin[i]; 
     62            var f = this.finish[i]; 
     63            if (b == null || f == null || isNaN(b) || isNaN(f)) { 
     64                OpenLayers.Console.error('invalid value for Tween'); 
     65            } 
     66             
     67            var c = f - b; 
     68            value[i] = this.easing.apply(this, [this.time, b, c, this.duration]); 
     69        } 
     70        this.time++; 
     71        callback.call(this, value); 
     72    }, 
     73     
     74    /** 
     75     * Create empty functions for all easing methods. 
     76     */ 
     77    CLASS_NAME: "OpenLayers.Tween" 
     78}); 
     79 
     80/** 
     81 * Namespace: OpenLayers.Easing 
     82 *  
     83 * Credits: 
     84 *      Easing Equations by Robert Penner, <http://www.robertpenner.com/easing/> 
     85 */ 
     86OpenLayers.Easing = { 
     87    /** 
     88     * Create empty functions for all easing methods. 
     89     */ 
     90    CLASS_NAME: "OpenLayers.Easing" 
     91} 
     92 
     93/** 
     94 * Namespace: OpenLayers.Easing.Linear 
     95 */ 
     96OpenLayers.Easing.Linear = { 
     97     
     98    /** 
     99     * APIFunction: easeIn 
     100     *  
     101     * Parameters: 
     102     * t - {Float} time 
     103     * b - {Float} beginning position 
     104     * c - {Float} total change 
     105     * d - {Float} duration of the transition 
     106     */ 
     107    easeIn: function(t, b, c, d) { 
     108        return c*t/d + b; 
     109    }, 
     110     
     111    /** 
     112     * APIFunction: easeOut 
     113     *  
     114     * Parameters: 
     115     * t - {Float} time 
     116     * b - {Float} beginning position 
     117     * c - {Float} total change 
     118     * d - {Float} duration of the transition 
     119     */ 
     120    easeOut: function(t, b, c, d) { 
     121        return c*t/d + b; 
     122    }, 
     123     
     124    /** 
     125     * APIFunction: easeInOut 
     126     *  
     127     * Parameters: 
     128     * t - {Float} time 
     129     * b - {Float} beginning position 
     130     * c - {Float} total change 
     131     * d - {Float} duration of the transition 
     132     */ 
     133    easeInOut: function(t, b, c, d) { 
     134        return c*t/d + b; 
     135    }, 
     136 
     137    CLASS_NAME: "OpenLayers.Easing.Linear" 
     138}; 
     139 
     140/** 
     141 * Namespace: OpenLayers.Easing.Expo 
     142 */ 
     143OpenLayers.Easing.Expo = { 
     144     
     145    /** 
     146     * APIFunction: easeIn 
     147     *  
     148     * Parameters: 
     149     * t - {Float} time 
     150     * b - {Float} beginning position 
     151     * c - {Float} total change 
     152     * d - {Float} duration of the transition 
     153     */ 
     154    easeIn: function(t, b, c, d) { 
     155        return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; 
     156    }, 
     157     
     158    /** 
     159     * APIFunction: easeOut 
     160     *  
     161     * Parameters: 
     162     * t - {Float} time 
     163     * b - {Float} beginning position 
     164     * c - {Float} total change 
     165     * d - {Float} duration of the transition 
     166     */ 
     167    easeOut: function(t, b, c, d) { 
     168        return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; 
     169    }, 
     170     
     171    /** 
     172     * APIFunction: easeInOut 
     173     *  
     174     * Parameters: 
     175     * t - {Float} time 
     176     * b - {Float} beginning position 
     177     * c - {Float} total change 
     178     * d - {Float} duration of the transition 
     179     */ 
     180    easeInOut: function(t, b, c, d) { 
     181        if (t==0) return b; 
     182        if (t==d) return b+c; 
     183        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b; 
     184        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; 
     185    }, 
     186 
     187    CLASS_NAME: "OpenLayers.Easing.Expo" 
     188}; 
  • lib/OpenLayers.js

    old new  
    7474            "OpenLayers/BaseTypes/Pixel.js", 
    7575            "OpenLayers/BaseTypes/Size.js", 
    7676            "OpenLayers/Console.js", 
     77            "OpenLayers/Tween.js", 
    7778            "Rico/Corner.js", 
    7879            "Rico/Color.js", 
    7980            "OpenLayers/Ajax.js", 
  • examples/tween.html

    old new  
     1<html xmlns="http://www.w3.org/1999/xhtml"> 
     2  <head> 
     3    <title>Markers Layer Example</title> 
     4    <style type="text/css"> 
     5        #map { 
     6            width: 512px; 
     7            height: 256px; 
     8            border: 1px solid black; 
     9        } 
     10    </style> 
     11    <script src="../lib/OpenLayers.js"></script> 
     12    <script type="text/javascript"> 
     13        var map, layer; 
     14 
     15        function init(){ 
     16            OpenLayers.ProxyHost="/proxy/?url="; 
     17            map = new OpenLayers.Map('map'); 
     18            layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",  
     19                "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} ); 
     20                 
     21            map.addLayer(layer); 
     22            map.setCenter(new OpenLayers.LonLat(0, 0), 0); 
     23 
     24            map.zoomToMaxExtent(); 
     25             
     26            var tween = new OpenLayers.Tween(OpenLayers.Easing.Expo.easeOut); 
     27             
     28            map.events.register("click", map, function(e) { 
     29                 
     30                function moveMap(lonlat) { 
     31                    var lonlat = new OpenLayers.LonLat(lonlat.lon, lonlat.lat); 
     32                    map.setCenter(lonlat, map.getZoom()); 
     33                }; 
     34                 
     35                var click = map.getLonLatFromViewPortPx(e.xy); 
     36                var to = { 
     37                    lon: click.lon, 
     38                    lat: click.lat 
     39                }; 
     40                 
     41                var center = map.getCenter(); 
     42                var from = { 
     43                    lon: center.lon, 
     44                    lat: center.lat 
     45                }; 
     46                tween.start(from, to, 50, moveMap); 
     47            }); 
     48        } 
     49         
     50        function setCenter() { 
     51            var lon = Math.random() * 360 - 180; 
     52            var lat = Math.random() * 180 - 90; 
     53            var lonlat = new OpenLayers.LonLat(lon, lat); 
     54            map.panTo(lonlat); 
     55        } 
     56    </script> 
     57  </head> 
     58  <body onload="init()"> 
     59    <div id="title">Tween Example</div> 
     60    <div id="tags"></div> 
     61    <div id="shortdesc">Show transition effects</div> 
     62    <div id="map"></div> 
     63    <div id="docs"> 
     64        This is an example of transition effects. One single click on the map should recenter it with a smooth slide. 
     65    </div> 
     66    <button onclick="setCenter()">recenter to random lonlat</button> 
     67  </body> 
     68</html>