datauri/图像到画布
如何将图像或 datauri 转换为画布?我在 http://www.nihilogic.dk/labs/canvas2image/ 看到了另一种方式,但我也想首先了解如何创建画布。
我问这个问题的原因是因为我想创建一个小应用程序,让用户创建画布“图像”,将其保存为某种东西,并能够在将来重新打开它以进行画布修改。我认为 datauri 是保存画布的好方法,但我不确定如何重新打开 datauri 并使用 canvas 修改绘图。
谢谢!
How do I convert an image or datauri to canvas? I see the other way at http://www.nihilogic.dk/labs/canvas2image/, but I also want to find out how I can create a canvas at the first place.
The reason I ask this question is because I would like to create a little app that lets user create a canvas 'image', save it as something and be able to reopen it for canvas modification in the future. I figured that datauri is a good way to save a canvas, but I am not sure how I can do to reopen the datauri and use canvas to modify the drawing.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 data URI 数据作为源创建一个图像元素,然后使用 drawImage 将其绘制到画布上:
更新:澄清 ctx 是 2D 上下文,而不是画布对象。
Create an image element using the data URI data as the source and then use drawImage to draw it to the canvas:
Updated: to clarify that ctx is a 2D context, not a canvas object.
我得到了这个工作:
看起来技巧是 Canvas 元素需要指定其宽度和高度。当我不指定尺寸时,它变得有点任意,它只显示我的大图像的一部分(来自 dataUri)
I got this to work:
Looks like the trick is that the Canvas element needs its width and height specified. When I don't specify the dimensions, it becomes sort of arbitrary and it only shows part of my big image (from dataUri)