I found processing.js just a few days ago, I am struggling a lot with it. I am trying to make a canvas appear at the moment I press a button. For example, this code works, and it creates a canvas after the click:
<!DOCTYPE HTML>
<html>
<script src="/js/processing.js"></script>
<head>
</head>
<body>
<input type="button" value="Cambiar imagen" id="clearbutton"
onclick="canvas();">
<script>
function canvas(){
var mycanvas = document.createElement("canvas");
mycanvas.id = "mycanvas";
document.body.appendChild(mycanvas); }
</script>
</body>
</html>
I then tried changing the code like this:
function canvas(){
var mycanvas = document.createElement("canvas");
mycanvas.id = "mycanvas";
document.body.appendChild(mycanvas);
mycanvas.data-processing-sources = "/var/www/html/processing/jS2.pde"
}
but it throws an error that says: invalid left-hand sign in asigment.
So then I tried something like:
<script type="text/processing" data-processing-target="mycanvas">
void setup()
{
size(300,300);
background(0);
fill(255);
noLoop();
PFont fontA = loadFont("courier");
textFont(fontA, 14);
}
void draw(){
text("Hello!",20,20);
}
</script>
But this just creates a white canvas, and it ignores the processing script. I am not that great at javascript either, so if you answer, thanks. I have checked the reference files to processing.js, but its been tough to follow it.
Following the suggestion in the comment, I tried:
<input type="button" value="Cambiar imagen" id="clearbutton" onclick="canvas();">
<script>
function canvas(){
var mycanvas = document.createElement("canvas");
mycanvas.id = "mycanvas";
document.body.appendChild(mycanvas);
var canvas = "/var/www/html/processing/jS2.pde"
var sources = canvas.getAttribute("data-processing-sources").split(/\s+/);
Processing.loadSketchFromSources(canvas, sources);
}
</script>
This gives an error that says: canvas.getAttribute is not a function at canvas.
And tried:
<body>
<input type="button" value="Cambiar imagen" id="clearbutton" onclick="loadSketchOnContentSwap();">
<script>
function loadSketchOnContentSwap() {
var canvas = "/var/www/html/processing/jS2.pde"
var sources = canvas.getAttribute("data-processing-sources").split(/\s+/);
Processing.loadSketchFromSources(canvas, sources);
}
</script>
</body>
Maybe I missed something or did not put the code in the correct place?
This is the link to the fiddle: https://jsfiddle.net/truxx/khht29zx/1/
/There is a problem with it though, this code creates a grey canvas in my chrome browser, but the same code doesn't do this on the fiddle. /