[英]Inserting HTML into canvas without creating CORS restriction
I am trying to get the image data from a canvas to which I added a HTML element's data by using MDN's method . 我试图从画布中获取图像数据,我使用MDN的方法向其中添加了HTML元素的数据。 After I insert the HTML data, I can't access the canvas' image data anymore though (in Chrome):
插入HTML数据后,我无法再访问画布的图像数据(在Chrome中):
//This function is a direct copy of https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Drawing_DOM_objects_into_a_canvas#JavaScript onload = function() { var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var data = '<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">' + '<foreignObject width="100%" height="100%">' + '<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:14px">' + 'example' + '</div>' + '</foreignObject>' + '</svg>'; var DOMURL = window.URL || window.webkitURL || window; 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); getData(ctx); }; img.src = url; }; //the part where it goes wrong: function getData(ctx) { try { ctx.getImageData(0,0,100,100); } catch(e) { alert(e); } }
<canvas id="canvas"></canvas>
My question is: is it possible to insert the HTML data, and still be able to access the image data? 我的问题是:是否可以插入HTML数据,仍然可以访问图像数据?
After looking around a bit more, I found out that data:
URIs do still permit access to image data after inserting the exact same bit of HTML. 看了一下之后,我发现了
data:
在插入完全相同的HTML后,URI仍允许访问图像数据。 Even though data:
URIs are slower than blob
s, this does seem like the best way to go here. 即使
data:
URI比blob
慢,这似乎是最好的方式。
onload = function() { var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var data = '<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">' + '<foreignObject width="100%" height="100%">' + '<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:14px">' + 'example' + '</div>' + '</foreignObject>' + '</svg>'; var DOMURL = window.URL || window.webkitURL || window; var img = new Image(); var url = 'data:image/svg+xml;base64,' + btoa(data); img.onload = function () { ctx.drawImage(img, 0, 0); DOMURL.revokeObjectURL(url); getData(ctx); }; img.src = url; }; function getData(ctx) { try { ctx.getImageData(0,0,100,100); alert('Finished without errors'); } catch(e) { alert(e); } }
<canvas id="canvas"></canvas>
No, the canvas becomes tainted with the SVG and therefore context.getImageData
is no longer allowed. 不,画布变得受SVG污染,因此不再允许
context.getImageData
。 This has become necessary because of cross-domain security issues. 由于跨域安全问题,这已成为必要。
The (sometimes unsatisfactory) workaround is to bounce the html off your server and use a headless browser (I recommend PhantomJS) to convert the html into an image. (有时令人不满意的)解决方法是将html从服务器上弹出并使用无头浏览器(我建议使用PhantomJS)将html转换为图像。 Then serve that image back to the client.
然后将该图像提供给客户端。 Since the html-as-image is not cross-domain, it will not taint the canvas and you can use
context.getImageData
. 由于html-as-image不是跨域的,因此它不会污染画布,您可以使用
context.getImageData
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.