我使用 KineticJS 中的 transitionTo 制作了一个形状,可以在箭头键(keydown 事件)的帮助下移动。
我有两个问题:
1. 按键后形状的移动有一个短暂的延迟。如何消除延迟?
2.如何使形状在同时按下两个箭头键的情况下沿对角线移动?这是javascript:
var stage = new Kinetic.Stage({
container: 'container',
width: screen.width,
height: 500
});
var layer = new Kinetic.Layer();
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'yellow',
stroke: 'black',
strokeWidth: 4
});
// add the shape to the layer
layer.add(circle);
// add the layer to the stage
stage.add(layer);
window.addEventListener('keydown', function(e) {
if (e.keyCode == 37) { //Left Arrow Key
setTimeout(function () {
circle.transitionTo({
x: circle.getX() - 10,
y: circle.getY(),
duration: 0.01
})
}, 0);
}
if (e.keyCode == 38) { //Up Arrow Key
setTimeout(function () {
circle.transitionTo({
x: circle.getX(),
y: circle.getY() - 10,
duration: 0.01
})
}, 0);
}
if (e.keyCode == 39) { //Right Arrow Key
setTimeout(function () {
circle.transitionTo({
x: circle.getX() + 10,
y: circle.getY(),
duration: 0.01
})
}, 0);
}
if (e.keyCode == 40) { //Top Arrow Key
setTimeout(function () {
circle.transitionTo({
x: circle.getX(),
y: circle.getY() + 10,
duration: 0.01
})
}, 0);
}
});
小提琴:http: //jsfiddle.net/uUWmC/1/