我们如何使用样式但不使用地图将OpenLayers 3功能渲染到画布?

时间:2016-08-03 20:16:34

标签: openlayers-3

问题

如何使用样式但不使用地图向画布渲染要素?

背景

我有一个示例,它将几何体呈现为一个符合ol3样式的画布,但它只运行openlayers的未构建版本(ol-debug.js),因为它使用了私有函数(ol.vec.Mat4)

works when using ol-debug.js

fails when using ol.js

另一种方法是创建地图,添加矢量图层,在要素上设置样式,将要素添加到图层并从地图中删除所有事件/控件,使其看起来像画布。

第二种是使用goog.vec.Mat4

let scale = Math.min(canvas.width / ol.extent.getWidth(extent), canvas.height / ol.extent.getHeight(extent));
console.log("scale", scale);

let transform = Mat4.makeTransform2D(identity,
    canvas.width / 2, canvas.height / 2, // translate to origin
    scale, -scale, //scale
    0, // rotation
    -center[0], -center[1] // translate back
);
console.log("transform", transform);

let renderer = new ol.render.canvas.Immediate(ctx, 1, extent, transform, 1);
renderer.drawFeature(feature, style);

第三个类似于第二个,因为我在使用ol.render.toContext之前负责将几何转换为像素坐标,如example所示。

我认为那是废气吗?或者还有另一种方式吗?

1 个答案:

答案 0 :(得分:3)

卫生署!在openlayers网站上找到example权限!

在该示例中,坐标已经是像素:

<!DOCTYPE html>
<html>
  <head>
    <title>Render geometries to a canvas</title>
    <link rel="stylesheet" href="http://openlayers.org/en/v3.17.1/css/ol.css" type="text/css">
    <script src="http://openlayers.org/en/v3.17.1/build/ol.js"></script>
  </head>
  <body>
    <canvas id="canvas"></canvas>
    <script>
      var canvas = document.getElementById('canvas');
      var vectorContext = ol.render.toContext(canvas.getContext('2d'), {size: [100, 100]});

      var fill = new ol.style.Fill({color: 'blue'});
      var stroke = new ol.style.Stroke({color: 'black'});
      var style = new ol.style.Style({
        fill: fill,
        stroke: stroke,
        image: new ol.style.Circle({
          radius: 10,
          fill: fill,
          stroke: stroke
        })
      });
      vectorContext.setStyle(style);

      vectorContext.drawGeometry(new ol.geom.LineString([[10, 10], [90, 90]]));
      vectorContext.drawGeometry(new ol.geom.Polygon([[[2, 2], [98, 2], [2, 98], [2, 2]]]));
      vectorContext.drawGeometry(new ol.geom.Point([88, 88]));
    </script>
  </body>
</html>

但正如问题所示,翻译 - &gt;规模 - &gt;翻译转换数据:

function render(canvas: HTMLCanvasElement, line: ol.Coordinate[], style: ol.style.Style) {
    let extent = ol.extent.boundingExtent(line);
    let [dx, dy] = ol.extent.getCenter(extent);
    let [sx, sy] = [canvas.width / ol.extent.getWidth(extent), canvas.height / ol.extent.getHeight(extent)];
    line= translate(line, [-dx, -dy]);
    line= scale(line, [Math.min(sx, sy), -Math.min(sx, sy)]);
    line= translate(line, [canvas.width / 2, canvas.height / 2]);

    let feature = new ol.Feature({
        geometry: new ol.geom.Polygon([line]),
        style: style
    });

    let vtx = ol.render.toContext(canvas.getContext("2d"));
    vtx.drawFeature(feature, style);
}

这是我的TRS逻辑:

function translate(points: number[][], vector: number[]) {
    return points.map(p => vector.map((v, i) => v + p[i]));
}

function rotate(points: number[][], a: number) {
    return points.map(p => {
        let [x, y, cos, sin] = [p[0], p[1], Math.cos(a), Math.sin(a)];
        return [
            x * cos - y * sin,
            x * sin + y * cos
        ];
    });
}

function scale(points: number[][], vector: number[]) {
    return points.map(p => vector.map((v, i) => v * p[i]));
}