为什么 javascript 不在画布上绘制图像?

发布于 2024-12-26 05:09:55 字数 1497 浏览 0 评论 0原文

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

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/

I have a problem with drawing images to my canvas. There is no error but still there is no images drawn. Please review the chunk of code and see if you can see what I cannot:

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);
}

And this is my function that loads when the page loads:

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);
}

Thanks in advance!
You can view the full source and so on here:
http://www.picturds.com/pacman_serious/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2025-01-02 05:09:56

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

http://jsfiddle.net/5dkx4/

Remove this from this.posX and this.posY in the load callback. this in that context is the image element, not the same this as when you assign to this.posX (which is window).

http://jsfiddle.net/5dkx4/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文