views:

27

answers:

2

I get Error: 'CanvasElement' is undefined when trying to run the following code using the javascript port of processing.

var p = Processing(CanvasElement);
    p.size(100, 100);
    p.background(0);
    p.fill(255);
    p.ellipse(50, 50, 50, 50);

Any help with this would be appreciated.

A: 

Where is CanvasElement defined? It sounds like the code you posted is executing before the code that defines CanvasElement.

Andrew Hare
Based on google CanvasElement is part of the html 5 standard, so how would I pass that into the function? The processing() function is included in a javascript file earlier ubt CanvasElement is not in that file.
Jared
Ah good question! Matthew's answer is what you are looking for.
Andrew Hare
+2  A: 

You need to have a canvas element somewhere on the page that you can select:

<canvas id="theCanvas" width="500" height="500"></canvas>

Then you can select the element however you want and pass it (or its ID) to the Processing function:

var p = Processing(document.getElementById("theCanvas")); // or Processing("theCanvas")
p.size(100, 100);
p.background(0);
p.fill(255);
p.ellipse(50, 50, 50, 50);
Matthew Crumley