Edit

Render geometries to a canvas

render2 geometry2 canvas5

Example of rendering geometries to an arbitrary canvas.

This example shows how to render geometries to an arbitrary canvas.

main.js
import 'ol/ol.css';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
import {LineString, Point, Polygon} from 'ol/geom';
import {toContext} from 'ol/render';

var canvas = document.getElementById('canvas');
var vectorContext = toContext(canvas.getContext('2d'), {size: [100, 100]});

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

vectorContext.drawGeometry(
  new LineString([
    [10, 10],
    [90, 90] ])
);
vectorContext.drawGeometry(
  new Polygon([
    [
      [2, 2],
      [98, 2],
      [2, 98],
      [2, 2] ] ])
);
vectorContext.drawGeometry(new Point([88, 88]));
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Render geometries to a canvas</title>
    <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
    <script src="https://unpkg.com/elm-pep"></script>
    <style>
      .map {
        width: 100%;
        height:400px;
      }
    </style>
  </head>
  <body>
    <canvas id="canvas"></canvas>
    <script src="main.js"></script>
  </body>
</html>
package.json
{
  "name": "render-geometry",
  "dependencies": {
    "ol": "6.4.3"
  },
  "devDependencies": {
    "parcel": "1.11.0"
  },
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --experimental-scope-hoisting --public-url . index.html"
  }
}