如何在画布中移动图像?

时间:2015-09-29 07:45:55

标签: javascript html5 canvas html5-canvas

帮助在画布上移动元素。如何在画布上添加图像并通过按键盘上的按钮使其移动

document.addEventListener('keydown', function(event) {
    if(event.keyCode == 37)
    {
     launcher.dir = 'left';
    }
}, false);
document.addEventListener('keyup', function(event) {
    if(event.keyCode == 37)
    {
     launcher.dir = '';
    }
}, false); 
document.addEventListener('keydown', function(event) {
    if(event.keyCode == 39)
    {
     launcher.dir = 'right';
    }
}, false);
document.addEventListener('keyup', function(event) {
    if(event.keyCode == 39)
    {
     launcher.dir = '';
    }
}, false); 
left_btn.addEventListener('mouseup', function(event) {
    launcher.dir = '';
});
right_btn.addEventListener('mousedown', function(event) {
    launcher.dir = 'right';
});

1 个答案:

答案 0 :(得分:0)

您可以通过两种方式在画布上移动“形状”: