gpt4 book ai didi

javascript - 如何在 openlayers3 中更改图像颜色?

转载 作者:行者123 更新时间:2023-11-28 03:37:04 24 4
gpt4 key购买 nike

我是 openlayers3 的新手。我将点显示为图像,但我想动态更改图像的颜色。在 openlayers3 中是否有可能或在 openlayers3 中有任何其他功能,如 Canvas ?

var iconStyle = new ol.style.Style({ 
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'data/icon.png'
}))
});

最佳答案

服用 Icon Colors作为起点,他们使用带有 ol.Color 选项的透明 PNG 来具有多个颜色点,我尝试像这样更改颜色但没有成功...

var london = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-0.12755, 51.507222]))
});


london.setStyle(new ol.style.Style({
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
color: '#ff0000',
crossOrigin: 'anonymous',
src: 'https://openlayers.org/en/v4.1.1/examples/data/dot.png'
}))
}));


var vectorSource = new ol.source.Vector({
features: [london]
});

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

var rasterLayer = new ol.layer.Tile({
source: new ol.source.TileJSON({
url: 'https://api.tiles.mapbox.com/v3/mapbox.geography-class.json?secure',
crossOrigin: ''
})
});

var map = new ol.Map({
layers: [rasterLayer, vectorLayer],
target: document.getElementById('map'),
view: new ol.View({
center: ol.proj.fromLonLat([2.896372, 44.60240]),
zoom: 3
})
});

map.on('singleclick', function (event) { // Use 'pointermove' to capture mouse movement over the feature
map.forEachFeatureAtPixel(event.pixel, function (feature, layer) {
/* Get the current image instance and its color */
var image = feature.getStyle().getImage();
console.log(image.getColor());

/* Color gets stored in image.i, but there's no setter */
console.log(image.i);

/* Trying to force the color change: */
image.i = [0, 255, 0, 1];

/* Dispatch the changed() event on layer, to force styles reload */
layer.changed();

console.log(image.getColor()); // Logs new color, but it doesn't work...

});
});
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.1.1/build/ol.js"></script>
<div id="map" class="map"></div>

看看如何像这样检索图标的颜色:

var image = feature.getStyle().getImage(),
color = image.getColor(); // returns [R, G, B, alpha]

但没有 setColor() 方法,因此无法在运行时更改颜色...


编辑:再次查看您的问题和代码,我注意到您从未为图标设置颜色,所以您可能真的不想更改 响应事件的颜色,但只是设置一种颜色。如果这就是您想要的,那很简单:

var iconStyle = new ol.style.Style({ 
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'data/icon.png',
color: '#ff0000' // allows HEX as String or RGB(a) as Array
}))
});

参见:http://openlayers.org/en/latest/apidoc/ol.style.Icon.html

关于javascript - 如何在 openlayers3 中更改图像颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44415126/

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