Một vài năm trước, tôi được yêu cầu tạo một trò chơi dựa trên Ajax 2 người chơi bằng SVG. Nó có thể không chính xác là giải pháp bạn đang tìm kiếm, nhưng nó có thể giúp bạn lắng nghe các sự kiện trong SVG của bạn. Dưới đây là bộ điều khiển SVG:
FYI, các SVG đã được kéo và thả (đó là Stratego)
/****************** Track and handle SVG object movement *************/
var svgDoc;
var svgRoot;
var mover=''; //keeps track of what I'm dragging
///start function////
//do this onload
function start(evt){
//set up the svg document elements
svgDoc=evt.target.ownerDocument;
svgRoot=svgDoc.documentElement;
//add the mousemove event to the whole thing
svgRoot.addEventListener('mousemove',go,false);
//do this when the mouse is released
svgRoot.addEventListener('mouseup',releaseMouse,false);
}
// set the id of the target to drag
function setMove(id){ mover=id; }
// clear the id of the dragging object
function releaseMouse(){
if(allowMoves == true){ sendMove(mover); }
mover='';
}
// this is launched every mousemove on the doc
// if we are dragging something, move it
function go(evt){
if(mover != '' && allowMoves != false) {
//init it
var me=document.getElementById(mover);
//actually change the location
moveX = evt.clientX-135; //css positioning minus 1/2 the width of the piece
moveY = evt.clientY-65;
me.setAttributeNS(null, 'x', evt.clientX-135);
me.setAttributeNS(null, 'y', evt.clientY-65);
}
}
function moveThis(pieceID, x, y) {
$(pieceID).setAttributeNS(null, 'x', x);
$(pieceID).setAttributeNS(null, 'y', y);
}
Ứng dụng của tôi là tinh khiết SVG + JavaScript, nhưng đây là ý chính của nó.
Nguồn
2008-09-27 19:01:23