We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, Im having some issues with the textSize function when using it multiple times in a single Canvas in namespacing mode. Wondered if anyone has any ideas where I'm going wrong.
Apologies for any messy/poorly written code, this is my first p5 programme.
Code Snippet:
var sketch5 = function(p) {
p.peopleCount;
p.currentValue;
p.textExample;
p.typeChoice;
p.mappedFigure;
p.setup = function() {
var peopleCanvas = p.createCanvas(200, 300);
peopleCanvas.parent('canvasHold');
calport27 = p.loadFont('fonts/ASoPMFCalport-Circle2.7.ttf', p.setType);
p.background(255);
p.peopleCount = 0;
p.buttonIn = p.createButton('IN');
p.buttonOut = p.createButton('OUT');
p.buttonIn.position(980, 30);
p.buttonOut.position(980, 70);
p.fill(0);
p.buttonIn.id('buttonIn');
p.buttonOut.id('buttonOut');
p.buttonIn.mousePressed(p.doCountPlus);
p.buttonOut.mousePressed(p.doCountMinus);
p.buttonIn.style("background-color", buttonColour);
p.buttonOut.style("background-color", buttonColour);
p.buttonIn.style("z-index", "100000");
p.buttonOut.style("z-index", "100000");
}
p.setType = function() {
p.fill(0);
currentTF = calport27 ;
p.textFont("ASop MF Calport");
p.currentValue = p.text("Visitors: " + p.peopleCount, 15, 270);
p.textAlign(p.LEFT, p.BASELINE);
p.mappedFigure = p.text("Type Frequency: " + '2.7', 15, 285);
p.textSize(12);
p.text('people', 15, 20);
p.textSize(27);
p.textAlign(p.CENTER, p.CENTER);
p.textFont(calport27);
p.textExample = p.text('A SUM\nOf\nPARTS', 100, 115);
}
p.doCountPlus = function() {
p.peopleCount++;
p.typeCheck();
}
p.doCountMinus = function() {
if (p.peopleCount > 0) {
p.peopleCount--;
}
p.typeCheck();
}
p.typeCheck = function() {
p.typeChoice = p.map(p.peopleCount, 0, 100, 2.7, 0.3);
p.rounded = Math.round(p.typeChoice * 10) / 10;
if (p.rounded < 0.3) {
p.rounded = 0.3;
}
var fixed = p.rounded.toFixed(1);
currentTF = window['calport' + fixed*10] ;
visitors = p.peopleCount;
typeFreq = p.rounded;
p.background(255);
p.textAlign(p.LEFT, p.BASELINE);
p.textSize(12);
p.textFont("ASop MF Calport");
p.text("people", 15, 20);
p.text("Type Frequency: " + p.rounded, 15, 285);
p.text("Visitors: " + p.peopleCount, 15, 270);
p.textAlign(p.CENTER, p.CENTER);
p.textSize(27);
p.textFont(currentTF);
p.text('A SUM\nOf\nPARTS', 100, 115);
}
p.draw = function() {
}
}
var people = new p5(sketch5);
The issue is that when the typeCheck function runs (after pressing in or out buttons) that the title and data text take the bigger text size specified later in the function for the "A Sum of Parts" text and not the smaller text size specified right before their text functions.
So basically I am trying trying to create 5 separate sketches that then share some variables that inform each other, which is all working okay locally but the video and audio inputs are not being gathered when on my server. Would this be because they need to be running on a https:// url?
Any help would be greatly appreciated, thanks!
Answers
This is just a cleanup attempt. I haven't tested it at all since I don't have those fonts! :(|)
Brilliant, thanks! Took a few tweaks but that seems to have fixed the textSize issue.
Now to go back through the rest of it and make it look as nice as your part haha! Lots of quicker ways to do things I can get from that.
Any ideas on why it is not asking permission for or trying to gathering the video and audio inputs? I've assumed its due to needing an SSL certificate but would rather not pay for that if I don't need to!
Check for output in your browser console: it should inform you if it's blocking AJAX requests and why; or alternatively will list any requests that failed for other reasons...
Well, I've peeked @ http://CallumStrachan.co.uk/aproductof/combined/sketch.js in order to know what those fixes were about. ;))
Exported back those to the posted code here as version 2.1. :ar!
Ah, and don't forget to transpile it to ES5 via https://BabelJS.io/repl/, OK?. L-)
You guys are crazy efficient, its actually scary! =D> haha
the color functions at 39,40 needed p. infront though :>
Did you notice it had happened again in the time programme then? X_X any ideas how to fix it in there? Seems to be when I use the custom fonts it just ignores the textSize :((
And yeah sure will remember to use that when it's ready to go live properly just uploading it to test for now!
In regards to the video/sound input issue, checked the console and it says getUserMedia() no longer works on insecure origins. So I guess it is in need of a SSL then!
Thanks for all the help so far code lords! ^:)^ ^:)^ ^:)^
Edit: Also should I be worried that you can access those files? I should take another look at my file permissions maybe :))
Oops, forgot about those! X_X In order to access any of p5.js' API we need the sketch's p5's reference.
In this case, parameter p holds it. :-B Anyways, fixed it now as of version 2.1.1. O:-)
Since my tweaked version 2.1, 'fonts/ASoPMFCalport-Regular.ttf' is being loaded via loadFont() too.
And when I go to your sketch's URL, and then paste my own version in the console, thus instantiating another "People" sketch canvas, textSize() is clearly working for me for 'People', 'Frequency: ' & 'Visitors: ' strings. :>
Oh yeah sure the "people" canvas is fine, its the "time" canvas that I having the problem with now :(
The clock in the middle should be center aligned and bigger, similar to the "a sum of parts" text.
The idea was to set the typeface by the people amount and then serve that to the clock so they match up via a global variable, but when I do that the seems to revert to size 12 and left aligned. #-o
Looking at your sketch code I see:
Note that in this context
p
is a reference to the p5 instance; but you appear to be using it to store arbitrary sketch variables and functions by adding them as ad-hoc properties/methods ofp
. This is very BAD...At some point one of your property/function names will collide with an internal p5js property/method and all hell will break loose; the world will end and Trump will become president... oh shhhii... :-&
You can safely set them as function level variables since they'll only exist within the scope of the individual sketch function:
Or, if you must, you can create your own object to store sketch variables:
I'm not going to try debugging your code further as it may be just such a collision that is causing your problems...
Are you serious? If you put anything on the web your dirty laundry is there for everyone to see :P
Ah I see, I learn't the instance mode stuff from the shiffman videos on youtube, must have misunderstood the variable part. Apologies! :(
oh no we dont want that @-)
Have gone through and changed all of the variables that were using the p. and the problem is still persisting with the clock text ~X(
I think its being caused by it drawing from the font that is saved in a global variable. As when I remove that the text is the correct size and alignment.
Is there any better way I can pass over what font to use from the "people" sketch to the "time" one?
EDIT:
I've managed to fix it by loading the fonts array within both the sketches and then just sharing the fontIdx as a global variable rather than the whole array. Probably a bit messy but its working!
Thanks guys! [-O< :D
Looking at the p5 source it runs the following when setting a font size:
this._renderer._setProperty('_textFont', theFont);
I haven't followed through further; but the fact that it accepts a reference to
theFont
suggests that it applies changes directly to the stored font reference. In that case changes made to the font in one sketch would obviously affect the other sketches...Here's some template plan to tidy it up. :bz
Although I'm not much confident about it b/c I'm suspicious that textSize() might be shared for the same p5.Font. :-/
P.S.: Seems like @blindfish shares my worries about it too. ^#(^