Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paths are clipped before they are rendered #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 97 additions & 5 deletions src/L.LineUtil.PolylineDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ L.LineUtil.PolylineDecorator = {
return dist;
},

/**
* Calculates the length of the given path
* path: array of L.Point objects (pixel coordinates)
* Returns the length of the given path
*/
getLength: function(path) {
var numPoints = path.length;
if (numPoints < 2) {
return 0;
}
var i, len = 0, pt, prevPt = path[0];
for (i = 1; i < numPoints; ++i) {
len += prevPt.distanceTo(pt = path[i]);
prevPt = pt;
}
return len;
},

getPixelLength: function(pl, map) {
var ll = (pl instanceof L.Polyline) ? pl.getLatLngs() : pl,
nbPts = ll.length;
Expand All @@ -36,15 +54,13 @@ L.LineUtil.PolylineDecorator = {

/**
* path: array of L.LatLng
* pathAsPoints: array of L.Point
* offsetRatio: the ratio of the total pixel length where the pattern will start
* repeatRatio: the ratio of the total pixel length between two points of the pattern
* map: the map, to access the current projection state
*/
projectPatternOnPath: function (path, offsetRatio, repeatRatio, map) {
var pathAsPoints = [], i;
for(i=0, l=path.length; i<l; i++) {
pathAsPoints[i] = map.project(path[i]);
}
projectPatternOnPath: function (path, pathAsPoints, offsetRatio, repeatRatio, map) {
var i;
// project the pattern as pixel points
var pattern = this.projectPatternOnPointPath(pathAsPoints, offsetRatio, repeatRatio);
// and convert it to latlngs;
Expand Down Expand Up @@ -157,5 +173,81 @@ L.LineUtil.PolylineDecorator = {
}
// special case where points lie on the same vertical axis
return new L.Point(ptA.x, ptA.y + (ptB.y - ptA.y) * ratio);
},

/**
* Returns the resulting paths when clipping the given path with the given
* bounds
* path: array of Point objects (pixel coordinates)
* bounds: pixel bounds
* Returns array of clipped paths. Each clipped path carries its offset
* from the beginning of the source path
*/
clipPath: function (path, bounds) {
var pathBounds = L.bounds(path);
if (bounds.contains(pathBounds)) {
return [{
coords: path,
offset: 0
}];
}

var paths = [],
i,
distance = 0,
p;

for (i = 0; i < (path.length - 1); ++i) {
// Assumes L.LineUtil.clipSegment return false if the line is
// outside the bounds
// Assumes that L.LineUtil.clipSegment does not modify p1/p2
// directly
var clipped = L.LineUtil.clipSegment(path[i], path[i + 1], bounds);
if (clipped !== false) {

var p1 = clipped[0],
p2 = clipped[1],
distInSegment = p1.distanceTo(path[i]);

// Start new path
if (p === undefined) {
p = {
coords: [p1, p2],
offset: distance + distInSegment
};
}

// Continue existing path?
else {

// Add coordinate to already started path
if (p.coords[p.coords.length - 1].equals(p1)) {
p.coords.push(p2);
}

// End started path, and start a new path
else {
paths.push(p);
p = {
coords: [p1, p2],
offset: distance + distInSegment
};
}

}

}

// Keep track of the distance to the current point
distance += path[i].distanceTo(path[i + 1]);

}

if (p !== undefined) {
paths.push(p);
}

return paths;
}

};
109 changes: 60 additions & 49 deletions src/L.PolylineDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ L.PolylineDecorator = L.LayerGroup.extend({
setPatterns: function(patterns) {
this.options.patterns = patterns;
this._initPatterns();
this._softRedraw();
this.redraw();
},

/**
Expand All @@ -99,7 +99,6 @@ L.PolylineDecorator = L.LayerGroup.extend({
*/
_parsePatternDef: function(patternDef, latLngs) {
var pattern = {
cache: [],
symbolFactory: patternDef.symbol,
isOffsetInPixels: false,
isRepeatInPixels: false
Expand Down Expand Up @@ -132,13 +131,13 @@ L.PolylineDecorator = L.LayerGroup.extend({
this._draw();
// listen to zoom changes to redraw pixel-spaced patterns
if(this._isZoomDependant) {
this._map.on('zoomend', this._softRedraw, this);
this._map.on('moveend', this.redraw, this);
}
},

onRemove: function (map) {
// remove optional map zoom listener
this._map.off('zoomend', this._softRedraw, this);
this._map.off('moveend', this.redraw, this);
this._map = null;
L.LayerGroup.prototype.onRemove.call(this, map);
},
Expand All @@ -154,71 +153,83 @@ L.PolylineDecorator = L.LayerGroup.extend({
return symbols;
},

_getCache: function(pattern, zoom, pathIndex) {
var zoomCache = pattern.cache[zoom];
if(typeof zoomCache === 'undefined') {
pattern.cache[zoom] = [];
return null;
}
return zoomCache[pathIndex];
},

/**
* Select pairs of LatLng and heading angle,
* that define positions and directions of the symbols
* on the path
*/
_getDirectionPoints: function(pathIndex, pattern) {
var zoom = this._map.getZoom();
var dirPoints = this._getCache(pattern, zoom, pathIndex);
if(dirPoints) {
return dirPoints;
var dirPoints = [];

var pxLength = null,
latLngs = this._paths[pathIndex];
var map = this._map;

var pxBounds = map.getPixelBounds();
// TODO: Buffer pxBounds to prevent large symbols being removed when
// rendered close to the border of the viewport. The user should maybe
// set the buffer?
var pxPath = latLngs.map(function (latLng) {
return map.project(latLng);
});

var clippedPxPaths = L.LineUtil.PolylineDecorator.clipPath(pxPath, pxBounds);

if (clippedPxPaths.length > 0) {
pxLength = L.LineUtil.PolylineDecorator.getLength(pxPath);
}

var offset, repeat, pathPixelLength = null, latLngs = this._paths[pathIndex];
if(pattern.isOffsetInPixels) {
pathPixelLength = L.LineUtil.PolylineDecorator.getPixelLength(latLngs, this._map);
offset = pattern.offset/pathPixelLength;
clippedPxPaths.forEach(function (clipped) {
var offset, repeat,
clippedLength = L.LineUtil.PolylineDecorator.getLength(clipped.coords);

repeat = this._calcRepeat(pattern, pxLength, clippedLength);
offset = this._calcOffset(pattern, pxLength, clippedLength, clipped.offset, repeat * clippedLength);

if (offset <= 1) {
var coordsAsLatLngs = clipped.coords.map(function (p) {
return map.unproject(p);
});
dirPoints = dirPoints.concat(L.LineUtil.PolylineDecorator.projectPatternOnPath(coordsAsLatLngs, clipped.coords, offset, repeat, map));
}

}, this);

return dirPoints;
},

_calcOffset: function (pattern, pxLength, clippedLength, clippedOffset, pxRepeat) {
var pxOffset,
offset;
// Calc pattern offset in pixels
if (pattern.isOffsetInPixels) {
pxOffset = pattern.offset;
} else {
offset = pattern.offset;
pxOffset = pattern.offset * pxLength;
}
// Calc pattern offset for the clipped path
if (clippedOffset <= pxOffset) {
offset = pxOffset - clippedOffset;
} else {
offset = Math.ceil((clippedOffset - pxOffset) / pxRepeat) * pxRepeat - (clippedOffset - pxOffset);
}
// Return pattern offset in percent
return offset / clippedLength;
},

_calcRepeat: function (pattern, pxLength, clippedLength) {
if(pattern.isRepeatInPixels) {
pathPixelLength = (pathPixelLength !== null) ? pathPixelLength : L.LineUtil.PolylineDecorator.getPixelLength(latLngs, this._map);
repeat = pattern.repeat/pathPixelLength;
return pattern.repeat / clippedLength;
} else {
repeat = pattern.repeat;
return pattern.repeat * pxLength / clippedLength;
}
dirPoints = L.LineUtil.PolylineDecorator.projectPatternOnPath(latLngs, offset, repeat, this._map);
// save in cache to avoid recomputing this
pattern.cache[zoom][pathIndex] = dirPoints;

return dirPoints;
},

/**
* Public redraw, invalidating the cache.
*/
redraw: function() {
this._redraw(true);
},

/**
* "Soft" redraw, called internally for example on zoom changes,
* keeping the cache.
*/
_softRedraw: function() {
this._redraw(false);
},

_redraw: function(clearCache) {
if(this._map === null)
return;
this.clearLayers();
if(clearCache) {
for(var i=0; i<this._patterns.length; i++) {
this._patterns[i].cache = [];
}
}
this._draw();
},

Expand Down