Last active
August 29, 2015 14:01
-
-
Save jacob414/5602dd38fc53c94ef7e1 to your computer and use it in GitHub Desktop.
Interpolation of element closest to point by sampling in expanding circles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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