简体   繁体   English

如何从 openlayers 6 中 map 的 postcompose 事件中获取 canvas

[英]How to get canvas from postcompose event of map in openlayers 6

We are using openlayers and in version 5.3 we were using this construction:我们正在使用 openlayers,在 5.3 版本中我们使用了这种结构:

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

But in openLayers 6.0 the argument context is undefined for event (which of course broke our application).但是在 openLayers 6.0 中,事件的参数上下文是未定义的(这当然破坏了我们的应用程序)。

I read here that:在这里读到:

Layers are no longer composed to a single Canvas element.层不再由单个 Canvas 元素组成。 Instead, they are added to the map viewport as individual elements.相反,它们作为单独的元素添加到 map 视口中。

So how do I get the canvas of a single layer?那么如何获得单层的canvas呢?

I also read here that:我还在这里读到:

Canvas context. Canvas 上下文。 Not available when the event is dispatched by the map.当事件由 map 调度时不可用。 Only available when a Canvas renderer is used, null otherwise.仅在使用 Canvas 渲染器时可用,否则为 null。

Is it somehow possible to set canvas renderer to all the layers so that CanvasRenderingContext2D is not undefined for the 'postcompose' event ?是否可以以某种方式将 canvas 渲染器设置为所有图层,以便 CanvasRenderingContext2D 不会为“postcompose”事件定义?

With ol6 use postrender event on layers and new getVectorContext function provides access to the immediate vector rendering API.使用 ol6 在图层上使用postrender事件和新getVectorContext function 提供对立即矢量渲染 API 的访问。
See https://github.com/openlayers/openlayers/releases/tag/v6.0.0https://github.com/openlayers/openlayers/releases/tag/v6.0.0

To get render context on a single layer:要在单个图层上获取渲染上下文:

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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM