动态更改输入 svg 图标的位置

Dynamically change location of a input svg icon

我正在使用 openlayers 和 javascript。
我正在尝试根据 openlayers 示例动态更改 svg 文件的位置
https://openlayers.org/en/latest/examples/dynamic-data.html
但我希望 sgv 文件成为动画的头部而不是绘制的黑色圆圈。

import 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import Draw from 'ol/interaction/Draw';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import {OSM, Vector as VectorSource} from 'ol/source';

import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import {fromLonLat} from 'ol/proj';
import TileJSON from 'ol/source/TileJSON';
import {Icon, Style} from 'ol/style';
import {getVectorContext} from 'ol/render';
import {Circle as CircleStyle, Fill, Stroke} from 'ol/style';
import {MultiPoint} from 'ol/geom';

var home = new Point(fromLonLat([-46.68, -23.59]));

var Plane = new Feature({
    geometry: home
});

Plane.setStyle(new Style({
  image: new Icon({
    color: '#F5AC7C',
    crossOrigin: 'anonymous',
    rotation: 180 / 180 * 3.14,
    src: 'data/plane.svg'
  })
}));

var imageStyle = new Style({
  image: new CircleStyle({
    radius: 5,
    fill: new Fill({color: 'yellow'}),
    stroke: new Stroke({color: 'red', width: 1})
  })
});

var headInnerImageStyle = new Style({
  image: new CircleStyle({
    radius: 2,
    fill: new Fill({color: 'blue'})
  })
});

var headOuterImageStyle = new Style({
  image: new CircleStyle({
   radius: 5,
    fill: new Fill({color: 'black'})
  })
});


var rasterLayer = new TileLayer({
  source: new OSM()
});

var source = new VectorSource({
    wrapX: false,
    features: [Rectangle, Alvo_Secundario]
});

var vectorLayer = new VectorLayer({
  source: source
});

var map = new Map({
  layers: [rasterLayer, vectorLayer],
  target: 'map',
  view: new View({
        center:fromLonLat([-46.68, -23.59]),
    zoom: 4
  })
});

var n = 100;
var omegaTheta = 20000; // Rotation period in ms
var R = 3e6;
var r = 1e6;
var p = 1e6;
rasterLayer.on('postrender', function(event) {
  var vectorContext = getVectorContext(event);
  var frameState = event.frameState;
  var theta = 2 * Math.PI * frameState.time / omegaTheta;
  var coordinates = [];
  var i;
  for (i = 0; i < n; ++i) {
    var t = theta + 2 * Math.PI * i / n;
    var x = (R + r) * Math.cos(t) + p * Math.cos((R + r) * t / r);
    var y = (R + r) * Math.sin(t) + p * Math.sin((R + r) * t / r);
    coordinates.push([x, y]);
  }
  vectorContext.setStyle(imageStyle);
  vectorContext.drawGeometry(new MultiPoint(coordinates));

  var headPoint = new Point(coordinates[coordinates.length - 1]);

  vectorContext.setStyle(headOuterImageStyle);
  vectorContext.drawGeometry(headPoint);

  vectorContext.setStyle(headInnerImageStyle);
  vectorContext.drawGeometry(headPoint);

  map.render();
});
map.render();

画了100个圆做动画,头点是黑圆,我希望是最初画在"home"点

的sgv文件

如果可以,如何将100个圆点变成100个sgv文件?

图标样式似乎仅在图标已加载时才在 vectorContext 中起作用。如果尚未使用它,则需要在构建样式时强制加载它。可以确保加载它的最可靠方法是将 src 加载到 img 并在图标构造函数中使用 img https://codesandbox.io/s/dynamic-data-3x8ux 不要尝试在 vectorContext 中使用不完整的样式,因为它会停止渲染并锁定地图.

为了解决这个问题,我编写了一个接收新点并调用 source.addFeature(newPoint);

的函数