I'm probably missing something very obvious here. I can use p5.js in global mode and use constants for textAlign with no problem, e.g. CENTER.
Here is global mode code where it works fine:
function setup() {
var canvas = createCanvas(720, 400);
canvas.parent('main_canvas');
};
function draw() {
textSize(32);
textAlign(CENTER);
text("word", 50, 50);
};
However when I try using CENTER in instance mode I get:
Uncaught ReferenceError: CENTER is not defined:
Here is instance mode code where it fails:
var s = function (p) {
p.setup = function() {
p.createCanvas(720, 400);
};
p.draw = function() {
p.textSize(32);
p.textAlign(CENTER);
p.text("word", 50, 50);
};
};
var myp5 = new p5(s,'main_canvas');
Any ideas on what I am missing here?
In global mode, all the P5.js functions and variables are added to the global namespace. In instance mode, all the P5.js functions and variables are added to the variable passed into the sketch function (in your case, your p
variable).
To use the CENTER
variable, you have to get at it through the p
variable.
In other words, you need to do this:
p.textAlign(p.CENTER);
You'll also have to do this with other variables, like mouseX
and mouseY
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With