javascript - Move a HTML5 Canvas Element - Stack Overflow

I'm making a small jigsaw like puzzle in html5. Each jigsaw piece is it's own canvas. I need

I'm making a small jigsaw like puzzle in html5. Each jigsaw piece is it's own canvas. I need to move the canvas element using the mouse position. I've managed to get the canvas that is clicked, I just need to move it. I tried manipulating the top and left style attributes but the canvas didn't move. Can this be done or am I trying something impossible.

Thanks!

function MouseDown(can, e)
{
    MovingCanvas = can;
    clicked = true;
}

function MouseMove(e)
{
    if(clicked)
    {
        var mx = e.clientX;
        var my = e.clientY;

        MovingCanvas.style.top = my;
        MovingCanvas.style.left = mx;
    }
}

I'm making a small jigsaw like puzzle in html5. Each jigsaw piece is it's own canvas. I need to move the canvas element using the mouse position. I've managed to get the canvas that is clicked, I just need to move it. I tried manipulating the top and left style attributes but the canvas didn't move. Can this be done or am I trying something impossible.

Thanks!

function MouseDown(can, e)
{
    MovingCanvas = can;
    clicked = true;
}

function MouseMove(e)
{
    if(clicked)
    {
        var mx = e.clientX;
        var my = e.clientY;

        MovingCanvas.style.top = my;
        MovingCanvas.style.left = mx;
    }
}
Share Improve this question edited Sep 23, 2013 at 9:44 Vinay 6,8774 gold badges34 silver badges51 bronze badges asked Sep 23, 2013 at 9:42 JamesTJamesT 472 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

e.clientX and e.clientY are integers.

Styles expect a string of the form {NUMBER}{UNIT}.

You are missing a unit, therefore it won't work.

MovingCanvas.style.top = my+"px";
MovingCanvas.style.left = mx+"px";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745420681a4626952.html

相关推荐

  • javascript - Move a HTML5 Canvas Element - Stack Overflow

    I'm making a small jigsaw like puzzle in html5. Each jigsaw piece is it's own canvas. I need

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信