使用缓冲区中的drawImage更新画布不使用drawImage应用操作

发布于 2024-12-22 01:08:57 字数 559 浏览 8 评论 0原文

我使用缓冲方法来更新画布,问题是当我在缓冲区画布中绘制图像并将其应用到真实画布时,真实画布上没有图像。但我可以将其他任何东西应用到真实的画布上。

这是我的代码:

var ctx = $('#canvas')[0].getContext("2d"),
    width  = $("#canvas").width(),
    height = $("#canvas").height(),
    buffer = $("<canvas>")[0].getContext("2d");
ctx.canvas.width = width;
ctx.canvas.height = height;
buffer.canvas.width = width;
buffer.canvas.height = height;
var image = new Image();
image.src = "img/logo.png";
$(image).load(function() {
    buffer.drawImage(image, 0, 0);
});
ctx.drawImage(buffer.canvas, 0, 0);

I use the buffering method to update a canvas, the thing is when I draw an Image in my buffer canvas and apply it to the real canvas there is no image at the real canvas. But I can apply anything else to the real canvas.

This is my code:

var ctx = $('#canvas')[0].getContext("2d"),
    width  = $("#canvas").width(),
    height = $("#canvas").height(),
    buffer = $("<canvas>")[0].getContext("2d");
ctx.canvas.width = width;
ctx.canvas.height = height;
buffer.canvas.width = width;
buffer.canvas.height = height;
var image = new Image();
image.src = "img/logo.png";
$(image).load(function() {
    buffer.drawImage(image, 0, 0);
});
ctx.drawImage(buffer.canvas, 0, 0);

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

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

发布评论

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

评论(1

最近可好 2024-12-29 01:08:57

它不起作用,因为这些事件正在发生:

  1. 您正在向常规画布绘制一个空缓冲区。
  2. 然后图像就完成加载了。
  3. 然后图像被绘制在缓冲区上。您现在有了带有图像的缓冲区,但它永远不会被绘制到常规画布上。

如果您将 ctx.drawImage 行放在 onload: 内部,

$(image).load(function() {
    buffer.drawImage(image, 0, 0);
    ctx.drawImage(buffer.canvas, 0, 0);
});

它将按照您想要的方式工作。

It's not working because these events are occuring:

  1. you're drawing an empty buffer to your regular canvas.
  2. Then an image is done loading.
  3. Then the image gets drawn on the buffer. You now have buffer-with-image, but that never gets drawn to the regular canvas.

If you put the ctx.drawImage line inside of the onload:

$(image).load(function() {
    buffer.drawImage(image, 0, 0);
    ctx.drawImage(buffer.canvas, 0, 0);
});

it would work like you want.

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