html2canvas将HTML内容写入Canvas生成图片

用法

http://html2canvas.hertzen.com/documentation
要element使用带有一些(可选)选项的 html2canvas 呈现,只需调用html2canvas(element, options);

html2canvas(document.body).then(function(canvas) {
    document.body.appendChild(canvas);
});

可用的配置选项

namedefaultdescription
allowTaintfalse是否允许跨域图像污染画布
backgroundColor#ffffff画布背景色(如果未在DOM中指定)。设置null为透明
canvasnull现有canvas元素用作绘图的基础
foreignObjectRenderingfalse如果浏览器支持,是否使用ForeignObject渲染
imageTimeout15000加载图像的超时时间(以毫秒为单位)。设置0为禁用超时。
ignoreElements(element) => false谓词功能,可从渲染中删除匹配的元素。
loggingtrue启用日志以进行调试
onclonenull克隆文档以进行渲染时调用的回调函数可用于修改将要渲染的内容,而不会影响原始源文档。
proxynull代理将用于加载跨域图像的网址。如果保留为空,则不会加载跨域图像。
removeContainertrue是否清除html2canvas临时创建的克隆DOM元素
scalewindow.devicePixelRatio用于渲染的比例。默认为浏览器设备像素比率。
useCORSfalse是否尝试使用CORS从服务器加载图像
widthElement widthcanvas宽度
heightElement heightcanvas高度
xElement x-offset裁剪画布X坐标
yElement y-offset裁剪画布y坐标
scrollXElement scrollX渲染元素时要使用的x滚动位置(例如,如果Element使用position: fixed)
scrollYElement scrollY呈现元素时要使用的y-scroll位置(例如,如果Element使用position: fixed)
windowWidthWindow.innerWidth渲染时使用的窗口宽度Element,这可能会影响媒体查询之类的内容
windowHeightWindow.innerHeight渲染时要使用的窗口高度Element,这可能会影响媒体查询之类的内容
<div class="save-img" ref="userCode"> </div>
import html2canvas from 'html2canvas'
const canvas2 = document.createElement('canvas');
let dom = that.$refs.userCode;
let width = dom.offsetWidth; //获取dom 宽度
let height = dom.offsetHeight; //获取dom 高度
canvas2.width = width * window.devicePixelRatio;
canvas2.height = height * window.devicePixelRatio;
html2canvas(dom,{
	backgroundColor: null,
	async: true,
	useCORS:true,
	//dpi: window.devicePixelRatio ,
	canvas: canvas2
}).then((canvas) => {
	let dataURL = canvas.toDataURL("image/png");
	that.saveImgUrl = dataURL;
});
JavaScript HTML renderer The script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page. How does it work? The script renders the current page as a canvas image, by reading the DOM and the different styles applied to the elements. It does not require any rendering from the server, as the whole image is created on the clients browser. However, as it is heavily dependent on the browser, this library is not suitable to be used in nodejs. It doesn't magically circumvent any browser content policy restrictions either, so rendering cross-origin content will require a proxy to get the content to the same origin. The script is still in a very experimental state, so I don't recommend using it in a production environment nor start building applications with it yet, as there will be still major changes made. Browser compatibility The script should work fine on the following browsers: Firefox 3.5+ Google Chrome Opera 12+ IE9+ Safari 6+ As each CSS property needs to be manually built to be supported, there are a number of properties that are not yet supported. Usage Note! These instructions are for using the current dev version of 0.5, for the latest release version (0.4.1), checkout the old readme. To render an element with html2canvas, simply call: html2canvas(element[, options]); The function returns a Promise containing the <canvas> element. Simply add a promise fullfillment handler to the promise using then: html2canvas(document.body).then(function(canvas) { document.body.appendChild(canvas); }); Building The library uses grunt for building. Alternatively, you can download the latest build from here. Clone git repository with submodules: $ git clone --recursive git://github.com/niklasvh/html2canvas.git Install Grunt and uglifyjs: $ npm install -g grunt-cli uglify-js Run the full build process (including lint, qunit and webdriver tests): $ grunt Skip lint and tests and simply build from source: $ grunt build Running tests The library has two sets of tests. The first set is a number of qunit tests that check that different values parsed by browsers are correctly converted in html2canvas. To run these tests with grunt you'll need phantomjs. The other set of tests run Firefox, Chrome and Internet Explorer with webdriver. The selenium standalone server (runs on Java) is required for these tests and can be downloaded from here. They capture an actual screenshot from the test pages and compare the image to the screenshot created by html2canvas and calculate the percentage differences. These tests generally aren't expected to provide 100% matches, but while commiting changes, these should generally not go decrease from the baseline values. Start by downloading the dependencies: $ npm install Run qunit tests: $ grunt test Examples For more information and examples, please visit the homepage or try the test console. Contributing If you wish to contribute to the project, please send the pull requests to the develop branch. Before submitting any changes, try and test that the changes work with all the support browsers. If some CSS property isn't supported or is incomplete, please create appropriate tests for it as well before submitting any code changes.
html2canvas是一个JavaScript库,它可以在浏览器端或app端直接对整个或部分页面进行截屏,并将页面渲染成一个canvas图片。通过读取DOM并将不同的样式应用到这些元素上实现截图功能。 在uniapp中使用html2canvas生成图片的步骤如下: 1. 监听截图按钮的点击事件。 2. 调用html2canvas函数,并传入要截图的元素作为参数,可以使用document.getElementById()来获取元素。 3. 在html2canvas的配置对象中,可以设置一些属性,比如背景颜色、是否支持图片跨域、放大倍数、截图的高度和宽度等。 4. 使用then()方法来处理生成canvas对象。 5. 使用canvas对象的toDataURL()方法将canvas转换为base64格式的图片数据。 6. 使用生成图片数据,可以进行一些操作,比如保存到本地或者跳转到其他页面进行展示。 通过以上步骤,你可以使用html2canvas在uniapp中生成图片[1]。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [小程序 uniapp中webview内嵌H5页面 html2canvas截图 base64的图片保存到手机相册](https://blog.csdn.net/M__O__M/article/details/126985611)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [html2canvasHTML内容写入Canvas生成图片 uniapp](https://download.csdn.net/download/weixin_42085648/13704954)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值