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. After I insert the HTML data, I can't access the canvas' image data anymore though (in Chrome):
//This function is a direct copy of http://ift.tt/1wM4r2W
onload = function() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var data = '<svg xmlns="http://ift.tt/nvqhV5" width="200" height="200">' +
'<foreignObject width="100%" height="100%">' +
'<div xmlns="http://ift.tt/lH0Osb" 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?
Aucun commentaire:
Enregistrer un commentaire