There are multiple ways to modify SVG content interactively. Most users prefer to manipulate geometry with the mouse, even when it is not as exact as numerical parameter input.
The implementation uses a single ECMAScript object Grips for managing the grips behavior.
window.grips of the global window object.<g> element in the SVG's <defs> section
and referenced via <use> elements. So it is very easy to change the grip's shape.<g id="gripsContainer"> then receives the grip's mouse events via event bubbeling.
Here is the code for the external file <Grips.js>.
// == Grips object ==============================================
function Grips(container, callback)
{
this.container = container;
this.callback = callback;
this.current = null;
window.grips = this;
}
Grips.Pick = function Grips_Pick(evt)
{
window.grips.current = evt.getTarget();
window.grips.current.setAttribute("class", "pickedGrip");
window.grips.container.addEventListener("mouseup", Grips.HandleEvent, false);
window.grips.container.addEventListener("mousemove", Grips.HandleEvent, false);
Grips.HandleEvent(evt);
}
Grips.HandleEvent = function Grips_HandleEvent(evt)
{
if (window.grips.callback)
window.grips.callback(window.grips.current, evt.getType(), evt.getClientX(), evt.getClientY());
if (evt.getType() == "mouseup") // release grip ..
{
window.grips.current.setAttribute("class", "grip");
window.grips.container.removeEventListener("mouseup", Grips.HandleEvent, false);
window.grips.container.removeEventListener("mousemove", Grips.HandleEvent, false);
window.grips.current = null;
}
}
// === end ======================================================
An application Fourbar (4-Bar Linkage) demonstrates the use of the grips.
lnk1, lnk2, lnk3 that are connected
to each other in the points A, B.Fourbar.Fourbar object implements the GripHandler method, that is handed over to
the Grips object as the callback function.There is still a lot to improve. So use this code as a starting point for your own solution.
© 2000
MecXpert