Skip to content

Commit

Permalink
Split off distanceSquared function
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Dec 15, 2024
1 parent c9ddb0f commit 8201059
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
4 changes: 0 additions & 4 deletions app/javascript/src/legacy/common.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ Object.extend Element.Methods,
$(element).hide()
Element.addMethods()

### Return the squared distance between two points. ###
window.distance_squared = (x1, y1, x2, y2) ->
(x1 - x2) ** 2 + (y1 - y2) ** 2

Prototype.Browser.AndroidWebKit = navigator.userAgent.indexOf('Android') != -1 and navigator.userAgent.indexOf('WebKit') != -1

# Some UI simply doesn't make sense on a touchscreen, and may need to be disabled or changed.
Expand Down
17 changes: 7 additions & 10 deletions app/javascript/src/legacy/touchscreen-web-app-helpers.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { distanceSquared } from 'src/utils/math'
###
# This file implements several helpers for fixing up full-page web apps on touchscreen
# browsers:
Expand Down Expand Up @@ -81,9 +82,8 @@ EmulateDoubleClick::touchstart_event = (event) ->
if time_since_previous > 500
return

### Check that the clicks aren't too far apart. ###

distance = (this_touch.screenX - (last_click.position[0])) ** 2 + (this_touch.screenY - (last_click.position[1])) ** 2
# Check that the clicks aren't too far apart.
distance = distanceSquared(this_touch.screenX, this_touch.screenY, last_click.position[0], last_click.position[1])
if distance > 500
return
if event.target != last_click.target
Expand All @@ -110,11 +110,9 @@ EmulateDoubleClick::touchend_event = (event) ->
this_click = event.changedTouches[0]
if this_click.identifier == last_click_identifier

### If the touch moved too far when it was removed, don't fire a doubleclick; for
# If the touch moved too far when it was removed, don't fire a doubleclick; for
# example, two quick swipe gestures aren't a double-click.
###

distance = (this_click.screenX - (last_click_position[0])) ** 2 + (this_click.screenY - (last_click_position[1])) ** 2
distance = distanceSquared(this_click.screenX, this_click.screenY, last_click_position[0], last_click_position[1])
if distance > 500
@last_click = null
return
Expand Down Expand Up @@ -178,9 +176,8 @@ ResponsiveSingleClick::touchend_event = (event) ->
touch.screenY
]

### Don't trigger a click if the point has moved too far. ###

distance = distance_squared(this_touch[0], this_touch[1], last_touch[0], last_touch[1])
# Don't trigger a click if the point has moved too far.
distance = distanceSquared(this_touch[0], this_touch[1], last_touch[0], last_touch[1])
if distance > 50
return
e = document.createEvent('MouseEvent')
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/src/utils/math.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export clamp = (n, min, max) ->
Math.max Math.min(n, max), min

# Return the squared distance between two points.
export distanceSquared = (x1, y1, x2, y2) ->
(x1 - x2) ** 2 + (y1 - y2) ** 2

export numberToHumanSize = (size, precision) ->
precision ?= 1
text = undefined
Expand Down

0 comments on commit 8201059

Please sign in to comment.