From 71a6c4f2701521680c216360a537e0c220d729cd Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 14 Oct 2013 16:43:47 -0600 Subject: [PATCH] Only call lineTo if we are in a new pixel for linestring Rendering performance degrades as the context lineWidth increases when calling lineTo many times for the same pixel coordinate. This appears to be a widespread performance issue and is very clearly improved by only calling lineTo when in a new pixel coordinate. --- .../renderer/canvas/canvasvectorrenderer.js | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/ol/renderer/canvas/canvasvectorrenderer.js b/src/ol/renderer/canvas/canvasvectorrenderer.js index c6b0d87f0c..ccb17c7df3 100644 --- a/src/ol/renderer/canvas/canvasvectorrenderer.js +++ b/src/ol/renderer/canvas/canvasvectorrenderer.js @@ -144,8 +144,12 @@ ol.renderer.canvas.Vector.prototype.renderLineStringFeatures_ = function(features, symbolizer) { var context = this.context_, - i, ii, feature, id, currentSize, geometry, components, j, jj, line, - k, kk, vec, strokeSize; + i, ii, feature, id, currentSize, geometry, components, j, jj, + coordinates, coordinate, k, kk, strokeSize; + + var vec = [NaN, NaN, 0]; + var pixel = [NaN, NaN]; + var lastPixel = [NaN, NaN]; context.globalAlpha = symbolizer.opacity; context.strokeStyle = symbolizer.color; @@ -175,14 +179,24 @@ ol.renderer.canvas.Vector.prototype.renderLineStringFeatures_ = components = geometry.getComponents(); } for (j = 0, jj = components.length; j < jj; ++j) { - line = components[j]; - for (k = 0, kk = line.getCount(); k < kk; ++k) { - vec = [line.get(k, 0), line.get(k, 1), 0]; + coordinates = components[j].getCoordinates(); + for (k = 0, kk = coordinates.length; k < kk; ++k) { + coordinate = coordinates[k]; + vec[0] = coordinate[0]; + vec[1] = coordinate[1]; goog.vec.Mat4.multVec3(this.transform_, vec, vec); if (k === 0) { + lastPixel[0] = NaN; + lastPixel[1] = NaN; context.moveTo(vec[0], vec[1]); } else { - context.lineTo(vec[0], vec[1]); + pixel[0] = Math.round(vec[0]); + pixel[1] = Math.round(vec[1]); + if (pixel[0] !== lastPixel[0] || pixel[1] !== lastPixel[1]) { + context.lineTo(vec[0], vec[1]); + lastPixel[0] = pixel[0]; + lastPixel[1] = pixel[1]; + } } } }