62

I would like to be able to make Canvas elements from the constructor so that I could make a function like this.

https://hacknite.vercel.app/mishal

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.

CC BY-SA 3.0
1
  • 4
    There is no Canvas constructor. If you want to ceate a new <canvas> element, use var canvas = document.createElement("canvas");. *PS. This answer seems to solve your previous question. Use the Accept answer feature to mark the answer as the right one.
    – Rob W
    Nov 30, 2011 at 18:34

2 Answers 2

107

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');

is what you want. You have to use that, just like with divs.

CC BY-SA 4.0
67
var mycanvas = document.createElement("canvas");
mycanvas.id = "mycanvas";
document.body.appendChild(mycanvas);
CC BY-SA 3.0
7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.