UNPKG

web-utils-super

Version:

前端函数库

19 lines (17 loc) 694 B
/** * @desc: 截图 返回图片base64链接(只能img标签) * @param {String} id * @param {Number} height * @param {Number} width * @return {String} base64 */ function domToBase64(id, height, width) { const dom = document.getElementById(id); dom.setAttribute("crossOrigin", "anonymous"); // 添加srossOrigin属性,解决跨域问题 const canvas = document.createElement("canvas"); canvas.width = height || dom.clientWidth; canvas.height = width || dom.clientHeight; canvas.getContext("2d").drawImage(dom, 0, 0, canvas.width, canvas.height); // 截 return canvas.toDataURL("image/png"); // 将图片转成base64格式 } module.exports = domToBase64;