bemky / mapbox-gl-draw-freehand-mode

Mode for Mapbox GL Draw that simplifies UI to only free drawing
https://github.com/mapbox/mapbox-gl-draw
ISC License
70 stars 43 forks source link

Tolerance issues at different zoom levels #11

Closed laurenoldham1202 closed 3 years ago

laurenoldham1202 commented 3 years ago

Hi there!

I'm trying to use this freehand mode plugin for my project and I'm getting errors at small-scale views, e.g. zoom levels ~0-4.

You can replicate the error in the official plugin demo: https://bemky.github.io/mapbox-gl-draw-freehand-mode/

Screen Shot 2021-06-08 at 10 41 47 AM

The console logs the error from @turf/turf/simplify:

Uncaught Error: invalid tolerance
    at Me (main.es.js:167)
    at Q.ke.onMouseUp (index.js:52)
    at Object.<anonymous> (object_to_mode.js:33)
    at n (mode_handler.js:40)
    at Object.mouseup (mode_handler.js:88)
    at n.mouseup (events.js:72)
    at n.It.fire (mapbox-gl.js:31)
    at n.fire (setup.js:52)
    at Ri.mouseup (mapbox-gl.js:35)
    at gn.handleEvent (mapbox-gl.js:35)

The issue stems from the code:

var tolerance = (3 / ((this.map.getZoom()-4) * 150)) - 0.001 // https://www.desmos.com/calculator/b3zi8jqskw 
simplify(state.polygon, {
    mutate: true,
    tolerance: tolerance,
    highQuality: true 
});

I played around with it locally, and when you take out this tolerance, small-scale drawings work, BUT large-scale (zoomed-in) drawings now fail.

Is there a way to set the tolerance so that both small and large-scale shapes can be drawn on the map with freehand mode error-free?

bemky commented 3 years ago

I'll take a look. There are few open issues about this tolerance calculation.

bemky commented 3 years ago

I just pushed a new release, that I believe fixes this problem https://github.com/bemky/mapbox-gl-draw-freehand-mode/releases/tag/2.1.0

laurenoldham1202 commented 3 years ago

I just pushed a new release, that I believe fixes this problem https://github.com/bemky/mapbox-gl-draw-freehand-mode/releases/tag/2.1.0

Fantastic! This seems to do the trick. :) One caveat is that in my actual code, I had to update my event handlers because this fired both draw.update and draw.create for each drawn shape.

bemky commented 3 years ago

Interesting, I assume Mapboxes DrawPolygon functions the same way. I didn't really change anything that would have changed how events are fired.