帮助在画布上移动元素。如何在画布上添加图像并通过按键盘上的按钮使其移动
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';
});
答案 0 :(得分:0)
您可以通过两种方式在画布上移动“形状”:
translation
。 (参见http://www.html5canvastutorials.com/advanced/html5-canvas-transform-translate-tutorial/)要真正找不到如何使用它,阅读有关状态堆栈http://www.html5canvastutorials.com/advanced/html5-canvas-transformation-state-stack-tutorial/