const randomizer = {
"rgb1": random(0, 255),
}
draw = () => {
let y = random(0, height);
let x = random(0, width);
noStroke();
fill(randomizer["rgb1"], randomizer["rgb1"], randomizer["rgb1"]);
ellipse(x, y, 15, 50);
}
I’ve made a program that draws ellipses in random positions. However, I cannot understand why the randomizer object is not returning a value, even more so no ellipses are being drawn on the canvas. With that said, the program worked when I defined and used a variable with random(0, 255) instead.
(since you use the same value for fill() three times, it should always be a gray color…?)
That’s what I thought. Although, it’s not drawing any ellipses let alone filling them with colors. I’ll just keep it simple for now. Thanks for your input, though
this means rgb1 was 0 probably… so black ellipse, you don’t see anything
Full console:
sketch.js
warning in line10 :['rgb1'] is better written in dot notation.
warning in line10 :['rgb1'] is better written in dot notation.
warning in line10 :['rgb1'] is better written in dot notation.
Current lineline 12
Console
ReferenceError: random is not defined (sketch: line 2)
p5.js says: There’s an error due to “random” not being defined in the current scope (on line 72 in about:srcdoc [about:srcdoc:72:5]).
If you have defined it in your code, you should check its scope, spelling, and letter-casing (JavaScript is case-sensitive). For more:
Did you just try to use p5.js’s random() function? If so, you may want to move it into your sketch’s setup() function.
In general the p5*js API is only ready to use when preload() is called back.
Invoking p5 methods, such as random(), when declaring global variables would fail, b/c they’re still unavailable in the global context.
If you think you need to access p5 properties before callback preload() you can use the hack trick from the sketch link below:
window.mocha = 'Hack to block p5.js auto global instantiation.';
new p5; // Prematurely instantiates p5.js so its props are available now.
window._setupDone = undefined; // Disables duplicate instantiation warn.