We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello guys, I need some help to get started with the p5.js libriary
The following code will display the clock, and color the text, using the current clock(ss/mm/hh) as a hex value. I first wrote the code in processing where it works. Now I want to execute it as p5 however, after changing the syntax to fit the p5 language, it dosen't work. I can't figure out why, tho I suspect the "var" instead of "string" to be an possible issue?
Can you guys explain why it dosen't work?
p5.js code:
var s,m,h;
function setup() {
createCanvas(800,600);
}
function draw() {
s = second();
m = minute();
h = hour();
background(30);
var s_ = nf(s,2);
var m_ = nf(m,2);
var h_ = nf(h,2);
var hex_ = "FF" + s_+ m_+ h_;
var c = unhex(hex_);
fill(c);
textSize(30);
text("#",width/2-70,height/2);
text(s_,width/2-40,height/2);
text(m_,width/2,height/2);
text(h_,width/2+40,height/2);
}
Same programme, just written in processing and WORKS:
int s, m, h;
void setup(){
size(800,600);
}
void draw(){
s = second();
m = minute();
h = hour();
background(200);
String s_ = nf(s,2);
String m_ = nf(m,2);
String h_ = nf(h,2);
String hex_ = "FF" + s_+ m_+ h_;
int c = unhex(hex_);
fill(c);
textSize(30);
text("#",width/2-70,height/2);
text(s_,width/2-40,height/2);
text(m_,width/2,height/2);
text(h_,width/2+40,height/2);
}
Answers
http://p5ide.HerokuApp.com/editor#?sketch=57b6ca4507ebfa0300553e0e
https://GitHub.com/processing/p5.js/wiki/Processing-transition
Thx for the links, however the Processing-translation section dosen't seem to cover my problem?
When I print hex_ to the console in the p5 sketch, it appears as a string, which it should. But when I parse the string into the unhex(); it doesn't seem to covert the same way as processing do. I have read the referencers for unhex() and it seems to be the same as in processing, so I can't really locate the problem..
https://p5js.org/reference/#/p5/unhex
Running side-by-side Java Mode & p5.js sketches, I've got the same result here! :-@
Have you actually run the p5.js sketch from HerokuApp's link I posted above? /:)
Yeah I tried the HerokuApp link you posted, and with the same result as when I run it in the P5 IDE. The color still dont change?
So there's some misunderstanding here! I thought the Java Mode sketch was the model behavior you wanted to convert to p5.js? :-/ Now it turns out it's also incorrect?! 8-}
If text written using the fill colour or the stroke colour?
Or both?
You also might want to map 24 / 60 / 60 each to the range 0 - 255 to get the full range of colours. The ones you are using are all quite dark.
There'll also be a discontinuity in your colours because it'll go from 0x09 (9 in base 10) to 0x10 (16 on base 10). You are using decimal numbers as hex numbers.
Generally I'd drop unhex completely and do something different.
I posted two sketches. One made in processing/java mode, and the other one translated into P5. The processing sketch works as it should, using the time stamp as the hex color value to color the text.
The P5 sketch does not color anything. Its like the time value isn't converted to a color value. - And thats my problem. The time isn't changing color of the text.
http://p5ide.HerokuApp.com/editor#?sketch=57b9b0b3e1da7a03006db21b