我的代碼:移動矩形畫布內
$(document).ready(function() {
var canvas,
ctx,
playerimage,
x,
y,
speed = 5, //speed for the player to move at
width = 10, //width of the player
height = 10; //height of the player
function init() {
canvas = $("#Mycanvas");
ctx = canvas[0].getContext('2d');
x = canvas.width()/2;
y = canvas.height()/2;
playerimage = new Image();
playerimage.src = "ninja.png"; //path to the image to use for the player
window.addEventListener("keydown", update, false);
//canvas.addEventListener("keypress", update);
render();
}
$(window).load(function() { init(); });
function update(event) {
if (event.keyCode == 38) {
y -= speed; //going up
}
if (event.keyCode == 40) {
y += speed; //going down
}
if (event.keyCode == 37) {
x -= speed; //going left
}
if (event.keyCode == 39) {
x += speed; //going right
}
render();
}
function render() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillRect(x, y, width, height);
}
});
我的jsfiddle:http://jsfiddle.net/BLpGH/14/
,你可以看到矩形被代替移動好像繪製移動我不的線代替我不想畫出一條線,我該怎麼做?
非常感謝你 – Sora
@索拉,不客氣! – Sergio
有無論如何修改我的代碼,所以當矩形命中可以用畫布邊框動畫? – Sora