H5之canvas画图,下载,借用html2canvas插件
DPR() { // 获取设备的屏幕尺寸
if (window.devicePixelRatio && window.devicePixelRatio > 1) {
return window.devicePixelRatio;
}
return 1;
},
parseValue(value) { // 转换参数为number类型
return parseInt(value, 10);
},
async drawCanvas (selector) { // canvas画图
// 获取想要转换的 DOM 节点
const dom = document.querySelector(selector);
const box = window.getComputedStyle(dom);
// DOM 节点计算后宽高
const width = this.parseValue(box.width);
const height = this.parseValue(box.height);
// 获取像素比
const scaleBy = this.DPR();
// 创建自定义 canvas 元素
var canvas = document.createElement('canvas');
// 设定 canvas 元素属性宽高为 DOM 节点宽高 * 像素比
canvas.width = width * scaleBy;
canvas.height = height * scaleBy;
// 设定 canvas css宽高为 DOM 节点宽高
canvas.style.width = `