简体   繁体   English

在画布上绘制背景图像

[英]Draw a background image onto the canvas

I am trying to draw dom objects on to a canvas as shown below. 我正在尝试将dom对象绘制到画布上,如下所示。

https://developer.mozilla.org/en-US/docs/HTML/Canvas/Drawing_DOM_objects_into_a_canvas https://developer.mozilla.org/en-US/docs/HTML/Canvas/Drawing_DOM_objects_into_a_canvas

I have the below code and it appears as though background images don't work? 我有下面的代码,看起来好像背景图像不起作用? Am I doing something wrong here? 我在这里做错了吗? Or is there a way around it? 或者有办法吗?

See Fiddle 见小提琴

    <canvas id="canvas" style="border:2px solid black;" width="400" height="300"></canvas>
    <div style='background-color:red' class='test'><h1>Hello World</h1></div>
    <div style='background-image:url(http://lorempixel.com/400/100/transport/)' class='test'><h1>Hello World</h1></div>


    <script>
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    var data = "<svg xmlns='http://www.w3.org/2000/svg' width='500' height='500'>" +
                 "<foreignObject width='100%' height='100%'>" +
                   "<div xmlns='http://www.w3.org/1999/xhtml' style='font-size:40px'>" +
        "<div style='background-color:red' class='test'><p>Hello World</p></div>" +
         "<div style='background-image:url(http://lorempixel.com/500/500/transport/)' class='test'><p>Hello World</p></div>" +
                   "</div>" +
                 "</foreignObject>" +
               "</svg>";
    var DOMURL = self.URL || self.webkitURL || self;
    var img = new Image();
    var svg = new Blob([data], {type: "image/svg+xml;charset=utf-8"});
    var url = DOMURL.createObjectURL(svg);
    img.onload = function() {
        ctx.drawImage(img, 0, 0);
        DOMURL.revokeObjectURL(url);
    };
    img.src = url;
    </script>

Maybe https://developer.mozilla.org/en-US/docs/HTML/Canvas/Drawing_DOM_objects_into_a_canvas same domain security reason: 也许https://developer.mozilla.org/en-US/docs/HTML/Canvas/Drawing_DOM_objects_into_a_canvas同样的域名安全原因:

Security 安全

SVG images aren't allowed to load any external resources, for example, even ones that appear to be from the same domain. SVG图像不允许加载任何外部资源,例如,甚至是那些看起来来自同一域的资源。 Resources such as raster images (such as JPEG images) or s have to be inlined as data: URIs. 光栅图像(如JPEG图像)或s等资源必须作为数据内联:URI。

SVG images aren't allowed to load any external resources, for example, even ones that appear to be from the same domain. SVG图像不允许加载任何外部资源,例如,甚至是那些看起来来自同一域的资源。 Resources such as raster images (such as JPEG images) or s have to be inlined as data: URIs. 光栅图像(如JPEG图像)或s等资源必须作为数据内联:URI。

This is from the page you linked. 这是您链接的页面。 This confirms that you can't use background images unless you convert them to data URIs. 这确认您不能使用背景图像,除非您将它们转换为data URI。 In itself, that's not too hard, all you have to do is copy the image to another canvas and use toDataURL() on it. 本身,这不是太难 ,你所要做的就是将图像复制到另一个画布并在其上使用toDataURL() However you will need to scan the input for any such images and it will still only work on same-domain resources. 但是,您需要扫描任何此类图像的输入,它仍然只能在同一域资源上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM