我使用以下代码将div内容保存为imgae:
function saveDiv() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var mydiv = $(".mydivclass").html();
var data = "data:image/svg+xml," +
"<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>" +
"<foreignObject width='100%' height='100%'>" +
mydiv +
"</foreignObject>" +
"</svg>";
var img = new Image();
img.src = data;
img.onload = function () {
ctx.drawImage(img, 0, 0);
}
}
当我调用该函数时,得到以下内容:
NS_ERROR_NOT_AVAILABLE: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMCanvasRenderingContext2D.drawImage]
出了什么问题?
答案 0 :(得分:1)
在Iceweasel 17.0.10
我有类似你的问题。
这是我使用的代码。我的代码几乎与此相同:http://www.w3schools.com/tags/canvas_drawimage.asp
<style>
#canvasbg {
background:url('image.jpg')
}
</style>
</head><body>
<div id="canvasbg"><br></div>
<canvas></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
var img = document.getElementById('canvasbg');
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
</script>
我也得到错误:
NS_ERROR_NOT_AVAILABLE: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMCanvasRenderingContext2D.drawImage] @ file:///home/tijs/Development/html/js/canvas.js:35
编辑: 固定! 问题是在您尝试使用图像之前尚未加载图像。 试试这个:
var img = new Image();
img.onload = function () {
this._canvas.drawImage(img, 300, 300);// this is line 14
};
img.src = "images/logo.jpg";