This example shows how to use postrender and vectorContext to animate features. Here we choose to do a flash animation each time a feature is added to the layer. 此示例演示如何使用 postrender 和 vectorContext 对要素进行动画处理。 做法是当一个要素被添加到图层时给它添加闪烁效果。
/** * Adds the given layer to the top of this map. If you want to add a layer * elsewhere in the stack, use `getLayers()` and the methods available on * {@link module:ol/Collection~Collection}. * @param {import("./layer/Base.js").default}layer Layer. * @api */ addLayer(layer) { const layers = this.getLayerGroup().getLayers(); layers.push(layer); }
/** * Transforms a coordinate from longitude/latitude to a different projection. * @param {import("./coordinate.js").Coordinate}coordinate Coordinate as longitude and latitude, i.e. * an array with longitude as 1st and latitude as 2nd element. * @param {ProjectionLike=}opt_projection Target projection. The * default is Web Mercator, i.e. 'EPSG:3857'. * @return {import("./coordinate.js").Coordinate}Coordinate projected to the target projection. * @api */ exportfunctionfromLonLat(coordinate, opt_projection) { return transform( coordinate, "EPSG:4326", opt_projection !== undefined ? opt_projection : "EPSG:3857" ); }
/** * Start slow and speed up. * @param {number}t Input between 0 and 1. * @return {number}Output between 0 and 1. * @api */ exportfunctioneaseIn(t) { returnMath.pow(t, 3); }
/** * Start fast and slow down. * @param {number}t Input between 0 and 1. * @return {number}Output between 0 and 1. * @api */ exportfunctioneaseOut(t) { return1 - easeIn(1 - t); }
/** * Start slow, speed up, and then slow down again. * @param {number}t Input between 0 and 1. * @return {number}Output between 0 and 1. * @api */ exportfunctioninAndOut(t) { return3 * t * t - 2 * t * t * t; }
/** * Maintain a constant speed over time. * @param {number}t Input between 0 and 1. * @return {number}Output between 0 and 1. * @api */ exportfunctionlinear(t) { return t; }
/** * Start slow, speed up, and at the very end slow down again. This has the * same general behavior as {@link module:ol/easing~inAndOut}, but the final * slowdown is delayed. * @param {number}t Input between 0 and 1. * @return {number}Output between 0 and 1. * @api */ exportfunctionupAndDown(t) { if (t < 0.5) { return inAndOut(2 * t); } else { return1 - inAndOut(2 * (t - 0.5)); } }
之前 demo 中没有见到的 API 还有ol.Observable.unByKey,这个 API 很奇怪,不知道为什么在官网查询 API 的时候查不到,但是在源码中却能看到。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * Removes an event listener using the key returned by `on()` or `once()`. * @param {import("./events.js").EventsKey|Array<import("./events.js").EventsKey>}key The key returned by `on()` * or `once()` (or an array of keys). * @api */ exportfunctionunByKey(key) { if (Array.isArray(key)) { for (let i = 0, ii = key.length; i < ii; ++i) { unlistenByKey(key[i]); } } else { unlistenByKey(/** @type {import("./events.js").EventsKey} */ (key)); } }