-
Notifications
You must be signed in to change notification settings - Fork 85
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
Add new handler: src/handlers/lineargradient.js
#257
Conversation
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)"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jtpio it's definitely not perfect (performances ?), but please let me know if you already see any room for improvement. |
Yes it works right now on Chrome and Safari. But this problem still exists on Safari: |
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? |
// 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])); | ||
} | ||
|
||
}); |
There was a problem hiding this comment.
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:
leaflet-elevation/libs/leaflet-hotline.js
Lines 238 to 266 in e0c68cb
_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..)
There was a problem hiding this comment.
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
Feature released in v2.5.0 |
Nice thanks @Raruto! |
Preview
Add support for graduated chart profiles colors:
Sample usage:
Further information:
<linearGradient>
👉 whenpreferCanvas: false
CanvasGradient
👉 whenpreferCanvas: true
Closes: #251
Partially handles: #232