Viglino / ol-ext

Cool extensions for Openlayers (ol) - animated clusters, CSS popup, Font Awesome symbol renderer, charts for statistical map (pie/bar), layer switcher, wikipedia layer, animations, canvas filters.
https://viglino.github.io/ol-ext/
Other
1.24k stars 462 forks source link

map.animateFeature does not work if last added vector layer is empty #464

Closed MauroIT closed 4 years ago

MauroIT commented 4 years ago

When calling the map.animateFeature method while the last added vector layer does not contain any features, the layer's postrender event will never be fired and consequently no animation will be shown on the map.

To reproduce this beheavior you can simply adjust the map.Pulse example code as follows:

Replace

var layer = new ol.layer.Tile({
    title:'terrain-background',
    source: new ol.source.Stamen({ layer: 'terrain' })
  });

  // The map
  var map = new ol.Map ({
    target: 'map',
    view: new ol.View({
      zoom: 5,
      center: [166326, 5992663]
    }),
    layers: [layer]
  });

with

var layer = new ol.layer.Tile({
    title:'terrain-background',
    source: new ol.source.Stamen({ layer: 'terrain' })
  });

var emptyVectorLayer = new ol.layer.Vector({
   source: new ol.source.Vector()
});

  // The map
  var map = new ol.Map ({
    target: 'map',
    view: new ol.View({
      zoom: 5,
      center: [166326, 5992663]
    }),
    layers: [layer, emptyVectorLayer]
  });
Viglino commented 4 years ago

You have to add the feature to the layer otherwise Openlayers optimizes rendering and doesn't draw the layer. Remove it on animation end (use animationend event). NB: using ol6 featureAnimation on maps should be deprecated as there is no more global canvas to draw the animation (ol-ext tries to animate on the top layer but optimisation make it unsafe). I recommend using featureAnimation on layers (create an Overlay on top top play animation).

var overlay = new ol.layer.Vector({
   source: new ol.source.Vector()
});
overlay.setMap(map);
var animation = new ol.featureAnimation.Zoom({
  fade: ol.easing.easeOut,
  duration: 3000,
  easing: ol.easing[$("#easing").val()]
});
animation.on('animationstart', function(e) {
  overlay.getSource().addFeature(e.feature);
});
animation.on('animationend', function(e) {
  overlay.getSource().removeFeature(e.feature);
});
overlay.animateFeature (f, animation);

I'll change the example to reflect this.

MauroIT commented 4 years ago

Hi Viglino, thanks for your reply! I have implemented according your suggestion. The animation works now.

Viglino commented 4 years ago

Hello, I've changed the code for map animation. Now it creates its own animation overlay and animate feature on it. Just use map.animateFeature.