首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >html2canvas - IndexSizeError

html2canvas - IndexSizeError
EN

Stack Overflow用户
提问于 2016-09-08 18:32:41
回答 1查看 464关注 0票数 0

我正在尝试获取加载到页面div中的图像,并尝试传入图像的原始数据,或者尝试将图像复制到某个位置。

我能找到的最好的方法是使用html2canvas

在尝试将图像打开到窗口中进行预览时,出现以下错误:

JavaScript运行时错误: IndexSizeError

我读过一些帖子,建议我可以使用以下方法呈现div:

代码语言:javascript
复制
html2canvas($("#mapDiv"), {
            onrendered: function (canvas) {
                // canvas is the final rendered <canvas> element
                var imgData = canvas.GetContext("2d").getImageData(bounds.left, bounds.top, bounds.width, bounds.height);
                var myImage = imgData.toDataURL("image/png");
                window.open(myImage);
            }
        });

问题出现在html2canvas js文件中的以下行

代码语言:javascript
复制
ctx.drawImage(canvas, bounds.left, bounds.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);

是否有可能以这种方式呈现动态加载到div中的图像,因为由于用户具有不同的分辨率,我无法传入像素参数

如果有可能,我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2016-09-08 19:13:29

代码语言:javascript
复制
function capture(id) {

        html2canvas([document.getElementById(id)], {
            logging: true,
            onrendered: function(canvas) {
                document.body.appendChild(canvas);
                var dataUrl = canvas.toDataURL("image/png")
                //console.log(dataUrl)
                $('#img-out').attr("src", canvas.toDataURL("image/png"));

                $.post("/upload-img", {img: dataUrl, name: id}, function (res) {
                    console.log(res) 


                    location.href = $("#" + id).attr("shareUrl") + "&picture=" + (window.location.protocol + "//" +window.location.host + "/" + res.filePath)

                } )
            }
        });
    }

传递您想要快照的元素的id。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39388640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档