HTML5:将图像添加为画布元素

时间:2017-12-23 22:23:32

标签: javascript jquery css html5 2d-games

基于this HTML5 / JS游戏教程,我试图更改"播放器"从简单的颜色到图像。

如何将图像添加到画布元素?

我尝试编辑的代码就是这一部分:



player.x += player.velX;
player.y += player.velY;

ctx.fill();
ctx.fillStyle = "green";
ctx.fillRect(player.x, player.y, player.width, player.height);

requestAnimationFrame(update);




1 个答案:

答案 0 :(得分:0)

player.x += player.velX;
player.y += player.velY;

var img = new Image();
img.src = "linkToYourImage.jpg";
//Can be any extension (png, etc.)

ctx.drawImage(img, player.x, player.y, player.width, player.height);

requestAnimationFrame(update);

在此处查看更多选项:https://www.w3schools.com/tags/canvas_drawimage.asp