【问题标题】:Openlayers 3: animate point featureOpenlayers 3:动画点特征
【发布时间】:2016-08-18 10:15:52
【问题描述】:

我在此处设置了当前设置:fully functional fiddle example,并且我需要对点特征进行动画处理...使其像 Google 等上的 GPS 位置一样脉冲。我找到了这篇文章:http://openlayers.org/en/master/examples/feature-animation.html,但发现它真的很混乱并且不知道如何将它应用到我的代码中。

这是小提琴中创建点特征并将其应用于地图的部分......

function locate_me() {
    var locationPoint = new ol.Feature({
        geometry: new ol.geom.Point([0.3901863098144531, 52.803332200169166])
    });
    locationPoint.getGeometry().transform('EPSG:4326', 'EPSG:3857');

    // A vector layer to hold the location point
    var locationLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
            features: [
                locationPoint
            ]
        })
    });
    map.addLayer(locationLayer);
}

【问题讨论】:

  • 更好地展示你想要实现的动画。
  • @JonatasWalker OL 示例中的那个很好,但是用蓝色圆圈代替红色...我只是不知道从哪里开始!

标签: javascript openlayers-3


【解决方案1】:

隔离并评论为特征创建闪光效果的函数:

/*
 * @param {ol.Feature}
 * @param {Number} Duration in milliseconds.
 */
function flash(feature, duration) {
  var start = +new Date();
  var listenerKey; // to remove the listener after the duration

  function animate(event) {
    // canvas context where the effect will be drawn
    var vectorContext = event.vectorContext;
    var frameState = event.frameState;

    // create a clone of the original ol.Feature
    // on each browser frame a new style will be applied
    var flashGeom = feature.getGeometry().clone();
    var elapsed = frameState.time - start;
    var elapsedRatio = elapsed / duration;
    // radius will be 5 at start and 30 at end.
    var radius = ol.easing.easeOut(elapsedRatio) * 25 + 5;
    var opacity = ol.easing.easeOut(1 - elapsedRatio);

    // you can customize here the style
    // like color, width
    var style = new ol.style.Style({
      image: new ol.style.Circle({
        radius: radius,
        snapToPixel: false,
        stroke: new ol.style.Stroke({
          color: [51, 51, 51, opacity],
          width: 0.25 + opacity
        })
      })
    });

    vectorContext.setStyle(style);
    vectorContext.drawGeometry(flashGeom);
    if (elapsed > duration) { // stop the effect
      ol.Observable.unByKey(listenerKey);
      return;
    }
    // tell OL3 to continue postcompose animation
    map.render();
  }

  listenerKey = map.on('postcompose', animate);
}

用法:

var marker = new ol.Feature(new ol.geom.Point([0, 0]));
var vectorLayer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [marker]
  })
});
map.addLayer(vectorLayer);

flash(marker, 2000);

【讨论】:

  • 非常感谢...我已将它应用到这里的小提琴...jsfiddle.net/littleninja/yd0tdxje/9 但不断收到错误vectorContext.setStyle is not a function。有任何想法吗?非常感谢您的帮助!
  • 您使用的是旧的OL版本。使用最新的。
  • 太棒了!!!谢谢!我可以很高兴地从您的代码中调整它的外观,但是我怎样才能让动画一遍又一遍地重复呢?非常感谢!!
  • 使用setInterval
  • 告诉地图渲染每个函数调用 - 所以function make_point_flash() { map.render(); flash(locationPoint, 2000); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多