如何将画布的一部分捕获为位图
我想捕获画布的一小部分作为位图。这可能吗?这样我就可以在该区域上绘制另一个位图后替换它。完成位图后,我想用原始作品替换我绘制的一小块画布。
谢谢!
I would like to capture a small part of a canvas as a bitmap. Is that possible? This is so that I can replace it after I draw another bitmap on that area. Once I am done with the bitmap I would like to replace the small bit of canvas that I drew on with the original piece.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上下文的
drawImage()
方法允许您使用现有的画布作为源。它还允许您指定要绘制的源“图像”的子区域。您还可以通过编程方式创建新的画布元素。将它们结合起来,您就可以创建自己的离屏缓冲区并与它们进行 blit,而无需通过getImageData()/putImageData()
或数据 URL。我已经在网上放置了一个示例。
请注意,虽然该示例碰巧将动态创建的画布附加到文档中以便您可以看到它(源代码的第 29 行),但这不是必需的。在文档功能中创建的画布元素,无论是否附加到层次结构。
简而言之:
编辑:我基准测试此技术与
getImageData/putImageData
;如果速度很重要,请使用drawImage
来进行位块传送区域。这是我看到的:(来源:phrogz.net)
基准执行者在 2.8GHz Core i7 MacBook Pro 的 OS X 上保存和恢复 125x180 区域 10,000 次。您的里程可能会有所不同。具体来说,保存/恢复更大或更小的区域可能会导致不同的相对性能。
The
drawImage()
method of contexts allows you to use an existing canvas as the source. It also allows you to specify sub-regions of the source "image" to draw. You can also create a new canvas element programmatically. Combine these, and you can create your own offscreen buffers and blit to/from them without needing to go throughgetImageData()/putImageData()
or data URLs.I've put an example of this online.
Note that while the example happens to append the dynamically-created canvas to the document so that you can see it (line 29 of the source), this is not necessary. Canvas elements created in the document function whether attached to the hierarchy or not.
In short:
Edit: I benchmarked this technique versus
getImageData/putImageData
; if speed is important, usedrawImage
for blitting regions. Here's what I saw:(source: phrogz.net)
Benchmarks performed by saving and restoring a 125x180 region 10,000 times on OS X on a 2.8GHz Core i7 MacBook Pro. Your mileage may vary. Specifically, saving/restoring regions that are either much larger or smaller may result in different relative performance.
您可以使用 < 来执行此操作code>.getImageData() 和
.putImageData()
示例
和一个简单的在线示例,只有你:)
在这里尝试
you can do this using
.getImageData()
and.putImageData()
Example
and a simple Online Example, just you :)
Try it Here
在这里看看类似的问题 如何将一个画布的内容本地复制到另一个画布
我建议编写一些内容,例如
将图像保存在变量中,然后使用
Taken from 此处
have a look at a similar question here How to Copy Contents of One Canvas to Another Canvas Locally
What i suggest is write somthing like
to save image in a variable and then retrive it later with
Taken from here