ctx.drawImage not working

ctx.drawImage not working

本文关键字:working not drawImage ctx      更新时间:2023-09-26

我正在制作一个游戏。下面应该给我画一张c.png(硬币)的照片,但它不起作用。

以下链接中有一个示例:http://jonirautiainen.net/html5peli/testi.html

<div id="game"></div><script>
var canvasWidth = window.innerWidth - 20;
var canvasHeight = window.innerHeight - 100;
var game = document.getElementById("game");
game.innerHTML = '<canvas id="canvas" width="' + canvasWidth + '"height="' + canvasHeight + '"style="border:solid black 1px"></canvas>';
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var cimg = new Image();
//coin
cimg.src = "c.png";
var time = 0;
var delay = 0;
var coin = {
        srcX: 0,
        srcY: 0,
        w: 90,
        h: 89,
        dw: 40,
        dw: 40,
        x: Math.floor(Math.random() * canvasWidth),
        y: Math.floor(Math.random() * canvasHeight) 
}
ctx.drawImage(cimg, coin.srcX, coin.srcY, coin.w, coin.h, coin.x, coin.y, coin.dw, coin.dh);

</script>

有什么想法吗?它没有给我任何错误。

谢谢。

您已经在此处键入了两次dw

var coin = {
        srcX: 0,
        srcY: 0,
        w: 90,
        h: 89,
        dw: 40,
        dw: 40,
        x: Math.floor(Math.random() * canvasWidth),
        y: Math.floor(Math.random() * canvasHeight) 
};

第二个应该是dh: 40。=)