-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
debounce rounding position on Marker #11167
Changes from 4 commits
0ae8f5e
309e14a
f0dd607
dfb4113
a74e3a9
b2c7c41
1d0dab6
4c2b83c
1fc674a
4c08403
16fa227
dc792c3
9fefbd3
1c1281c
7895068
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,7 @@ export default class Marker extends Evented { | |
_rotationAlignment: string; | ||
_originalTabIndex: ?string; // original tabindex of _element | ||
_fadeTimer: ?TimeoutID; | ||
_updateFrameId: number; | ||
|
||
constructor(options?: Options, legacyOptions?: Options) { | ||
super(); | ||
|
@@ -256,7 +257,7 @@ export default class Marker extends Evented { | |
this.remove(); | ||
this._map = map; | ||
map.getCanvasContainer().appendChild(this._element); | ||
map.on('move', this._update); | ||
map.on('move', () => this._update(true)); | ||
map.on('moveend', this._update); | ||
map.on('remove', this._clearFadeTimer); | ||
map._addMarker(this); | ||
|
@@ -282,7 +283,7 @@ export default class Marker extends Evented { | |
remove() { | ||
if (this._map) { | ||
this._map.off('click', this._onMapClick); | ||
this._map.off('move', this._update); | ||
this._map.off('move', () => this._update(true)); | ||
this._map.off('moveend', this._update); | ||
this._map.off('mousedown', this._addDragHandler); | ||
this._map.off('touchstart', this._addDragHandler); | ||
|
@@ -337,7 +338,7 @@ export default class Marker extends Evented { | |
this._lngLat = LngLat.convert(lnglat); | ||
this._pos = null; | ||
if (this._popup) this._popup.setLngLat(this._lngLat); | ||
this._update(); | ||
this._update(true); | ||
return this; | ||
} | ||
|
||
|
@@ -507,7 +508,8 @@ export default class Marker extends Evented { | |
position.y >= 0 && position.y < tr.height; | ||
} | ||
|
||
_update(e?: {type: 'move' | 'moveend'}) { | ||
_update(delaySnap?: boolean) { | ||
window.cancelAnimationFrame(this._updateFrameId); | ||
if (!this._map) return; | ||
|
||
if (this._map.transform.renderWorldCopies) { | ||
|
@@ -533,7 +535,14 @@ export default class Marker extends Evented { | |
// because rounding the coordinates at every `move` event causes stuttered zooming | ||
// we only round them when _update is called with `moveend` or when its called with | ||
// no arguments (when the Marker is initialized or Marker#setLngLat is invoked). | ||
if (!e || e.type === "moveend") { | ||
if (delaySnap) { | ||
this._updateFrameId = window.requestAnimationFrame(() => { | ||
if (this._element && this._pos && this._anchor) { | ||
this._pos = this._pos.round(); | ||
DOM.setTransform(this._element, `${anchorTranslate[this._anchor]} translate(${this._pos.x}px, ${this._pos.y}px) ${pitch} ${rotation}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I wonder if we could deduplicate this long line of code that's duplicated below, but not a big deal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I don't have any idea, do you have? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe seperate that line into something like: function _updateDOM(){ DOM.setTransform(this._element, `${anchorTranslate[this._anchor]} translate(${this._pos.x}px, ${this._pos.y}px) ${pitch} ${rotation}`);} If you move the pitch and location calculating code into this function, then it can live in the marker's scope. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I separate some methods, I appreciate reviewing changes and giving your feedback |
||
} | ||
}); | ||
} else { | ||
this._pos = this._pos.round(); | ||
} | ||
|
||
|
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 won't actually remove the listener that was added previously, because this is a different function. You need to pass the exact reference — e.g. by creating a separate
_updateMoving
that internally calls_update(true)
.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.
Done!