按住键的同时移动画布元素,然后在松开时停止



http://jsfiddle.net/genome314/4Hm8X/

代码问题:如果按住箭头键并查看控制台,则每次按下键后都会启动keyup。此外,按住箭头键后,对象将停止移动。

为什么在这种情况下会出现问题:我希望每次按下箭头键时都移动正方形,然后在按下键时停止。

所需效果:我想按住一个键来移动盒子,然后在松开键时让它停止移动。

  • 这是可以实现的吗?如果是,大致如何?如果没有,是否有其他注册方向的方法

为了记录在案,我不想使用Jquery。感谢您的任何提示或建议。

Javascript

keyLeft = 0;
keyRight = 0;
keyNorth = 0;
keyBot = 0;
var interval;
var c = 0;
window.onload=function(){
    addButton.addEventListener
        ('click',function(){addRowCell("table1")},false);
    removeButton.addEventListener
        ('click',function(){deleteRowCell("table1")},false);
    stopButton.addEventListener('click',stop,false);
    startButton.addEventListener('click',initiate,false);
}
//38 up
//37 left
//40 down
//39 right
document.onkeydown = function (e) {
    console.debug(e.keyCode, "down");
    console.debug(e.keyCode, "up");
    switch (e.keyCode){
        case 37:
            initiateLeft();
            break;
        case 38:
            initiateNorth();
            break;
        case 40:
            initiateBot();
            break;
        case 39:
            initiateRight();
            break;
        case 32:
            stop();
            break;
    }
}
function stop(){
    clearInterval(interval);
}
function initiateRight(){
    clearInterval(interval);
    interval = setInterval(right,100);
}
function initiateLeft(){
    clearInterval(interval);
    interval = setInterval(left,100);
}
function initiateNorth(){
    clearInterval(interval);
    c=0;
    interval = setInterval(north,100);
}
function initiateBot(){
    clearInterval(interval);
    interval = setInterval(bot,100);
}
function right(){
    var x = document.getElementById("counter");
    x.value = keyRight;
    var d = document.getElementById("canv1");
    var ctx = d.getContext("2d");
    var ctx1 = document.querySelector('#canv1').getContext('2d');
    ctx1.clearRect(0,0,100,100);    
    ctx.strokeRect(20+keyRight-keyLeft,20-keyNorth+keyBot,40,40);
    keyRight++;
}
function left(){
    var x = document.getElementById("counter");
    x.value = keyLeft;
    var d = document.getElementById("canv1");
    var ctx = d.getContext("2d");
    var ctx1 = document.querySelector('#canv1').getContext('2d');
    ctx1.clearRect(0,0,100,100);
    ctx.strokeRect(20+keyRight-keyLeft,20-keyNorth+keyBot,40,40);
    keyLeft++;
}
function north(){
    var x = document.getElementById("counter");
    x.value = keyNorth;
    var d = document.getElementById("canv1");
    var ctx = d.getContext("2d");
    var ctx1 = document.querySelector('#canv1').getContext('2d');
    ctx1.clearRect(0,0,100,100);
    keyNorth+=(10-c);
    c+=2;
    ctx.strokeRect(20+keyRight-keyLeft,20-keyNorth+keyBot,40,40);
    if(20-keyNorth == 20)
    clearInterval(interval);
}
function bot(){
    var x = document.getElementById("counter");
    x.value = keyBot;
    var d = document.getElementById("canv1");
    var ctx = d.getContext("2d");
    var ctx1 = document.querySelector('#canv1').getContext('2d');
    ctx1.clearRect(0,0,100,100);
    ctx.strokeRect(20+keyRight-keyLeft,20-keyNorth+keyBot,40,40);
    keyBot++;
}

HTML

<canvas id="canv1" width="300px" height="100px"></canvas>
<input id="counter" style="width:40px;"></input>
<div style="clear:left;">
Press spacebar to pause.<br>
我希望我没有在做你的家庭作业。如果是这样,你应该感到内疚。
keyLeft = 0;
keyRight = 0;
keyNorth = 0;
keyBot = 0;
var interval;
var c = 0;

//38 up
//37 left
//40 down
//39 right
document.onkeydown = function (e) {
    console.debug(e.keyCode, "down");
    console.debug(e.keyCode, "up");
    switch (e.keyCode){
        case 37:
            initiateLeft();
            break;
        case 38:
            initiateNorth();
            break;
        case 40:
            initiateBot();
            break;
        case 39:
            initiateRight();
            break;
        case 32:
            stop();
            break;
    }
}
function stop(){
}
function initiateRight(){
    interval = setTimeout(right,100);
}
function initiateLeft(){
    interval = setTimeout(left,100);
}
function initiateNorth(){
    c=0;
    interval = setTimeout(north,100);
}
function initiateBot(){
    interval = setTimeout(bot,100);
}
function right(){
    var x = document.getElementById("counter");
    x.value = keyRight;
    var d = document.getElementById("canv1");
    var ctx = d.getContext("2d");
    var ctx1 = document.querySelector('#canv1').getContext('2d');
    ctx1.clearRect(0,0,100,100);    
    ctx.strokeRect(20+keyRight-keyLeft,20-keyNorth+keyBot,40,40);
    keyRight++;
}
function left(){
    var x = document.getElementById("counter");
    x.value = keyLeft;
    var d = document.getElementById("canv1");
    var ctx = d.getContext("2d");
    var ctx1 = document.querySelector('#canv1').getContext('2d');
    ctx1.clearRect(0,0,100,100);
    ctx.strokeRect(20+keyRight-keyLeft,20-keyNorth+keyBot,40,40);
    keyLeft++;
}
function north(){
    var x = document.getElementById("counter");
    x.value = keyNorth;
    var d = document.getElementById("canv1");
    var ctx = d.getContext("2d");
    var ctx1 = document.querySelector('#canv1').getContext('2d');
    ctx1.clearRect(0,0,100,100);
    keyNorth+=(10-c);
    c+=2;
    ctx.strokeRect(20+keyRight-keyLeft,20-keyNorth+keyBot,40,40);
}
function bot(){
    var x = document.getElementById("counter");
    x.value = keyBot;
    var d = document.getElementById("canv1");
    var ctx = d.getContext("2d");
    var ctx1 = document.querySelector('#canv1').getContext('2d');
    ctx1.clearRect(0,0,100,100);
    ctx.strokeRect(20+keyRight-keyLeft,20-keyNorth+keyBot,40,40);
    keyBot++;
}

相关内容

  • 没有找到相关文章

最新更新