我只是不能得到这个为我工作。使用drawImage()绘制多个图像在画布上的问题
我试图使用drawImage()绘制多个图像到画布。我觉得我大大忽视了一些小事。
它应该画18张牌。它将从左侧开始50px,从顶部开始下降,并绘制每张牌100w * 150h。每张卡片图像之间应该有25px。画布尺寸设置为825w * 600h。
试图用简单的Javascript(没有jQuery)完成此操作。任何帮助表示赞赏。
// Draw the cards to the canvas.
function drawCards()
{
// Starting positions.
var x = 50;
var y = 50;
// Counter that will hold current card position.
var cardCount = 0;
var img = new Image(100, 150);
// How many rows.
for (var row = 0; row < 3; row++)
{
// How many columns.
for (var column = 0; column < 6; column++)
{
// Store the current card.
var card = memoryDeck[cardCount];
// Check if the card is flipped, if it is set an image with url to face card.
if (card.flipped == true)
{
img.onload = function() {
ctx.drawImage(this, x, y, 100, 150);
}
img.src = card.faceImage;
}
// Otherwise set image url to back of card.
else
{
img.onload = function() {
ctx.drawImage(this, x, y, 100, 150);
}
img.src = card.backImage;
}
// Increase the x position (the width of a card, plus the space in between), and the current card position being stored.
x += 125;
cardCount++;
}
// We are on a new row, reset the column card position and increase the row card position.
x = 50;
y += 175;
}
}
这解决了我的一半问题。谢谢!我仍然需要弄清楚它为什么只画出最后一张图像,但是......:/ –
看起来我没有完全应用这个。使用这个,做了些微的调整,并设法修复整个事情。非常感谢! –