diff --git a/lib/utils.js b/lib/utils.js index 5e9c0ee..a52ed92 100755 --- a/lib/utils.js +++ b/lib/utils.js @@ -411,20 +411,96 @@ export class Points { } static fromPageToNode(element, p) { - if (window.webkitConvertPointFromPageToNode) { - return window.webkitConvertPointFromPageToNode(element, new WebKitPoint(p.x, p.y)) - } - //return window.convertPointFromPageToNode(element, p.x, p.y) + // if (window.webkitConvertPointFromPageToNode) { + // return window.webkitConvertPointFromPageToNode(element, new WebKitPoint(p.x, p.y)) + // } + return convertPointFromPageToNode(element, p.x, p.y) } static fromNodeToPage(element, p) { - if (window.webkitConvertPointFromNodeToPage) { - return window.webkitConvertPointFromNodeToPage(element, new WebKitPoint(p.x, p.y)) - } - //return window.convertPointFromNodeToPage(element, p.x, p.y) + // if (window.webkitConvertPointFromNodeToPage) { + // return window.webkitConvertPointFromNodeToPage(element, new WebKitPoint(p.x, p.y)) + // } + return convertPointFromNodeToPage(element, p.x, p.y) } } + + + + +var I = typeof WebKitCSSMatrix == 'undefined' ? new DOMMatrix() : new WebKitCSSMatrix() + +function Point(x, y, z) { + this.x = x + this.y = y + this.z = z +} + +Point.prototype.transformBy = function (matrix) { + var tmp = matrix.multiply(I.translate(this.x, this.y, this.z)) + return new Point(tmp.m41, tmp.m42, tmp.m43) +} + +function createMatrix(transform) { + try { + return typeof WebKitCSSMatrix == 'undefined' ? new DOMMatrix(transform) : new WebKitCSSMatrix(transform) + } catch (e) { + console.warn(transform) + console.warn(e.toString()) + return I + } +} + +function getTransformationMatrix(element) { + var transformationMatrix = I + var x = element + + while (x != undefined && x !== x.ownerDocument.documentElement) { + var computedStyle = window.getComputedStyle(x, undefined) + var transform = computedStyle.transform || 'none' + var c = transform === 'none' ? I : createMatrix(transform) + transformationMatrix = c.multiply(transformationMatrix) + x = x.parentNode + } + + var w = element.offsetWidth + var h = element.offsetHeight + var i = 4 + var left = +Infinity + var top = +Infinity + while (--i >= 0) { + var p = new Point(i === 0 || i === 1 ? 0 : w, i === 0 || i === 3 ? 0 : h, 0).transformBy( + transformationMatrix + ) + if (p.x < left) { + left = p.x + } + if (p.y < top) { + top = p.y + } + } + var rect = element.getBoundingClientRect() + transformationMatrix = I.translate( + window.pageXOffset + rect.left - left, + window.pageYOffset + rect.top - top, + 0 + ).multiply(transformationMatrix) + return transformationMatrix +} + +var convertPointFromPageToNode = function (element, pageX, pageY) { + return new Point(pageX, pageY, 0).transformBy(getTransformationMatrix(element).inverse()) +} + +var convertPointFromNodeToPage = function (element, offsetX, offsetY) { + return new Point(offsetX, offsetY, 0).transformBy(getTransformationMatrix(element)) +} + + + + + /** * A helper class for common set operations. *