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

Parameter 'lineOffset': offset line to the left or right. #103

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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%'`).
Expand Down
1 change: 1 addition & 0 deletions src/L.PolylineDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ L.PolylineDecorator = L.FeatureGroup.extend({
offset: parseRelativeOrAbsoluteValue(patternDef.offset),
endOffset: parseRelativeOrAbsoluteValue(patternDef.endOffset),
repeat: parseRelativeOrAbsoluteValue(patternDef.repeat),
lineOffset: patternDef.lineOffset
};
},

Expand Down
18 changes: 12 additions & 6 deletions src/patternUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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,
};
});
Expand All @@ -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,
};
}

Expand Down