Skip to content

Commit fce68e6

Browse files
committed
fix elevation chart drag on mobile
1 parent 7259a3e commit fce68e6

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

app/assets/stylesheets/feature.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@
162162
color: var(--color-dark-charcoal);
163163
height: 1rem;
164164
visibility: hidden;
165+
margin-left: 3rem
166+
}
167+
168+
@media (width <=574px) {
169+
#elevation-info-line {
170+
margin-left: 0.5rem
171+
}
165172
}
166173

167174
#feature-details-meta {

app/javascript/maplibre/feature/elevation.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ export async function showElevationChart (feature) {
144144
canvasAbort = new AbortController()
145145
const signal = canvasAbort.signal
146146

147-
// Update info line on hover
148-
chart.canvas.addEventListener('mousemove', (event) => {
147+
// Shared handler for hover/touch interaction
148+
const handleChartInteraction = (event) => {
149149
const points = chart.getElementsAtEventForMode(event, 'index', { intersect: false }, true)
150150
if (points.length === 0) {
151151
infoLine.style.visibility = 'hidden'
@@ -158,20 +158,32 @@ export async function showElevationChart (feature) {
158158
const elevation = active.values[i].toFixed(0)
159159
const grade = i === 0 ? 0 : computeGrade(active.values, active.labels, i)
160160

161-
infoLine.textContent = `Point: ${distance}Elevation: ${elevation}m • Steepness: ${grade.toFixed(1)}%`
161+
infoLine.innerHTML = `Waypoint: ${distance}<i class="bi bi-triangle"></i> ${elevation}m • <i class="bi bi-arrow-up-right"></i> ${grade.toFixed(1)}%`
162162
infoLine.style.visibility = 'visible'
163163

164164
// Place a marker on the map at the hovered point
165165
const coord = active.coords[i]
166166
marker = getMarker(feature)
167167
marker.setLngLat([coord[0], coord[1]])
168168
marker.addTo(map)
169-
}, { signal })
169+
}
170170

171-
chart.canvas.addEventListener('mouseout', () => {
171+
const hideChartInteraction = () => {
172172
infoLine.style.visibility = 'hidden'
173173
if (marker) marker.remove()
174-
}, { signal })
174+
}
175+
176+
// Update info line on hover (desktop)
177+
chart.canvas.addEventListener('mousemove', handleChartInteraction, { signal })
178+
chart.canvas.addEventListener('mouseout', hideChartInteraction, { signal })
179+
180+
// Update info line on touch drag (mobile)
181+
chart.canvas.addEventListener('touchmove', (event) => {
182+
event.preventDefault() // Prevent scrolling while dragging
183+
handleChartInteraction(event)
184+
}, { signal, passive: false })
185+
186+
chart.canvas.addEventListener('touchend', hideChartInteraction, { signal })
175187

176188
// Fly to the clicked point on the map (stopPropagation prevents the click
177189
// from bubbling to the map, which would re-trigger highlightFeature and
@@ -245,7 +257,7 @@ function computeDistances (coords) {
245257
}
246258

247259
// Find the nearest track point to the given GPS position
248-
// Returns index if within 25m threshold, else -1
260+
// Returns index if within 100m threshold, else -1
249261
function findNearestTrackIndex (lngLat, coords) {
250262
let minDist = Infinity
251263
let minIdx = -1
@@ -256,7 +268,7 @@ function findNearestTrackIndex (lngLat, coords) {
256268
minIdx = i
257269
}
258270
}
259-
return minDist <= 25 ? minIdx : -1
271+
return minDist <= 100 ? minIdx : -1
260272
}
261273

262274
// Filter active data to only include points visible in the current map viewport

app/views/maps/modals/_feature.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#feature-details-elevation.feature-section-card.ps-1.hidden
3333
%h6.ms-2.mt-2 Elevation profile
34-
#elevation-info-line.ms-5
34+
#elevation-info-line
3535
%canvas#route-elevation-chart
3636
#elevation-stats.hidden.flex-center.mt-1
3737
%span#elevation-gain.text-nowrap

0 commit comments

Comments
 (0)