We are about to switch to a new forum software. Until then we have removed the registration on this forum.
In short I am attempting to pass numbers (received from an api) into the draw function to be printed onto an instance of a canvas, when the user's mouseIsPressed. Essentially I need to figure out how to know if a value is being passed into the draw function or not. Below is a very simple example of what I am trying to accomplish.
function setup() {
createCanvas(1000, 600);
}
function draw(Lat, Long) {
if (Lat == 'undefined') {
console.log(2);
} else {
console.log(3);
}
}
I'd like the console to print 2 if there are no values being passed, but when this sketch runs, 3 is logged to the console. Sorry for the noob javascript question, any insight would be appreciated. Also, if this sample code (dumbed down version of what I am trying to accomplish) is not enough, let me know and I will post my exact code. Thanks!
Answers
ADVICE: As for convention, only use UpperCamelCase names for classes & constructor functions. Therefore, parameters Lat & Long should be renamed as lat & lon, following LowerCamelCase naming convention. L-)
let lat, lon;
Another non-conventional approach is adding them as properties of draw().
So you kinda make them somewhat "local" to that callback. :ar!
I've made a simple example demoing it. Go to the link below to see it running online: O:-)
http://p5js.SketchPad.cc/sp/pad/view/ro.C4DoFhUDGlhV0s/latest
@GoToLoop
I ran your code and there is a bug. I fixed it by doing the following modification:
The reason of this bug is that
draw.lon
is not being init or updated in the sketch. Maybe you have a better idea of how to fix it as a see mine more of a hack.Kf
Which bug? It runs fine on me, both online & locally. :-/
Under OP's original post, it checks for undefined:
if (Lat == 'undefined') console.log(2);
So not being initialized is also expected and dealt w/. :-j
Merging first post and your post will do it.
Kf
@GoToLoop can you elaborate more on line 14. Every time mouseIsPressed, the canvas is being drawn but passed the parameter of latitude? Also, can you explain
text(draw.lat + '\n' + draw.lon, width>>1, height>>1);
I understand the text function but I do not understand the function of draw.lat and draw.lon. The API I am using is here, http://api.open-notify.org/iss-now.json. I am attempting to have the JSON data refresh every second and update to the text function. What I have is below.
HTML
JS (Painthead.js)
This works the first time the user's mouseIsPressed. The lat and long update. If the user lets go of mouse and attempts to press their mouse again, to reveal the lat and long values, an error
is received. I am assuming this is because at some point the draw function is attempting to print the vals of lat and long, but they are not defined because the callback from p.GetValue has not had time to execute, or there is some discontinuity there. I am aware of the loadJSON function here
https://p5js.org/reference/#/p5/loadJSON
but am not sure how to update this information every second the draw loop is executed, as I want this information updated while the user is viewing. Sorry for the long post. Thanks for the help @GoToLoop and @kfrajerI was checking the reference: https://p5js.org/reference/#/p5/draw
and draw() has no parameters. So this is the first thing to modify in your code. Also notice that you are calling p.GetValue inside draw(lat,lon) and in the former function you are calling p.draw(lat,lon) making this a recursive call. This behavior is probably not desired in your application. I might suggest the following alternate version, which is untested:
Remove line 16.
Update your calls of draw to p.draw().
Now every time p.GetValue(); is called, it will update the global values of lat and lon. These values are then used by the p.draw() function.
Kf
@kfrajer perfect! Thank you. Needed a reminder in scope! Thanks again.