使用todataurl时输入错误

时间:2012-08-19 16:54:30

标签: javascript jquery canvas

每当我尝试运行此操作时,我都会在chrome中遇到类型错误:代码更新

// Get the drawing canvas
canvas = $('#drawing');
context = canvas[0].getContext('2d');

function resizeCanvas() {

    canvas.attr({'width' : $(window).width()});
    canvas.attr({'height' : $(window).height() - 158});

    var dataURL = canvas[0].toDataURL('image/png');
    context.drawImage(dataURL, 0, 0);

}

$(window).resize(function() {
    resizeCanvas();
}); resizeCanvas();

// Various event handlers after this.

为什么!?

2 个答案:

答案 0 :(得分:1)

您的代码似乎很好。请检查以下内容:

  1. 存在标识为canvas;
  2. drawing元素
  3. 您的Chrome版本支持canvas

答案 1 :(得分:1)

drawImage获取图像或画布参考,您传递的是字符串。

例如:

var a = document.createElement("canvas");
a.getContext("2d").drawImage("asdasdasd", 0, 0 )
//TypeError: Type error

你可以尝试:

context.drawImage(canvas[0], 0, 0);

或者

var image = new Image();
image.src = dataURL;
context.drawImage( image, 0, 0 );