设置宽度/高度时禁用 HTML5 Canvas 中的重置

发布于 2024-10-25 06:36:18 字数 172 浏览 2 评论 0原文

我的场景是画布宽度/高度动态更改,并且我不想重置画布。相反,我使用clearRect(),因为我知道必须清除矩形的边界。

  1. 是否可以在再次设置宽度/高度时禁用画布的重置?

  2. 有没有一种方法可以保存以前的状态并将其完全加载回画布而不重新绘制它?

My scenario is that the Canvas width / height is changed dynamically, And I dont want to reset the Canvas. Instead I use the clearRect() as I know the bounds in which the rectangle has to be cleared.

  1. Is there a way to disable the resetting of canvas when Width/Height is set again?

  2. Is there a way to save the previous state and just load it back to the canvas exactly without re-drawing it?

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

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

发布评论

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

评论(1

始于初秋 2024-11-01 06:36:18
  1. 恐怕没有办法禁用它,它内置于画布规范中。

    第 4.8.11 节:

    <块引用>

    当创建canvas元素时,以及随后每当设置宽度和高度属性(无论是新值还是以前的值)时,位图和任何关联的上下文都必须清除回其初始状态并重新初始化新指定的坐标空间尺寸。

    恐怕

  2. 是的,GetImageData/PutImageData 是一种方法,但它可能比以下方法慢得多:

    假设您的画布名为 realCanvas。制作第二个画布(我们将其称为 fakeCanvas),其大小与您想要的真实画布一样大,但仅在 javascript 代码中制作它,并且永远不会将其添加到文档中(所以不人们将永远看到它)。

    然后,在调整 realCanvas 大小之前,执行以下操作:

    fakeCanvasContext.drawImage(realCanvas, 0, 0);
    

    这会将整个画布绘制到另一个画布上,从性能角度来看,这会发生得非常快。

    完成大小调整后,您可以将 fakeCanvas 的内容绘制回 realCanvas 上。

    realCanvasContext.drawImage(fakeCanvas, 0, 0);
    

    就是这样!

    如果您想获得技术知识,您可以通过在第二次抽奖中执行此操作来加快我的速度:

    realCanvasContext.drawImage(fakeCanvas,
        0, 0, realCanvas.宽度, realCanvas.高度,
        0, 0, realCanvas.width, realCanvas.height);`
    

    这样,您只需复制适合 realCanvas 的部分。请注意,我尚未测试我编写的代码,因此可能存在一两个语法错误。

  1. I'm afraid there's no way to disable this, it is built into the canvas spec.

    Section 4.8.11:

    When the canvas element is created, and subsequently whenever the width and height attributes are set (whether to a new value or to the previous value), the bitmap and any associated contexts must be cleared back to their initial state and reinitialized with the newly specified coordinate space dimensions.

  2. Yes, and GetImageData/PutImageData is one way but it is probably much slower than the following way:

    Let's say your canvas is called realCanvas. Make a second canvas (we'll call it fakeCanvas) that is as large as you ever intend your real canvas to be, but only make it in javascript code and never add it to the document (so no one will ever see it).

    Then, right before you resize realCanvas, do this operation:

    fakeCanvasContext.drawImage(realCanvas, 0, 0);
    

    This will draw one entire canvas to another and it will happen very quickly from a performance perspective.

    Once you are done with the resize you can draw the contents of fakeCanvas back onto your realCanvas.

    realCanvasContext.drawImage(fakeCanvas, 0, 0);
    

    And that's it!

    If you want to get technical, you can speed up my way even more by doing this for the second draw:

    realCanvasContext.drawImage(fakeCanvas,
        0, 0, realCanvas.width, realCanvas.height,
        0, 0, realCanvas.width, realCanvas.height);`
    

    That way you are only copying the part that can fit onto realCanvas. Please note that I haven't tested the code I wrote so there might be a syntax error or two.

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