From 2b7e7b8dbd43a49c2f09a89036a860463fb7e14a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Sat, 2 May 2020 18:35:44 +0200 Subject: [PATCH] Parameter 'lineOffset': offset line to the left or right. --- README.md | 1 + src/L.PolylineDecorator.js | 1 + src/patternUtils.js | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 09e3481..a2fc8c8 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ Property | Type | Required | Description `offset`| *see below* | No | Offset of the first pattern symbol, from the start point of the line. Default: 0. `endOffset`| *see below* | No | Minimum offset of the last pattern symbol, from the end point of the line. Default: 0. `repeat`| *see below* | Yes | Repetition interval of the pattern symbols. Defines the distance between each consecutive symbol's anchor point. +`lineOffset` | number (pixels) | No | Offset line to the left (negative value) or the right (positive value). Default: 0. `symbol`| Symbol factory | Yes | Instance of a symbol factory class. `offset`, `endOffset` and `repeat` can be each defined as a number, in pixels, or in percentage of the line's length, as a string (ex: `'10%'`). diff --git a/src/L.PolylineDecorator.js b/src/L.PolylineDecorator.js index 307cef7..d4e9353 100644 --- a/src/L.PolylineDecorator.js +++ b/src/L.PolylineDecorator.js @@ -85,6 +85,7 @@ L.PolylineDecorator = L.FeatureGroup.extend({ offset: parseRelativeOrAbsoluteValue(patternDef.offset), endOffset: parseRelativeOrAbsoluteValue(patternDef.endOffset), repeat: parseRelativeOrAbsoluteValue(patternDef.repeat), + lineOffset: patternDef.lineOffset }; }, diff --git a/src/patternUtils.js b/src/patternUtils.js index 7b0e44c..8a9c1d9 100644 --- a/src/patternUtils.js +++ b/src/patternUtils.js @@ -62,6 +62,7 @@ function projectPatternOnPointPath(pts, pattern) { const repeatIntervalPixels = totalPathLength * repeat; const startOffsetPixels = offset > 0 ? totalPathLength * offset : 0; const endOffsetPixels = endOffset > 0 ? totalPathLength * endOffset : 0; + const lineOffset = pattern.lineOffset || 0; // 2. generate the positions of the pattern as offsets from the path start const positionOffsets = []; @@ -84,7 +85,7 @@ function projectPatternOnPointPath(pts, pattern) { const segmentRatio = (positionOffset - segment.distA) / (segment.distB - segment.distA); return { - pt: interpolateBetweenPoints(segment.a, segment.b, segmentRatio), + pt: interpolateBetweenPoints(segment.a, segment.b, segmentRatio, lineOffset, segment.distB - segment.distA), heading: segment.heading, }; }); @@ -94,17 +95,22 @@ function projectPatternOnPointPath(pts, pattern) { * Finds the point which lies on the segment defined by points A and B, * at the given ratio of the distance from A to B, by linear interpolation. */ -function interpolateBetweenPoints(ptA, ptB, ratio) { +function interpolateBetweenPoints(ptA, ptB, ratio, lineOffset, length) { + let n = {x: 0, y: 0} + if (lineOffset !== 0) { + n = {x: - (ptB.y - ptA.y) / length, y: (ptB.x - ptA.x) / length} + } + if (ptB.x !== ptA.x) { return { - x: ptA.x + ratio * (ptB.x - ptA.x), - y: ptA.y + ratio * (ptB.y - ptA.y), + x: ptA.x + ratio * (ptB.x - ptA.x) + n.x * lineOffset, + y: ptA.y + ratio * (ptB.y - ptA.y) + n.y * lineOffset }; } // special case where points lie on the same vertical axis return { - x: ptA.x, - y: ptA.y + (ptB.y - ptA.y) * ratio, + x: ptA.x + n.x * lineOffset, + y: ptA.y + (ptB.y - ptA.y) * ratio + n.y * lineOffset, }; }