javascript - 开放层 3 : animate point feature

标签 javascript openlayers-3

我在这里设置了电流:fully functional fiddle example我需要为点特征制作动画……让它像谷歌上的 GPS 位置一样脉动。我找到了这篇文章:http://openlayers.org/en/master/examples/feature-animation.html但发现它真的很困惑,不知道如何将它应用到我的代码中。

这是创建点特征并将其应用于 map 的 fiddle 部分...

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);
}

最佳答案

隔离和注释创建闪光效果的函数:

/*
 * @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);

关于javascript - 开放层 3 : animate point feature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39015587/

相关文章:

javascript - 在 JavaScript 中保留大小写的动态正则表达式

javascript - 撤消 OL3 中的最后一点会删除最后一段

JavaScript 每次点击时增加++ 不透明度

javascript - 当条件之一为 FALSE 时执行 IF

javascript - 外部 javascript 文件在 IE 中不起作用

javascript - openlayers 3 簇中的字体大小

javascript - OpenLayers 3矢量图层上所有特征的范围?

javascript - OpenLayers3 在圆上添加文字

javascript - 无法从 map 中删除所有图层

javascript - 将动态数据传递给 angularjs 指令的正确方法