简体   繁体   English

使用带有静态图像的 OpenLayers Canvas 源

[英]Use OpenLayers Canvas source with Image static

I've been trying to draw on the Canvas using OpenLayers (v6.1.1) but unfortunately, I'm struggling with the Canvas implementation.我一直在尝试使用 OpenLayers (v6.1.1) 在 Canvas 上绘图,但不幸的是,我在 Canvas 实现上苦苦挣扎。

I've tried this simple approach:我试过这个简单的方法:

const extent = [0, 0, 1024, 968];

const projection = new ol.proj.Projection({
  code: 'pixel-projection',
  units: 'pixels',
  extent: extent,
});

let canvas = document.createElement('canvas');

canvas.width = 1024;
canvas.height = 968;
let ctx = canvas.getContext('2d');
ctx.fillStyle = 'green';
ctx.fillRect(0, 0, 100, 100);

const imageLayer = new ol.layer.Image({
  source: new ol.source.ImageCanvas({
    canvas: canvas,
    projection: projection,
    imageExtent: extent,
  }),
});

but I'm getting an undefined error:但我收到一个未定义的错误:

在此处输入图像描述

Also, here is a fiddle for easier navigation: https://jsfiddle.net/Loque/ur5gw270/4/此外,这里是一个更容易导航的小提琴: https ://jsfiddle.net/Loque/ur5gw270/4/

Any help would be highly appreciated, thank you.任何帮助将不胜感激,谢谢。

ImageCanvas needs a canvasFunction option: ImageCanvas需要一个canvasFunction选项:

  source: new ol.source.ImageCanvas({
    canvasFunction: function() {
      return canvas;
    },
    projection: projection,
    imageExtent: extent,
    ratio: 1,
  }),

ImageStatic needs a url option: ImageStatic需要一个url选项:

  source: new ol.source.ImageStatic({
    url: canvas.toDataURL(),
    projection: projection,
    imageExtent: extent,
  }),

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

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