我有一个带有外部图像的HTML div.(以下是一个例子,但在实际情况中,我使用的是Amazon S3,因此在同一台服务器上下载并存储图像不是一个选项)目前,我使用html2canvas将div转换为图像.然而,外部图像总是被一个空白代替.

我用来捕捉图像的代码:

$(function() { 
    $("#btnSave").click(function() { 
        html2canvas($("#widget"), {
            onrendered: function(canvas) {
                theCanvas = canvas;
                document.body.appendChild(canvas);

                // Convert and download as image 
                Canvas2Image.saveAsPNG(canvas); 
                $("#img-out").append(canvas);
            }
        });
    });
}); 

Edited: jsfiddle: https://jsfiddle.net/0y2zxxL8/3/

I may use other library. I may also do that in backend. (I am using PHP + laravel 5 for backend) Is there a way I can generate a "screenshot" of the HTML div with external images?

Update The current answer are working after editing. Yet, for my actual use, there will be multiple image with their position set by the user by drag and drop. I can still get the position, but it would be better for me if it is possible to not set the position specifically.

推荐答案

Your JSFiddle given ReferenceError: Canvas2Image is not defined while hit on 'Save PNG' button. So check your code(not fiddle) for same error.

UPDATE
See this JSFiddle example as reference. May this one will help you.

UPDATE 2

Working result with FF v44 and its working. Taken snap with JSFiddle code enter image description here

$(function() { 
    $("#btnSave").click(function() { 
        html2canvas($("#widget"), {
            onrendered: function(canvas) {
                var context=canvas.getContext("2d"); // returns the 2d context object
                var img=new Image() //creates a variable for a new image

                img.src= "http://lorempixel.com/400/200/"; // specifies the location of the image
                context.drawImage(img,0,50); // draws the image at the specified x and y location
                // Convert and download as image 
                theCanvas = canvas;
                document.body.appendChild(canvas);

                // Convert and download as image 
                Canvas2Image.saveAsPNG(canvas); 
                $("#img-out").append(canvas);
            }
        });
    });
});

UPDATE 3 (12/29/2016) JSFiddle

经过研究,发现问题是因为我们只分配了图像源,这个图像源只是将消息传递给浏览器来检索数据.

当点击以从其绘制画布时,浏览器可能不能真正访问进一步图像元素.我们只是告诉代码加载它,然后就完成了.

Change code to solve OP's facing chrome issue like below:

img.onload = function() {
  context.drawImage(img, 0, 50);// draws the image at the specified x and y location
}

UPDATE 4 JSFiddle
Make image position dynamic based on OP requirement.

Laravel相关问答推荐

try 编写一个函数,如果产品存在,则无法删除特定类别

URL类图中的循环关系

Laravel使用PostgreSQL显示";`try 读取null`上的属性,而不是SQL错误消息

我应该如何使用遵循依赖反转原则的工厂方法设计模式

到查询构建器的MySQL查询

Uncaught ReferenceError:未定义require[Shopify PHP React Browser问题]

找不到 Laravel 5 类日志(log)

laravel render() 方法有什么用?

自定义 Laravel 关系?

Laravel 重定向数据不工作

如何处理 Laravel 的 SMTP 驱动程序中的自签名 TLS 证书?

我怎样才能了解更多关于我的 Laravel 排队作业(job)失败的原因?

如何在没有 Artisan 的情况下运行 Laravel?

Laravel 动作未定义

Laravel 存储文件的公共 url

改变模式生成器中的列长度?

assets资源不引用公用文件夹(Laravel)

在 Laravel 中结合 AND/OR Eloquent的查询

laravel如何访问具有表编号名称的列?

如何以 JSON 格式发送 Laravel 错误响应