OpenLayers OpenLayers

Ticket #442: final_animatedZooming_PanZoomBar.patch

File final_animatedZooming_PanZoomBar.patch, 4.9 kB (added by emanuel, 3 years ago)

PanZoomBar supports animated zooming (included unit tests)

  • tests/Control/test_PanZoomBar.html

    old new  
    3030        map.addControl(control2, new OpenLayers.Pixel(100,100)); 
    3131        t.eq( control2.div.style.top, "100px", "2nd control div is located correctly"); 
    3232    } 
     33 
     34    function test_03_Control_PanZoomBar_control_events (t) { 
     35        t.plan( 3 ); 
     36 
     37        var evt = {which: 1}; // control expects left-click 
     38        map = new OpenLayers.Map('map'); 
     39        var layer = new OpenLayers.Layer.WMS("Test Layer",  
     40            "http://octo.metacarta.com/cgi-bin/mapserv?", 
     41            {map: "/mapdata/vmap_wms.map", layers: "basic"}); 
     42        map.addLayer(layer); 
     43        control = new OpenLayers.Control.PanZoomBar(); 
     44        map.addControl(control); 
     45        var centerLL = new OpenLayers.LonLat(0,0);  
     46        map.setCenter(centerLL, 0); 
     47 
     48        evt.xy = new OpenLayers.Pixel(10,115);  
     49        evt.object = control; 
     50        control.divClick(evt); 
     51        t.delay_call( 
     52            1, function() { 
     53                t.eq(map.zoom, 5,  
     54                    "Clicking on the zoombar has the correct effect on zoomlevel"); 
     55 
     56                control.zoomBarDown(evt); 
     57                evt.xy.y -= 30;   
     58                control.zoomBarDrag(evt); 
     59 
     60                control.zoomBarUp(evt); 
     61            }, 
     62            1, function() { 
     63                t.eq(map.zoom, 8,  
     64                    "Dragging the zoomslider works correctly"); 
     65                t.eq(control.slider.style.top, "159px",  
     66                    "Zoomslider top position has been set correctly"); 
     67            } 
     68 
     69        ); 
     70         
     71 
     72 
     73    } 
     74 
    3375  // --> 
    3476  </script> 
    3577</head> 
  • lib/OpenLayers/Control/PanZoomBar.js

    old new  
    152152        var y = evt.xy.y; 
    153153        var top = OpenLayers.Util.pagePosition(evt.object)[1]; 
    154154        var levels = Math.floor((y - top)/this.zoomStopHeight); 
    155         this.map.zoomTo((this.map.getNumZoomLevels() -1) -  levels); 
     155 
     156        if (!this.map.zoomanimationActive) 
     157            this.map.zoomTo((this.map.getNumZoomLevels() -1) -  levels); 
     158 
    156159        OpenLayers.Event.stop(evt); 
    157160    }, 
    158161     
     
    165168        this.map.events.register("mousemove", this, this.passEventToSlider); 
    166169        this.map.events.register("mouseup", this, this.passEventToSlider); 
    167170        this.mouseDragStart = evt.xy.clone(); 
    168         this.zoomStart = evt.xy.clone(); 
     171        this.map.zoomStart = evt.xy.clone(); 
    169172        this.div.style.cursor = "move"; 
     173 
     174        // get and set some settings for zoom animation  
     175        this.map.prepareZoomAnimation(); 
     176         
    170177        OpenLayers.Event.stop(evt); 
    171178    }, 
    172179     
     
    186193                this.slider.style.top = newTop+"px"; 
    187194            } 
    188195            this.mouseDragStart = evt.xy.clone(); 
     196            
     197            // set current slider position 
     198            var sliderPosition = new OpenLayers.Pixel(evt.xy.x, evt.xy.y);  
     199 
     200            // run zoom animation -> scale tile(s) 
     201            this.map.runZoomAnimation(this.zoomStopHeight, sliderPosition); 
     202       
    189203            OpenLayers.Event.stop(evt); 
    190204        } 
    191205    }, 
     
    197211     */ 
    198212    zoomBarUp:function(evt) { 
    199213        if (!OpenLayers.Event.isLeftClick(evt)) return; 
    200         if (this.zoomStart) { 
     214        if (this.map.zoomStart) { 
    201215            this.div.style.cursor="default"; 
    202216            this.map.events.unregister("mouseup", this, this.passEventToSlider); 
    203217            this.map.events.unregister("mousemove", this, this.passEventToSlider); 
    204             var deltaY = this.zoomStart.y - evt.xy.y 
    205             this.map.zoomTo(this.map.zoom + Math.round(deltaY/this.zoomStopHeight)); 
     218            var deltaY = this.map.zoomStart.y - evt.xy.y; 
     219 
     220            // zoom map to new zoomlevel 
     221            var finalZoomlevel = this.map.zoom + Math.round(deltaY/this.zoomStopHeight); 
     222 
     223            // finish zoom animation 
     224            this.map.finishZoomAnimation(finalZoomlevel); 
     225 
    206226            this.moveZoomBar(); 
    207227            this.mouseDragStart = null; 
    208228            OpenLayers.Event.stop(evt); 
     
    211231     
    212232    /*  
    213233    * Change the location of the slider to match the current zoom level. 
     234    * 
     235    * @param {float} zoomlevel 
    214236    */ 
    215     moveZoomBar:function() { 
    216         var newTop =  
    217             ((this.map.getNumZoomLevels()-1) - this.map.getZoom()) *  
     237    moveZoomBar:function(zoomlevel) { 
     238        // check if zoomlevel is not a number 
     239        if (isNaN(zoomlevel)) { 
     240            zoomlevel = this.map.getZoom(); 
     241        } 
     242 
     243        // set new top position 
     244        var newTop = ((this.map.getNumZoomLevels()-1) - zoomlevel) * 
    218245            this.zoomStopHeight + this.startTop + 1; 
    219246        this.slider.style.top = newTop + "px"; 
    220247    },