问题描述
我希望能够从构造函数中创建Canvas元素,以便我可以创建这样的函数。
I would like to be able to make Canvas elements from the constructor so that I could make a function like this.
function createCanvasContext(height,width)
{
var body = document.getElementsById('body')[0];
var canvas = new Canvas();
canvas.height=height;
canvas.width = width;
var context = canvas.getContext('2d');
body.appendChild(canvas);
return context;
}
我在行var canvas = new Canvas()说出错了'画布未定义'HTML5不允许从构造函数创建元素吗?或者是否需要传递给构造函数的参数。任何想法都会很棒。
I get an error at the line var canvas = new Canvas() saying that 'Canvas is undefined' does HTML5 not allow creating elements from the constructor? or are there parameters that I need to pass to the constructor. Any ideas would be great.
推荐答案
虽然你可以做 new Image()
就好了,新的Canvas()
不是什么东西! Canvas
虽然 HTMLCanvasElement
是,但它甚至都不是。尽管如此,你不能使用它的构造函数。
While you can do new Image()
just fine, new Canvas()
isn't a thing! Canvas
Isn't even a thing, though HTMLCanvasElement
is. Nonetheless you cannot use its constructor.
document.createElement('canvas');
就是你想要的。你必须使用它,就像使用div一样。
document.createElement('canvas');
is what you want. You have to use that, just like with divs.
这篇关于可以从Canvas构造函数创建HTML5 Canvas元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!