gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:50:20 26 4
gpt4 key购买 nike

我正在使用 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 中使用不完整的样式,因为它会停止渲染并锁定 map 。

关于javascript - 动态更改输入 svg 图标的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58206314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com