How can we render an OpenLayers 3 feature to a can

2019-05-21 00:59发布

QUESTION:

How can we render a feature to a canvas using a style but not using a map?

BACKGROUND:

I have a sample which renders a geometry to a canvas honoring the ol3 style but it only runs with the unbuilt version of openlayers (ol-debug.js) and because it makes use of private functions (ol.vec.Mat4).

works when using ol-debug.js

fails when using ol.js

One alternative is to create a map, add a vector layer, set the style on the feature, add the feature to the layer and remove all the events/controls from the map so it looks like a canvas.

A second is to use 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);

A third is similar to the second in that I take on the responsibility of transforming the geometry into pixel coordinates before using ol.render.toContext, as demonstrated in this example.

I think that about exhausts it? Or is there another way?

1条回答
冷血范
2楼-- · 2019-05-21 01:45

Doh! Found an example right on the openlayers site!

In that sample coordinates are already pixels:

<!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>

But as the question indicates, translation -> scale -> translation transforms the data:

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

Here is my TRS logic:

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]));
}
查看更多
登录 后发表回答