每当我尝试运行此操作时,我都会在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.
为什么!?
答案 0 :(得分:1)
您的代码似乎很好。请检查以下内容:
canvas
; drawing
元素
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 );