如何从openlayers 6中地图的postcompose事件获取画布

Gal*_*Gal 3 javascript dictionary canvas openlayers openlayers-6

我们使用的是 openlayers,在 5.3 版本中我们使用的是这种结构:

map.once('postcompose', async(event) => {
  let canvas = event.context.canvas;
  // doing something with canvas
}
Run Code Online (Sandbox Code Playgroud)

但在 openLayers 6.0 中,事件的参数上下文未定义(这当然破坏了我们的应用程序)。

在这里读到:

图层不再组成单个 Canvas 元素。相反,它们作为单独的元素添加到地图视口中。

那么如何获取单层的画布呢?

我还在这里读到:

画布上下文。当事件由地图调度时不可用。仅当使用 Canvas 渲染器时才可用,否则为 null。

是否可以以某种方式将画布渲染器设置为所有图层,以便 CanvasRenderingContext2D 不会为“postcompose”事件未定义?

Vig*_*arc 5

ol6postrender在图层上使用事件,新getVectorContext函数提供对即时矢量渲染 API 的访问。
请参阅https://github.com/openlayers/openlayers/releases/tag/v6.0.0

要获取单个图层上的渲染上下文:

import {getVectorContext} from 'ol/render';

// construct your map and layers as usual

layer.on('postrender', function(event) {
  const vectorContext = getVectorContext(event);
  // use any of the drawing methods on the vector context
});
Run Code Online (Sandbox Code Playgroud)