`html2canvas(document.body)` 生成错误 – “加载图像时出错”

huangapple go评论76阅读模式
英文:

html2canvas(document.body) generates error - "Error loading image"

问题

我正在尝试使用html2canvas进行屏幕截图,但出现了“加载图像时出错”的错误。

我的代码非常简单 -

function SaveScreenshot() {
    html2canvas(document.body).then(canvas => {
        var image = canvas.toDataURL('image/webp');
        try {
            localStorage.setItem("img1", image);
        } catch (e) {
            console.log("Storage failed: " + e);
        }
    });
    return false;
}

您可以自行尝试。转到 - https://bengurion.azurewebsites.net/gallerynew
使用相机按钮拍摄一张照片(或者不要拍摄,这并不重要)。
通过点击汉堡按钮进入菜单。
转到相册按钮(看起来像有人的4张图片)。

这是调用该函数的按钮。

请协助。

英文:

I am trying to take a screenshot with html2canvas and I am getting the error "Error loading image".

My code is fairly simple -

 function SaveScreenshot() {
            html2canvas(document.body).then(canvas => {
               
                var image = canvas.toDataURL('image/webp');
                
                try {
                    localStorage.setItem("img1", image);
                }
                catch (e) {
                    console.log("Storage failed: " + e);
                }

            });
            return false;
        }

You can try it yourself. Go to - https://bengurion.azurewebsites.net/gallerynew
Take a snap with the webcam using the camera button (or don't. It doesn't really matter).
Go to the menu by pressing on the hamburger button.
Go to the gallery button (looks like 4 images with people in them).

This is the button that's calling the function.

Please assist.

答案1

得分: 2

I'm not pretty sure why you use 'image/webp' MIME type but it is supported only by chrome. I think you can try something like this. Much common way of doing screen shots.

 function SaveScreenshot() {
            html2canvas(document.body).then(canvas => {

                const base64Image = canvas.toDataURL('image/jpeg', 1);

                try {
                    localStorage.setItem("img1", base64Image);
                }
                catch (e) {
                    console.log("Storage failed: " + e);
                }

            });
            return false;
        }
英文:

I'm not pretty sure why you use 'image/webp' MIME type but it is supported only by chrome. I think you can try something like this. Much common way of doing screen shots.

 function SaveScreenshot() {
            html2canvas(document.body).then(canvas => {

                const base64Image = canvas.toDataURL('image/jpeg', 1);

                try {
                    localStorage.setItem("img1", base64Image);
                }
                catch (e) {
                    console.log("Storage failed: " + e);
                }

            });
            return false;
        }

huangapple
  • 本文由 发表于 2020年1月3日 19:40:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定