Improve feature-animation example
Keep features within tile bounds Skip drawing fully transparent style Don't clone the geometry each frame
This commit is contained in:
@@ -16,8 +16,15 @@ const tileLayer = new TileLayer({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const source = new VectorSource({
|
||||||
|
wrapX: false,
|
||||||
|
});
|
||||||
|
const vector = new VectorLayer({
|
||||||
|
source: source,
|
||||||
|
});
|
||||||
|
|
||||||
const map = new Map({
|
const map = new Map({
|
||||||
layers: [tileLayer],
|
layers: [tileLayer, vector],
|
||||||
target: 'map',
|
target: 'map',
|
||||||
view: new View({
|
view: new View({
|
||||||
center: [0, 0],
|
center: [0, 0],
|
||||||
@@ -26,17 +33,9 @@ const map = new Map({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const source = new VectorSource({
|
|
||||||
wrapX: false,
|
|
||||||
});
|
|
||||||
const vector = new VectorLayer({
|
|
||||||
source: source,
|
|
||||||
});
|
|
||||||
map.addLayer(vector);
|
|
||||||
|
|
||||||
function addRandomFeature() {
|
function addRandomFeature() {
|
||||||
const x = Math.random() * 360 - 180;
|
const x = Math.random() * 360 - 180;
|
||||||
const y = Math.random() * 180 - 90;
|
const y = Math.random() * 170 - 85;
|
||||||
const geom = new Point(fromLonLat([x, y]));
|
const geom = new Point(fromLonLat([x, y]));
|
||||||
const feature = new Feature(geom);
|
const feature = new Feature(geom);
|
||||||
source.addFeature(feature);
|
source.addFeature(feature);
|
||||||
@@ -45,13 +44,17 @@ function addRandomFeature() {
|
|||||||
const duration = 3000;
|
const duration = 3000;
|
||||||
function flash(feature) {
|
function flash(feature) {
|
||||||
const start = new Date().getTime();
|
const start = new Date().getTime();
|
||||||
|
const flashGeom = feature.getGeometry().clone();
|
||||||
const listenerKey = tileLayer.on('postrender', animate);
|
const listenerKey = tileLayer.on('postrender', animate);
|
||||||
|
|
||||||
function animate(event) {
|
function animate(event) {
|
||||||
const vectorContext = getVectorContext(event);
|
|
||||||
const frameState = event.frameState;
|
const frameState = event.frameState;
|
||||||
const flashGeom = feature.getGeometry().clone();
|
|
||||||
const elapsed = frameState.time - start;
|
const elapsed = frameState.time - start;
|
||||||
|
if (elapsed >= duration) {
|
||||||
|
unByKey(listenerKey);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const vectorContext = getVectorContext(event);
|
||||||
const elapsedRatio = elapsed / duration;
|
const elapsedRatio = elapsed / duration;
|
||||||
// radius will be 5 at start and 30 at end.
|
// radius will be 5 at start and 30 at end.
|
||||||
const radius = easeOut(elapsedRatio) * 25 + 5;
|
const radius = easeOut(elapsedRatio) * 25 + 5;
|
||||||
@@ -69,10 +72,6 @@ function flash(feature) {
|
|||||||
|
|
||||||
vectorContext.setStyle(style);
|
vectorContext.setStyle(style);
|
||||||
vectorContext.drawGeometry(flashGeom);
|
vectorContext.drawGeometry(flashGeom);
|
||||||
if (elapsed > duration) {
|
|
||||||
unByKey(listenerKey);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// tell OpenLayers to continue postrender animation
|
// tell OpenLayers to continue postrender animation
|
||||||
map.render();
|
map.render();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user