循环创建多个画布,其中包含多个图像,但并不总是显示图像

发布于 2024-12-29 22:39:15 字数 738 浏览 0 评论 0原文

这可能解释起来很奇怪: 我想动态创建一个画布集合,其中包含单个图像。这显示了(经过很多麻烦并摆脱了 onload 事件),但是当我尝试刷新页面时,有时屏幕上什么也没有。 当我使用 onload 事件(等待图像加载)时,它不会显示或显示最后一个画布中的所有内容。

这是代码片段:

var sources = []//the array that contains the images
var divCanvas = document.getElementById('showcase-wrapper')
for(var i=0; i<sources.length; i++){
    img =  new Image()
    img.src = sources[i]
    canvas  = document.createElement('canvas')
    $(canvas).attr('id', i)
    canvas.height=300
    canvas.width=200
    var context = canvas.getContext('2d');
    //onload commented out allows expected display
    //img.onload = function() {
    context.drawImage(img, 0, 0);
    //} 
    divCanvas.appendChild(canvas)
}

我看过很多看起来像我的帖子,并尝试了很多但无济于事。

this may be an odd one to explain:
I want to create dynamically a collection of canvas that will have a single image in it. And this displays (after much hassle and getting rid of the onload event) but when I try to refresh the page I sometimees get nothing on the screen.
And when I was using thee onload event (to wait until the image is loaded) it would not display or display everything in the last canvas.

Here is a snippet of the code:

var sources = []//the array that contains the images
var divCanvas = document.getElementById('showcase-wrapper')
for(var i=0; i<sources.length; i++){
    img =  new Image()
    img.src = sources[i]
    canvas  = document.createElement('canvas')
    $(canvas).attr('id', i)
    canvas.height=300
    canvas.width=200
    var context = canvas.getContext('2d');
    //onload commented out allows expected display
    //img.onload = function() {
    context.drawImage(img, 0, 0);
    //} 
    divCanvas.appendChild(canvas)
}

I have seen many posts that seemed to look like mine and tried quite a few but to no avail.

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

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

发布评论

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

评论(3

我做我的改变 2025-01-05 22:39:15

我认为问题在于 var 上下文在触发 img.onload 事件之前被覆盖。 img.onload 事件仅引用全局范围。这就是为什么只显示最后一张图像的原因。您需要在事件中找到正确的上下文。

我无法直接测试你的代码,但我认为它应该是这样的:

var sources = []//the array that contains the images
var divCanvas = document.getElementById('showcase-wrapper')
for(var i=0; i<sources.length; i++){
    img =  new Image();
    img.src = sources[i];
    img.id = i; //Allow img to remember its corresponding canvas/context
    canvas  = document.createElement('canvas');
    $(canvas).attr('id', i);
    canvas.height=300;
    canvas.width=200;
    var context = canvas.getContext('2d');
    divCanvas.appendChild(canvas);

    img.onload = function() {
        //Use the image id to get the correct context
        var canvas = document.getElementById(this.id);
        var context = canvas.getContext('2d');
        context.drawImage(this, 0, 0);
    } 
}

I think the issue is that var context is overwritten before the img.onload event is triggered. The img.onload event only has a reference to global scope. This is why only the last image shows up. You need to find the correct context inside the event.

I can't test your code directly, but I think it should be something like this:

var sources = []//the array that contains the images
var divCanvas = document.getElementById('showcase-wrapper')
for(var i=0; i<sources.length; i++){
    img =  new Image();
    img.src = sources[i];
    img.id = i; //Allow img to remember its corresponding canvas/context
    canvas  = document.createElement('canvas');
    $(canvas).attr('id', i);
    canvas.height=300;
    canvas.width=200;
    var context = canvas.getContext('2d');
    divCanvas.appendChild(canvas);

    img.onload = function() {
        //Use the image id to get the correct context
        var canvas = document.getElementById(this.id);
        var context = canvas.getContext('2d');
        context.drawImage(this, 0, 0);
    } 
}
带上头具痛哭 2025-01-05 22:39:15

为了保持行为一致,您必须使用onload。如果不这样做,画布绘制代码可能会在加载图像之前执行,并且不会绘制所需的图像。

可能不会调用将绘制到其他画布的 onload ,因为在事件触发之前 Image 正在被垃圾收集。

尝试

var images = [];

在代码开头和

  images.push(img);

img = new Image() 行之后添加。

如果这不起作用,请尝试将这些图像添加到 DOM 树中 - 首先 img.setAttribute('style', 'display: none') 这样您就看不到它们,它们也看不到。不干扰文档结构。

For consistent behavior, you must use onload. If you don't the canvas drawing code may execute before the image is loaded and the desired image will not be drawn.

It might be that the onloads that would draw to the other canvases are not being called because the Images are getting garbage collected before the event can fire.

Try adding

var images = [];

At the start of your code and

  images.push(img);

After the img = new Image() line.

If that doesn't work, try adding those images to the DOM tree-- img.setAttribute('style', 'display: none') first so you don't see them and they don't interfere with the document structure.

剩一世无双 2025-01-05 22:39:15

我在这里找到了修复程序:

https://developer.mozilla.org /docs/Web/Guide/HTML/Canvas_tutorial/Using_images#Drawing_images

显然他们已经删除了 img.onload 以便在正文时调用该函数已加载完毕。
不过,我仍然不明白为什么我的脚本或 ellisbben 解决方案不起作用。

如果有人能回答这个问题,那将非常受欢迎......

I found the fix here:

https://developer.mozilla.org/docs/Web/Guide/HTML/Canvas_tutorial/Using_images#Drawing_images

Apparently they have dropped img.onload to have the function called when the body has finished loaded.
Though, I still do not understand why my script or, ellisbben solution was not working.

If anyone as an answer for it, it would be very welcomed...

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