We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello there,
I am trying to reproduce an old sketch in p5js but I ran into troubles...
Here is the code
var fontRegular, fontBold
var offset = 20
var z = 0.0
function preload(){
fontRegular = loadFont("assets/Regular.otf")
fontBold = loadFont("assets/Bold.otf")
}
function setup() {
createCanvas(windowWidth, windowHeight)
colorMode(HSB,360, 100, 100);
}
function draw() {
background(5)
var chars = "+ — I O"
var splitString = split(chars, " ");
textFont(fontRegular);
fill(0,0,100).strokeWeight(0).textSize(20)
text(s, 10, 10, 200, 80)
for ( var x = offset; x < width - offset; x += offset){
for (var y = offset; y < height - offset; y += offset){
var nl = map(noise(z, x/450.0, y/450.0), 0, 1, 0, 3)
text(chars[nl], x, y)
}
}
z = z + .0006
}
Console is given me an error from the p5.js library: p5.js:12854 Uncaught TypeError: Cannot read property 'toString' of undefined
This is the original sketch that works perfectly in Processing:
float z = 0.0;
PFont myFont;
int offset = 18;
void setup(){
size(500,700);
pixelDensity(displayDensity());
//colorMode(HSB, 360,300,360);
noStroke();
myFont = createFont("MaisonNeue-Bold", 10);
textFont(myFont);
textAlign(CENTER, CENTER);
}
void draw() {
background(255);
String[] chars = split("+ — I O + C", ' ');
for ( int x = offset*3; x < width-offset*2; x +=offset){
for ( int y = offset*3; y < height-offset*2; y += offset){
int nl = (int) map(noise(z, x / 450.0, y / 450.0), 0, 1, 0, 7);
fill(0);
rectMode(CENTER);
if ((y - 16) % (2) == 0){
text(chars[nl], x,y );}
else{
text(chars[nl], x + 8 ,y);
}
}
}
z = z + .006;
}
I'm guessing I am doing something wrong with arrays in p5js...
Any suggestions?
Thank you!
Answers
First of all, your sketch won't run because
s
isn't defined. (While you're at it, might as well get rid of those fonts as well, since they have nothing to do with the problem and just get in the way.)But after I define
s
(I just set it to "abc") then I see the error in the JavaScript console.On the same line as the error, I also see a link to p5.js:15538. I click that to determine that the problem is in the
text()
function. That tells me that something is wrong withchars[nl]
.Okay, so knowing that, I print out the value of
nl
. Turns out it's1.4792704460346342
. Does this make sense?Probably not. Maybe you meant to convert
nl
into anint
before using it as an array index?Notice that you do this in the original code:
You might want to check out the
int()
function.var nl = map(noise(z, x/450, y/450), 0, 1, 0, 3)
There are many ways in order to truncate the fractional part of a number in JS. 2 of my favorites are:
~~
->const nl = ~~map(noise(z, x/450, y/450), 0, 1, 0, 3)
| 0
->const nl = map(noise(z, x/450, y/450), 0, 1, 0, 3) | 0
wow!
thank you so much for the help.
@GoToLoop now I wonder if these ways to truncate the fractional part are rounding my numbers up or down
The
int()
function is designed for exactly this purpose, and it matches the behavior of the original code. No point making it more complicated and harder to read.Truncating is not rounding. It's just chopping off the deimcal part. If you want to round, then use the
round()
function. If you always want to round up, then just add1
and then truncate using theint()
function. (You might want to add a check for the case where it's already anint
value.)@KevinWorkman but the thing is there is not
round( )
orint( )
function in p5js isn't? or I am missing something...@alexdontsurf Please click the link I provided in my post, which takes you to the P5.js reference on the
round()
function.Alternatively you could just google "JavaScript round", since P5.js is JavaScript.
Here's the link again: https://p5js.org/reference/#/p5/round
More generally, the reference should be your first stop for questions like this.
I did a really bad search, thank again Kevin
Those 2 techniques erase the fractional part completely w/ excellent performance! >:/
Even something as
4.999999999999999
becomes4
! @-)For rounding numbers, there are round(), floor(), ceil().
Other more JS specific includes parseInt() & Number.parseInt():
@GoToLoop Yes, that's the definition of truncating. That's what the original code does by casting to
int
, and that's what theint()
function does. I'm not sure what your point is?How about completeness for starters? So the OP can choose which 1 to use? :-\"
delete this comment
Hmm.... you've deleted your latest post. So it seems you've got it working already, right? >-)
However, since I was already trying to solve it as well, gonna leave my own version posted here. :-\"
1st 1 is the tweaked Java version (also compatible w/ Pjs), which is the basis for the p5.js version: :>
Now the p5.js itself. It can be watched running online at the link below too: :bz
http://p5js.SketchPad.cc/sp/pad/view/ro.G$G4q68Ayur/latest
index.html:
NoisyCharGrid.js: