当前位置:网站首页>JS将DOM导出为图片的方法
JS将DOM导出为图片的方法
2022-06-12 12:08:00 【老电影故事】
前言
今天遇到个需求,需要将页面中显示的思维导图或者echarts图标导出为图片文件,但是不想用自带的导出方法,虽然自带的很方便,香,那么,可以怎么搞呢,请听我娓娓道来。
第一步、安装插件
npm install html2canvas
或
yarn add html2canvas
第二步、引入插件
import html2canvas from 'html2canvas'
第三步、导出图片
// 获取要导出的DOM
const rect = document.querySelector('#chart').getBoundingClientRect()
html2canvas(document.querySelector('#chart'), {
width: rect.width,
height: rect.height
}).then(function (canvas) {
const pageData = canvas.toDataURL('image/jpeg', 1.0)
const imgData = pageData.replace('image/jpeg', 'image/octet-stream')
const imgName = '生成图片.jpg'
const save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
save_link.href = imgData
save_link.download = imgName
const event = document.createEvent('MouseEvents')
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
save_link.dispatchEvent(event)
})
边栏推荐
- LeetCode_二分搜索_中等_162. 寻找峰值
- Analyze the implementation principle of the onion model, and connect the onion model in your own project
- LeetCode_字符串_简单_344.反转字符串
- [转]placement new
- 无重复字符的最长字符串(LeetCode 3)
- Kubernetes cluster setup
- LeetCode 890. Find and replace mode (analog + double hash table)
- What is modularity? Benefits of modularity
- [译] Go语言测试进阶版建议与技巧
- Preliminary study on Regional Simulation of crop model
猜你喜欢

开源项目-(ERP+酒店+电商)后台管理系统

Common debugging tools and commands for ROS

5g NR Protocol Learning - - ts38.211 downlink channel

IP address management

無重複字符的最長字符串(LeetCode 3)

5G NR協議學習--TS38.211下行通道

LeetCode 1037. 有效的回旋镖(向量叉乘)

ELK搭建指南

QT adds a summary of the problems encountered in the QObject class (you want to use signals and slots) and solves them in person. Error: undefined reference to `vtable for xxxxx (your class name)‘

JS to load and display Excel files
随机推荐
LeetCode_ Binary search_ Medium_ 162. looking for peaks
Beyondcompare 4 uses PJ
无重复字符的最长字符串(LeetCode 3)
5G NR協議學習--TS38.211下行通道
淘宝新改版商家如何操作,需要注意的点有哪些
[database] SQLite version upgrade and downgrade
Google Earth engine (GEE) - quick land classification by kmeans clustering (double for loop quick parameter adjustment)
【Leetcode】79. Word search
B. Wall painting (C language)
[foundation of deep learning] back propagation method (1)
传统的DOM渲染方式?
KDD2022 | 边信息增强图Transformer
宏编译 预处理头 WIN32_LEAN_AND_MEAN
Left and right cases + rotating pictures of small dots + no time
About message
Automatic generation of folder directory structure
object. Defineproperty basic usage
5g NR protocol learning -- ts38.211 downlink channel
#ifndef#define#endif防止头文件重复包含, 你不是真的懂
创建Servlet项目