Get Off My Lawn
Get Off My Lawn

Reputation: 36299

JavaScript Canvas object

I want to create a canvas object in memory, and not require a HTML <canvas> tag. Is this possible?

With this code:

var canvas = new Canvas();
var ctx = canvas.getContext('2d');

I get this error message: Uncaught ReferenceError: Canvas is not defined

Upvotes: 4

Views: 17598

Answers (2)

ryanbrill
ryanbrill

Reputation: 2011

You should be able to create the element with JavaScript:

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');

Upvotes: 10

Bergi
Bergi

Reputation: 664297

Use document.createElement("canvas") instead. There is no Canvas constructor for canvases, as you know it from Image for images or Option for options - those are the sole exceptions.

Upvotes: 4

Related Questions