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

anchor Markers at their center #4751

Merged
merged 5 commits into from
Jul 7, 2017
Merged

Conversation

andrewharvey
Copy link
Collaborator

@andrewharvey andrewharvey commented May 25, 2017

Closes #2900 (breaking change!)

Not sure if this is the best approach (tweaking transform translate values) or a CSS approach is best.

Currently if you change the HTML Element's size, it will loose centering until the map camera changes and triggers Marker._update.

Launch Checklist

 - [x] document any changes to public APIs
 - [ ] post benchmark scores
Need to check this extra calculation at each camera change doesn't add much overhead.
 - [x] manually test the debug page
looks okay (including when the element has a border).

@jfirebaugh
Copy link
Contributor

Seems good to me. We could switch to a CSS approach later (including adding a anchor option like Popup) if it's needed.

@mollymerp
Copy link
Contributor

Thanks @andrewharvey ! Can you also round the offsets to the nearest pixel? We had issues w subpixel transforms causing Popups to render blurry on some screens.

@andrewharvey andrewharvey force-pushed the 2900-marker-offset-centered branch 2 times, most recently from a266c43 to d403456 Compare June 8, 2017 06:39
@andrewharvey
Copy link
Collaborator Author

Can you also round the offsets to the nearest pixel? We had issues w subpixel transforms causing Popups to render blurry on some screens.

Actually they are already rounded at

// because rounding the coordinates at every `move` event causes stuttered zooming

I've pulled out this test to get it passing, for some reason in the testing infrastructure the DOM element.offsetWidth isn't working so the test fails.

t.test('marker centered by default', (t) => {
    const map = createMap();
    const element = window.document.createElement('div');
    element.style.width = '10px';
    element.style.height = '10px';
    element.style.border = '6px solid black';

    const marker = new Marker(element).setLngLat([0, 0]).addTo(map);
    const translate = Math.round((map.getContainer().offsetWidth / 2) - ((6 + 10 + 6) / 2));
    t.equal(marker.getElement().style.transform, `translate(${translate}px, ${translate}px)`, 'Marker centered');
    t.end();
});

I've manually tested and it works well even when the Marker has a padding or border.

@mollymerp
Copy link
Contributor

@andrewharvey I believe the failing test is due to the changes from upgrading JSDOM (16c75b8). You'll have to use Object.defineProperty as in https://github.com/mapbox/mapbox-gl-js/blob/master/test/unit/ui/control/attribution.test.js#L45

@andrewharvey
Copy link
Collaborator Author

@andrewharvey I believe the failing test is due to the changes from upgrading JSDOM (16c75b8). You'll have to use Object.defineProperty as in https://github.com/mapbox/mapbox-gl-js/blob/master/test/unit/ui/control/attribution.test.js#L45

Thanks for the pointer @mollymerp. I've updated the unit test now, though it is far away from a real world browser but it's better than nothing I suppose.

Copy link
Contributor

@mollymerp mollymerp left a comment

Choose a reason for hiding this comment

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

Thanks, @andrewharvey!

@mollymerp
Copy link
Contributor

Ooo one thing I missed @andrewharvey – I think we should pass the offset to the popup when its bound to a Marker with setPopup, but allow it to be overridden if the user specifies different offsets.

@andrewharvey
Copy link
Collaborator Author

Ooo one thing I missed @andrewharvey – I think we should pass the offset to the popup when its bound to a Marker with setPopup, but allow it to be overridden if the user specifies different offsets.

I think I have this working now, but I'd like to test it more and I'll need to look into a unit test.

@mollymerp
Copy link
Contributor

Ooh sorry for not being more clear – I meant offset the popup by the same additional amount as the marker {x: -offsetWidth / 2, y: -offsetHeight / 2} unless they specify alternate offsets for the Popup, so that its also anchored at the Marker's center

@jfirebaugh jfirebaugh force-pushed the 2900-marker-offset-centered branch from ada9c7e to c740468 Compare July 7, 2017 19:08
@jfirebaugh jfirebaugh merged commit 6065823 into master Jul 7, 2017
@jfirebaugh jfirebaugh deleted the 2900-marker-offset-centered branch July 7, 2017 19:21
@mollymerp mollymerp mentioned this pull request Aug 14, 2017
5 tasks
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.

3 participants