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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new handler: src/handlers/lineargradient.js #257

Merged
merged 2 commits into from
Jul 5, 2023
Merged

Conversation

Raruto
Copy link
Owner

@Raruto Raruto commented May 18, 2023

Preview

Add support for graduated chart profiles colors:

image

Sample usage:

L.control.Elevation({
  altitude: true,
  distance: true,
  handlers: [ 'Altitude', 'Distance', 'LinearGradient', ],
  linearGradient: {
    attr: 'z',
    path: 'altitude',
    range: { 0.0: '#008800', 0.5: '#ffff00', 1.0: '#ff0000' },
    min: 'elevation_min',
    max: 'elevation_max',
  },
})

Further information:

Closes: #251
Partially handles: #232

Comment on lines +617 to 623
if (
/Mac|iPod|iPhone|iPad/.test(navigator.platform) &&
/AppleWebkit/i.test(navigator.userAgent) &&
!/Chrome/.test(navigator.userAgent)
) {
canvas .style("transform", "translate(" + margins.left + "px," + margins.top + "px)");
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hupe13 do you confirm that this is the patch you proposed in here? 馃憠 #232

@Raruto
Copy link
Owner Author

Raruto commented May 18, 2023

@jtpio it's definitely not perfect (performances ?), but please let me know if you already see any room for improvement.

@hupe13
Copy link
Contributor

hupe13 commented May 19, 2023

do you confirm that this is the patch you proposed in here? 馃憠 #232

Yes it works right now on Chrome and Safari.

But this problem still exists on Safari:
grafik
This is your /examples/leaflet-elevation.html. I set preferCanvas: true,.

@jtpio
Copy link

jtpio commented May 22, 2023

@jtpio it's definitely not perfect (performances ?), but please let me know if you already see any room for improvement.

Thanks @Raruto this is looking pretty good already!

Just a minor note on the screenshot posted above. It looks like the red color should probably be associated with the steeper sections of the elevation graph?

image

Comment on lines +100 to +105
// Generate gradient for each segment picking colors from palette
for (let i = 0, data = this._data; i < data.length; i++) {
gradient.addColorStop((i) / data.length, palette.getRGBColor(data[i][attr]));
}

});
Copy link
Owner Author

@Raruto Raruto May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtpio ref: #257 (comment)

I tried to write an algorithm as similar as possible to the one used to generate the hotline colors:

_drawHotline: function (ctx) {
var i, j, dataLength, path, pathLength, pointStart, pointEnd,
gradient, gradientStartRGB, gradientEndRGB;
ctx.lineWidth = this._weight;
for (i = 0, dataLength = this._data.length; i < dataLength; i++) {
path = this._data[i];
for (j = 1, pathLength = path.length; j < pathLength; j++) {
pointStart = path[j - 1];
pointEnd = path[j];
// Create a gradient for each segment, pick start end end colors from palette gradient
gradient = ctx.createLinearGradient(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
gradientStartRGB = this.getRGBForValue(pointStart.z);
gradientEndRGB = this.getRGBForValue(pointEnd.z);
gradient.addColorStop(0, 'rgb(' + gradientStartRGB.join(',') + ')');
gradient.addColorStop(1, 'rgb(' + gradientEndRGB.join(',') + ')');
ctx.strokeStyle = gradient;
ctx.beginPath();
ctx.moveTo(pointStart.x, pointStart.y);
ctx.lineTo(pointEnd.x, pointEnd.y);
ctx.stroke();
}
}
}
};

but as you can see, I think that there is a shift in here: src/handlers/lineargradient.js.

By chance, are you able to take a look at how to fix it?

It took me quite a bit of work to get to this point (and right now I don't think I'll be using this code any time soon..)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meanwhile I'm going to merging this, feel free to open a new pull request to improve or fix this

@Raruto Raruto merged commit 0a4596c into master Jul 5, 2023
@Raruto Raruto deleted the linear-gradient branch July 5, 2023 14:18
@Raruto
Copy link
Owner Author

Raruto commented Jul 5, 2023

Feature released in v2.5.0

@jtpio
Copy link

jtpio commented Jul 17, 2023

Nice thanks @Raruto!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support showing the elevation segments on the Altitude area chart directly
3 participants