gpt4 book ai didi

javascript - 为什么javascript不在 Canvas 上绘制图像?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:50:11 25 4
gpt4 key购买 nike

我在将图像绘制到 Canvas 时遇到问题。没有错误,但仍然没有绘制图像。请检查代码块,看看您是否能看到我看不到的内容:

function loadPacman(posX, posY) {
this.posX = posX;
this.posY = posY;
pacman = new Image();
pacman.src="http://www.frogbug.se/pacman/img/naziman.png";
pacman.addEventListener("load", function(){
canvas.drawImage(pacman, this.posX, this.posY)
}, false);
}

function loadGhost(posX, posY) {
this.posX = posX;
this.posY = posY;
ghost = new Image();
ghost.src="http://www.frogbug.se/pacman/img/ghost.png";
ghost.addEventListener("load", function(){
canvas.drawImage(ghost, this.posX, this.posY)
}, false);
}

这是我在页面加载时加载的函数:

function onLoad() {
var xy = document.getElementById('canvas');
canvas = xy.getContext('2d');
//calculate the x and y for canvas
x = xy.width;
y = xy.height;

//divide the width and length of the canvas to get small cubes
//which is 40x40
xtile = Math.floor(x/40);
ytile = Math.floor(y/40);

//draw lines around the canvas
canvas.strokeRect(0, 0, x, y);

//the array for the map
var map = getMapArray();

//draw the map
drawMap(map);
//fillCanvas("0010", 0, 40, 40, 40);

//load the ghost and pacman
loadPacman(0, 0);
loadGhost((xtile*40)-40, (ytile*40)-40);
}

提前致谢!您可以在此处查看完整的源代码等: http://www.picturds.com/pacman_serious/

最佳答案

从加载回调中的 this.posXthis.posY 中删除 thisthis 在该上下文中是图像元素,与分配给 this.posX 时的 this 不同(即 window)。

http://jsfiddle.net/5dkx4/

关于javascript - 为什么javascript不在 Canvas 上绘制图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8830297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com