Skip to content

Instantly share code, notes, and snippets.

@nuxodin
Last active November 20, 2017 05:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuxodin/6d937dddf7e0deacfef484feca11a147 to your computer and use it in GitHub Desktop.
Save nuxodin/6d937dddf7e0deacfef484feca11a147 to your computer and use it in GitHub Desktop.
Browsers have "elementFromPoint", here is "nodeFromPoint" (including text-nodes) !
/* Copyright (c) 2016 Tobias Buschor https://goo.gl/gl0mbf | MIT License https://goo.gl/HgajeK */
document.nodeFromPoint = function(x, y) {
const el = document.elementFromPoint(x, y);
const nodes = el.childNodes;
for (let i = 0, node; node = nodes[i++];) {
if (node.nodeType === 3) {
const range = document.createRange();
range.selectNode(node);
const rects = range.getClientRects();
for (let j = 0, rect; rect = rects[j++];) {
if (x > rect.left && x < rect.right && y > rect.top && y < rect.bottom)
return node;
}
}
}
return el;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment