Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 949
Get Off My Lawn Avatar asked Oct 23 '25 10:10

Get Off My Lawn


2 Answers

You should be able to create the element with JavaScript:

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
like image 90
ryanbrill Avatar answered Oct 25 '25 23:10

ryanbrill


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.

like image 32
Bergi Avatar answered Oct 25 '25 23:10

Bergi