SVGElement Drag & Drop

<html>
<script>
var dx, dy;
var x, y;
var actived = null;
function mouseDownListener(e){
    if(actived.setCapture){
        actived.setCapture();
    }
    else{
        window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
    }
    var canvas = document.getElementById('objCanvas');
    canvas.addEventListener("mousemove", mouseMoveListener, false);
    canvas.addEventListener("mouseup", mouseUpListener, false);
    x = e.offsetX, y = e.offsetY;
    dx = x-parseInt(actived.getAttribute('cx'), 10);
    dy = y-parseInt(actived.getAttribute('cy'), 10);
    console.log('('+x+','+y+'), ('+dx+','+dy+')');
    //console.log('mouse down | X,Y: ('+e.x+', '+e.y+') | client X,Y: (' + e.clientX + ', ' + e.clientY + ') offset X,Y: (' + e.offsetX + ', ' + e.offsetY + ')');
    //e.cancelBubble = true;
}

function mouseMoveListener(e){
    //actived.setAttribute("transform", "translate("+dx+", "+dy+")");
    actived.setAttribute("cx", e.offsetX-dx);
    actived.setAttribute("cy", e.offsetY-dy);
    //console.log('mouse moving: ('+e.clientX+', '+e.clientY+')('+e.x+', '+dy+')');
}

function mouseUpListener(e){
    dragging = false;
    console.log('mouse up');
    if(actived.releaseCapture){
        actived.releaseCapture();
    }
    else{
        window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
    }
    //actived.setAttribute('cx', x);
    //actived.setAttribute('cy', y);
    //actived.setAttribute('transform', 'translate(0, 0)');


    var canvas = document.getElementById('objCanvas');
    canvas.removeEventListener('mousemove', mouseMoveListener);
    canvas.removeEventListener('mouseup', mouseUpListener);
    //canvas.onmousemove  = null;
    //canvas.onmouseup = null;

}

document.onreadystatechange = function(e){
    if(document.readyState=='complete'){
        actived = document.getElementById('objCircle');
        //actived.onmousedown = mouseDownListener;
        actived.addEventListener("mousedown", mouseDownListener, false);
    }
}
</script>
<body>
<svg id="objCanvas" width="640" height="480" style="background-color:#eee" version="1.1"
xmlns="http://www.w3.org/2000/svg">

<circle id="objCircle" cx="100" cy="100" r="5" stroke="green"
stroke-width="2" fill="white"/>

</svg>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值