如何下载带有背景图片的画布? [英] How to download canvas with background image?

查看:82
本文介绍了如何下载带有背景图片的画布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个canvas元素,可以通过代码动态设置背景.然后,我使用素描库( http://intridea.github.io/sketch.js/)在画布上绘制. -全部都可以.

I have a canvas element that I'm setting the background on dynamically via code. Then I'm using the Sketch library (http://intridea.github.io/sketch.js/) to draw on the canvas. - This all works.

但是,每当我尝试使用canvas.toDataURL("image/png")转换画布时,它都可以保存画布图形,但是不会保存背景. -我知道这是按设计进行的.

However, whenever I try to convert the canvas using canvas.toDataURL("image/png") it's able to save the canvas drawing, however isn't saving the background. - I understand this is working as designed.

有没有办法将两者合并?我想弄清楚我可以在完成绘制并尝试将其导出后将图像src设置为背景src的想法,但是我不确定.有人对此有任何经验吗?

Is there a way to merge the two? I was toying around with the idea that I could set the image src to the background src after I'm done drawing and try to export that, however I'm not certain. Does anyone have any experience with this?

推荐答案

您已经发现,画布及其背景是分别维护的,并且toDataURL也不会捕获背景.

As you've discovered, the canvas and its background are maintained separately and toDataURL will not also capture the background.

您可以使用画布合成将背景与草图结合起来.

You can combine the background with the sketch using canvas compositing.

目标上方"合成模式可让您在草图后绘制背景图像

The 'destination-over' compositing mode will let you drawImage your background behind the sketches

context.globalCompositeOperation="destination-over";

context.drawImage(backgroundImage,0,0);

现在,背景像素已在您的草图后面绘制,并且都将使用toDataURL捕获.

Now the background pixels have been drawn behind you sketch and both will be captured with toDataURL.

这篇关于如何下载带有背景图片的画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆