是否可以在 HTML5 画布中使用多个图像?

发布于 2024-11-01 10:57:59 字数 536 浏览 1 评论 0原文

我正在尝试将 10 张不同的图像加载到画布中。我的计划是最终将这些图像制作成动画,但现在它们似乎正在互相覆盖。这是我的代码:

var DrawLetters = function()
{
    for (i = 0; i < howManyLetters; i++)
    {
        thisWidth = letters[i][0];
        thisHeight = letters[i][1];
        imgSrc = letters[i][2];
        letterImg = new Image();
        letterImg.onload = function()
        {
            context.drawImage(letterImg,thisWidth,thisHeight);
        }
        letterImg.src = imgSrc;
    }
};

letters 是一个包含 10 个元素的数组,其中每个元素都包含图像的路径。对此的任何帮助将不胜感激。

I'm trying to load 10 different images into a canvas. My plan is to eventually animate these images but right now they seem to be overwriting one another. Here is my code:

var DrawLetters = function()
{
    for (i = 0; i < howManyLetters; i++)
    {
        thisWidth = letters[i][0];
        thisHeight = letters[i][1];
        imgSrc = letters[i][2];
        letterImg = new Image();
        letterImg.onload = function()
        {
            context.drawImage(letterImg,thisWidth,thisHeight);
        }
        letterImg.src = imgSrc;
    }
};

letters is an array with 10 elements where each element contains a path to the image. Any help on this would be greatly appreciated.

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

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

发布评论

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

评论(3

深居我梦 2024-11-08 10:57:59

我已经尝试过您的代码,并且 onload 方法始终使用变量的最后一个值,而不是迭代数组时的值。

尝试将 X 和 Y 设置为图像对象的属性:

// I assume you are storing the coordinates where the letters must be
letterImg.setAtX = letter[i][XPOS];
letterImg.setAtY = letter[i][YPOS];

在 onload 上:

context.drawImage(this, this.setAtX, this.setAtY);

this 是引发 onload 事件的图像。

编辑 我更改了用于携带坐标的属性。现在它们已设置AtX/Y。您不能使用 x 和 y,因为它们是保留的。

I've tried your code and the onload method always use the LAST value of the vars, not the value when the array was iterated.

Try setting the X and the Y to properties of the image object:

// I assume you are storing the coordinates where the letters must be
letterImg.setAtX = letter[i][XPOS];
letterImg.setAtY = letter[i][YPOS];

and on the onload:

context.drawImage(this, this.setAtX, this.setAtY);

this is the image raising the onload event.

Edit I've changed the properties used to carry the coordinates. Now they're setAtX/Y. You cannot use x and y because they're reserved.

樱花坊 2024-11-08 10:57:59

你把它们画在同一个点上。 drawImage 不关心给定参数的高度或宽度;它只需要一个图像和一个坐标。请参阅 https://developer.mozilla.org/en/Canvas_tutorial/Using_images

所以,您需要为您的图像提供更多数据;像这样的东西:

    thisWidth = letters[i][0];
    thisHeight = letters[i][1];
    imgSrc = letters[i][2];
    thisX = letters[i][3]; //<---
    thisY = letters[i][4]; //<---
    letterImg = new Image();
    letterImg.onload = function()
    {
        context.drawImage(letterImg, thisX, thisY, thisWidth, thisHeight);
        //or, just
        context.drawImage(letterImg, thisX, thisY);
    }
    letterImg.src = imgSrc;

编辑:只是有一个想法 - 你可以以神奇的方式做到这一点:

context.drawImage(letterImg, letters[i-1][0]+thisWidth, letters[i-1]+thisHeight, thisWidth, thisHeight);

通过这种方式,你必须检查东西,但我认为你得到了总体意图。

You're drawing them on the same point. drawImage doesn't care about the height or width with your given parameters; it just wants an image and a coordinate. See https://developer.mozilla.org/en/Canvas_tutorial/Using_images

So, you're gonna need to give your images more data; something like:

    thisWidth = letters[i][0];
    thisHeight = letters[i][1];
    imgSrc = letters[i][2];
    thisX = letters[i][3]; //<---
    thisY = letters[i][4]; //<---
    letterImg = new Image();
    letterImg.onload = function()
    {
        context.drawImage(letterImg, thisX, thisY, thisWidth, thisHeight);
        //or, just
        context.drawImage(letterImg, thisX, thisY);
    }
    letterImg.src = imgSrc;

Edit: Just had a thought - you can do it dymagically:

context.drawImage(letterImg, letters[i-1][0]+thisWidth, letters[i-1]+thisHeight, thisWidth, thisHeight);

With this way you'll have to check for stuff, but I think you get the overall intention.

迷路的信 2024-11-08 10:57:59

您必须每次都重新定位绘制开始位置,以免图像被覆盖。

[context . drawImage(image, dx, dy, dw, dh)][1]

链接上有一张图片解释了每个参数的含义。

You have to reposition the draw start position every time so the images arent overwritten.

[context . drawImage(image, dx, dy, dw, dh)][1]

There's an image on the link explaning what every parameter means.

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