diff options
| author | Jules Laplace <jules@okfoc.us> | 2016-10-28 18:06:46 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2016-10-28 18:06:46 -0400 |
| commit | 9e7bacd46c1e5d0e1c24433690d421ab3f3a11f2 (patch) | |
| tree | 4d0cefa2780dfa4382f1ed2ea481b6aafbdbb15e /public/assets/javascripts/rectangles/util/coords.js | |
| parent | 50da9e3e677f121f15e501bf062da6c45db255ad (diff) | |
| parent | cce1dea756717f1308c6b72f762b5ea5f5b43958 (diff) | |
merge
Diffstat (limited to 'public/assets/javascripts/rectangles/util/coords.js')
| -rw-r--r-- | public/assets/javascripts/rectangles/util/coords.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/public/assets/javascripts/rectangles/util/coords.js b/public/assets/javascripts/rectangles/util/coords.js new file mode 100644 index 0000000..ff56199 --- /dev/null +++ b/public/assets/javascripts/rectangles/util/coords.js @@ -0,0 +1,33 @@ +function offsetFromPoint(event, element) { + function a(width) { + var l = 0, r = 200; + while (r - l > 0.0001) { + var mid = (r + l) / 2; + var a = document.createElement('div'); + a.style.cssText = 'position: absolute;left:0;top:0;background: red;z-index: 1000;'; + a.style[width ? 'width' : 'height'] = mid.toFixed(3) + '%'; + a.style[width ? 'height' : 'width'] = '100%'; + element.appendChild(a); + var x = document.elementFromPoint(event.clientX, event.clientY); + element.removeChild(a); + if (x === a) { + r = mid; + } else { + if (r === 200) { + return null; + } + l = mid; + } + } + return mid; + } + var l = a(1), + t = a(0); + return l && t ? { + left: l / 100, + top: t / 100, + toString: function () { + return 'left: ' + l + '%, top: ' + t + '%'; + } + } : null; +}
\ No newline at end of file |
