-
Notifications
You must be signed in to change notification settings - Fork 26
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
Native MGL integration #886
Conversation
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.
Good work! But, pls, try to avoid huge PR like this one, at least some changes were outside the scope of the PR like the movement of some docs. Thx!
examples/basics/add-layer.html
Outdated
@@ -37,10 +37,7 @@ <h1>Add layer</h1> | |||
container: 'map', | |||
style: 'https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json', | |||
center: [0, 30], | |||
zoom: 2, | |||
scrollZoom: false, |
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.
Why?
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.
Just for testing. I'll restore it
src/Layer.js
Outdated
}); | ||
|
||
this.id = id; | ||
this.type = 'custom'; |
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.
Why is this added?
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.
The idea is to convert carto.Layer
in a native MGL CustomLayer. I'll add a comment there
src/Layer.js
Outdated
@@ -138,7 +115,7 @@ export default class Layer { | |||
} | |||
|
|||
/** | |||
* Add this layer to a map. | |||
* Add this layer to a map. It waits for the map to be loaded. |
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.
I would change the wording
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.
Wait for the map to load
?
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.
I would say something like: If the map's style is not loaded yet it will wait for it to add the layer as soon as possible
src/Layer.js
Outdated
* @api | ||
*/ | ||
removeFrom (map) { | ||
map.removeLayer(this); |
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.
This will throw an error if the map hasn't been loaded yet and the layer hasn't been added yet
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.
Yes and MGL this is the expected behaviour IMO.
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.
Yes, but since we are making things easier in addTo for a similar problem, we could improve this, but I realize that this feature request is not simple, I think we could create an issue with it, are you ok?
* @api | ||
*/ | ||
async update (source, viz) { | ||
if (viz === undefined) { | ||
// Use current viz | ||
viz = this._viz; |
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.
I don't remember well but I think this wasn't done before for a reason, this should be tested well
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.
I found an example using layer.getViz()
as a second argument, so this could be the default behaviour. I'll add tests for this.
src/renderer/Renderer.js
Outdated
const b = map.getBounds(); | ||
const nw = b.getNorthWest(); | ||
const sw = b.getSouthWest(); | ||
const z = (util.projectToWebMercator(nw).y - util.projectToWebMercator(sw).y) / util.WM_2R; |
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.
I don't think this is a good idea either, the Renderer didn't know about webmercator or maps at all before
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.
Yes, I have decoupled the map so this functions are not in the Renderer anymore
src/renderer/Renderer.js
Outdated
this._zoom = zoom; | ||
_initCenter (map) { | ||
const c = map.getCenter(); | ||
this.setCenter(c.lng / 180.0, util.projectToWebMercator(c).y / util.WM_R); |
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.
same
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.
src/utils/util.js
Outdated
if (m1 && m2 && m1.length === m2.length) { | ||
let ret = true; | ||
for (let i = 0; i < m1.length; i++) { | ||
ret &= m1[i] === m2[i]; |
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.
this would be simpler and faster without the ret variable, early returning a false when the values mismatch
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.
Yes, I think I changed it already 🤔
@@ -1,8 +1,6 @@ | |||
const map = new carto.Map({ | |||
container: 'map', | |||
background: 'black', | |||
center: [-75, 40], |
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.
this was set to avoid draw ordering issues
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.
But this test uses carto.Map
that has no center
, zoom
attributes...
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.
ah, true, yes, I probably copy pasted them from the example I was thinking about
@@ -57,6 +57,6 @@ function project (coordinates) { | |||
const wm = projectToWebMercator(coordinates); | |||
return { | |||
x: mapSize * (0.5 + wm.x / WM_2R), | |||
y: mapSize * (0.5 - wm.y / WM_2R + 0.03 /* offset */) | |||
y: mapSize * (0.5 - wm.y / WM_2R) |
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.
Could you explain this since you introduced the magic 0.03
? Why was it added? The addition was done on: 7b46dee
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.
This changed was required to match the bounding box in the simulated mouse events. I have updated the bounding box in the carto.Map
so this is not required anymore.
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.
I don't really understand it, but if you are sure... 👍
4e1186c
to
a1de080
Compare
Suggestions and more improvements added. |
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.
I think this is ready to be merged, but I also think this is the most dangerous PR of this release cycle, please wait for the merging of the other PRs to leave this PR as the last PR of the new release
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.
Minor code style issues
src/Layer.js
Outdated
* @type {LayerEvent} | ||
* @api | ||
*/ | ||
function getRenderer (map, gl) { |
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.
This is a private function that should be placed at the bottom (after the class) and should be _getRenderer
instead of getRenderer
.
src/Layer.js
Outdated
// TODO: the best solution is to use the matrix at the shader | ||
// level and remove the aspect and scale logic from the renderer | ||
this.renderer.setCenter( | ||
-(1 + 2 * matrix[12] / matrix[0]), |
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.
Could we move this center
logic to an external utils file or similar?
src/Layer.js
Outdated
|
||
_setRendererCenter () { | ||
const c = this.map.getCenter(); | ||
this.renderer.setCenter(c.lng / 180.0, util.projectToWebMercator(c).y / util.WM_R); |
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.
Use constant instead of a number (180.0
)
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.
This is legacy code. It can be done with projectToWebMercator(c).x, etc but this is shorter. However, I'll move this to an utils file
093fbdb
to
41869c4
Compare
91a4c57
to
edaef0b
Compare
Related to #700, #428, #847.
This branch adds a re-implementation with some refactors for the MGL integration interface using the new Custom Layers API. Since this feature is not already in a stable version I have released from our fork the version
v0.48.0-carto1
(branch), from the stablev0.48.0
tag and the changes fromcustom-layers
.This is a pretty big PR. However, the main activity is in the files Layer, Renderer and Map. Here is a list of changes in this branch:
onAdd
,onRemove
,prerender
andrender
functions of Custom LayersLayer.removeFrom
maps/Map
to match the new interfacelayer.update(source)
using current viz by defaultIf we are interested in testing MGL upstream I can release another version (v0.48.0-carto2) pulling the changes from master.