Skip to content

Instantly share code, notes, and snippets.

@jacob414
Last active August 29, 2015 14:01
Show Gist options
  • Save jacob414/5602dd38fc53c94ef7e1 to your computer and use it in GitHub Desktop.
Save jacob414/5602dd38fc53c94ef7e1 to your computer and use it in GitHub Desktop.
Interpolation of element closest to point by sampling in expanding circles
@closest = (x, y) ->
el = document.elementFromPoint x, y
unless el is null or el.nodeName is 'BODY' then el
else
slice = 2 * Math.PI / 8
for radius in [5...200] by 8
for step in [0...7]
angle = slice * step
[candX, candY] = [x + radius*Math.cos(angle), y + radius*Math.sin(angle)]
cand = document.elementFromPoint candX, candY
if cand != null and cand.nodeName != 'BODY' then return cand
throw "Failed to find element close to (#{x}, #{y})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment