Based off this HTML5/JS game tutorial, I'm trying to change the "player" from just a simple color to an image.
How do I add an image to a canvas element?
The code that I'm trying to edit is this part right here:
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);
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);
See more options here: https://www.w3schools.com/tags/canvas_drawimage.asp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.