0

我使用以下代码将 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]

做错了什么?

4

1 回答 1

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";

归功于Ian:NS_ERROR_NOT_AVAILABLE:组件不可用

于 2013-12-07T13:57:41.887 回答