0
我試圖讓一個非常簡單的動畫工作,其中一個圖像加載到Javascript Canvas只是向右滑動。下面的代碼與繪製矩形一起工作,但是當我嘗試插入圖像時沒有任何加載,但我沒有收到任何錯誤消息。我需要讓它只用Javascript工作。在javascript畫布上移動圖像
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000/60);
};
})();
var canvas = document.getElementById("canvas"),
cx = canvas.getContext("2d");
function Card(x,y){
this.x = x || 0;
this.y = y || 0;
this.draw = function(){
var img = new Image();
img.onload = function()
{
cx.drawImage(img, x, y);
}
img.src = "images/back.jpg";
}
}
var myCard = new Card(50,50);
function loop(){
cx.clearRect(0, 0, canvas.width, canvas.height);
myCard.x++;
myCard.draw();
requestAnimFrame(loop);
}
loop();