如何从 openlayers 6 中地图的 postcompose 事件中获取 canvas
How to get canvas from postcompose event of map in openlayers 6
我们使用的是 openlayers,在 5.3 版中我们使用的是这种结构:
map.once('postcompose', async(event) => {
let canvas = event.context.canvas;
// doing something with canvas
}
但在 openLayers 6.0 中,event 的参数上下文未定义(这当然破坏了我们的应用程序)。
我读 here 是:
层不再由单个 Canvas 元素组成。相反,它们作为单独的元素添加到地图视口中。
那么如何获取单层的canvas呢?
我还读到here:
Canvas 上下文。当事件由地图调度时不可用。仅在使用 Canvas 渲染器时可用,否则为 null。
是否有可能以某种方式将 canvas 渲染器设置为所有层,以便 CanvasRenderingContext2D 不是未定义的 'postcompose' 事件 ?
使用 ol6 在层上使用 postrender
事件和新的 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
});
我们使用的是 openlayers,在 5.3 版中我们使用的是这种结构:
map.once('postcompose', async(event) => {
let canvas = event.context.canvas;
// doing something with canvas
}
但在 openLayers 6.0 中,event 的参数上下文未定义(这当然破坏了我们的应用程序)。
我读 here 是:
层不再由单个 Canvas 元素组成。相反,它们作为单独的元素添加到地图视口中。
那么如何获取单层的canvas呢?
我还读到here:
Canvas 上下文。当事件由地图调度时不可用。仅在使用 Canvas 渲染器时可用,否则为 null。
是否有可能以某种方式将 canvas 渲染器设置为所有层,以便 CanvasRenderingContext2D 不是未定义的 'postcompose' 事件 ?
使用 ol6 在层上使用 postrender
事件和新的 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
});