I'm experimenting with dynamically drawing things on an SVG element based on the position of the mouse. Unfortunately, I'm having difficulty translating the mouse coordinates from the mousemove event into the coordinate space of the SVG element.
Here is a buggy function I've been testing:
CylinderDemo.prototype.handleMouseMove = function(evt)
{
debugEvent = evt;
var bcr = evt.target.getBoundingClientRect();
var x2 = evt.clientX - bcr.left;
var y2 = evt.clientY - bcr.top;
console.log(evt.target);
//console.log(evt.clientY+" - "+evt.target.getBBox());
var d = this.pathForCylinder(this.x0, this.y0, x2, y2, 30);
this.cap.setAttributeNS(null, "d", d);
}
canvas.onmousemove = function(evt) {
self.handleMouseMove(evt);
}
The problem is that (at least in Firefox) getBoundingClientRect() does not give me the bounds of the SVG object. It gives me the bounds of the drawable objects inside the SVG object. It becomes painfully obvious when you mouse over the log lines in firebug and it highlights the paltry subrectangle of drawable elements. That means that my transformation of the coordinates gives defective results.
What technique should I use to transform from the event coordinate system into the coordinate system of the SVG container?
I just cobbled together http://jsfiddle.net/7kvkq/ to illustrate the problem.
setAttribute('transform',theRightString)or by creating/modifying a translation designed just for the dragging by using the SVG DOM. - Phrogz