Say I have a single SVG
element that serves as the background. Using the following working code to get the x & y
coordinates of the mouse on the move and the viewport width and height, I want to dynamically compute for the coordinates of a certain portion of the SVG element given any viewport width and height since I want to assign an onclick()
to that portion.
var eventDoc, doc, bodyDoc;
e = e || window.e;
var x = e.originalEvent.touches ? e.originalEvent.touches[0].pageY : e.pageY;
var y = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX;
if (x == null && event.clientX != null) {
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
bodyDoc = eventDoc.bodyDoc;
x = e.clientX +
(doc && doc.scrollLeft || bodyDoc && bodyDoc.scrollLeft || 0) -
(doc && doc.clientLeft || bodyDoc && bobodyDocdy.clientLeft || 0);
y = e.clientY +
(doc && doc.scrollTop || bodyDoc && bodyDoc.scrollTop || 0) -
(doc && doc.clientTop || bodyDoc && bodyDoc.clientTop || 0);
}
var vw = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var vh = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
A hardcoded solution I've thought of would be to have a lot of if statements for each different device vw/vh
and different desktop vw/vh's
, but I don't think that's very sustainable. Any suggestions?