Skip to content
Browse files

Polygon rendering (no holes yet)

  • Loading branch information...
1 parent 0414e33 commit 497eac82c28e685ac56a9820ca931b53dd284f88 @tschaub tschaub committed Jan 19, 2013
Showing with 73 additions and 0 deletions.
  1. +73 −0 src/ol/renderer/canvas/canvasrenderer.js
View
73 src/ol/renderer/canvas/canvasrenderer.js
@@ -9,8 +9,10 @@ goog.require('ol.canvas');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LineString');
goog.require('ol.geom.Point');
+goog.require('ol.geom.Polygon');
goog.require('ol.style.LiteralLine');
goog.require('ol.style.LiteralPoint');
+goog.require('ol.style.LiteralPolygon');
goog.require('ol.style.LiteralShape');
goog.require('ol.style.ShapeType');
@@ -117,6 +119,77 @@ ol.renderer.canvas.Renderer.prototype.renderPoints =
/**
+ * @param {Array.<ol.geom.Polygon>} polygons Array of polygons.
+ * @param {ol.style.LiteralPolygon} symbolizer Polygon symbolizer.
+ */
+ol.renderer.canvas.Renderer.prototype.renderPolygons =
+ function(polygons, symbolizer) {
+
+ var context = this.context_,
+ strokeStyle = symbolizer.strokeStyle,
+ fillStyle = symbolizer.fillStyle,
+ i, ii, poly, rings, numRings, coords, dim, j, jj, x, y;
+
+ context.globalAlpha = symbolizer.opacity;
+ if (strokeStyle) {
+ context.strokeStyle = symbolizer.strokeStyle;
+ context.lineWidth = symbolizer.strokeWidth;
+ }
+ if (fillStyle) {
+ context.fillStyle = fillStyle;
+ }
+
+ /**
+ * Four scenarios covered here:
+ * 1) stroke only, no holes - only need to have a single path
+ * 2) fill only, no holes - only need to have a single path
+ * 3) fill and stroke, no holes
+ * 4) holes - render polygon to sketch canvas first
+ */
+ context.beginPath();
+ for (i = 0, ii = polygons.length; i < ii; ++i) {
+ poly = polygons[i];
+ dim = poly.dimension;
+ rings = poly.rings;
+ numRings = rings.length;
+ if (numRings > 1) {
+ // scenario 4
+ // TODO: use sketch canvas to render outer and punch holes for inner rings
+ throw new Error('Rendering holes not implemented');
+ } else {
+ coords = rings[0].coordinates;
+ for (j = 0, jj = coords.length; j < jj; j += dim) {
+ x = coords[j];
+ y = coords[j + 1];
+ if (j === 0) {
+ context.moveTo(x, y);
+ } else {
+ context.lineTo(x, y);
+ }
+ }
+ if (fillStyle && strokeStyle) {
+ // scenario 3 - fill and stroke each time
+ context.fill();
+ context.stroke();
+ if (i < ii - 1) {
+ context.beginPath();
+ }
+ }
+ }
+ }
+ if (!(fillStyle && strokeStyle)) {
+ if (fillStyle) {
+ // scenario 2 - fill all at once
+ context.fill();
+ } else {
+ // scenario 1 - stroke all at once
+ context.stroke();
+ }
+ }
+};
+
+
+/**
* @param {ol.style.LiteralShape} circle Shape symbolizer.
* @return {!HTMLCanvasElement} Canvas element.
* @private

0 comments on commit 497eac8

Please sign in to comment.
Something went wrong with that request. Please try again.