import html2canvas from 'html2canvas';
const html2Image = ({ width, height, offsetX, offsetY } = {}) => {
    // 手动创建一个 canvas 标签
    const canvas = document.createElement('canvas');
    const container = document.getElementById('container');
    // 获取父级的宽高
    const containerWidth = parseInt(window.getComputedStyle(container).width);
    const containerHeight = parseInt(window.getComputedStyle(container).height);
    // 宽高 * 2 并放大 2 倍 是为了防止图片模糊
    canvas.width = (width || containerWidth) * 2;
    canvas.height = (height || containerHeight) * 2;
    const options = {
        x: offsetX || 0,
        y: offsetY || 0,
        canvas,
        useCORS: true,
        scale: 2,
        ignoreElements: (element) => {
            if (element &&
                element.className.includes &&
                element.className.includes('header-container')) {
                return true;
            }
        }
    };
    html2canvas(container, options).then((canvas) => {
        // toDataURL 图片格式转成 base64
        const dataURL = canvas.toDataURL('image/png');
        downloadImage(dataURL);
    });
};
// 下载图片
const downloadImage = (url) => {
    // 如果是在网页中可以直接创建一个 a 标签直接下载
    const a = document.createElement('a');
    a.href = url;
    a.download = '截图';
    a.click();
};
export default html2Image;
