我正在尝试创建一个代码,在屏幕上以随机颜色随机绘制笑脸,该笑脸应该循环,但我不知道如何最好地使用 random() 函数来完成此操作。谁能给我一些指点!我尝试过使用变量(绘图函数内的 var 来调用函数 smileyFace 但没有运气!
代码:
function setup() {
createCanvas(400, 400);
background(220);
smileyFace(random(0, 400), random(0, 400));
}
function draw() {
}
function smileyFace(x, y) {
fill(250);
ellipse(x, y, 60, 60);
fill(255);
ellipse(x - 10, y - 10, 10, 10);
ellipse(x + 10, y - 10, 10, 10);
arc(x, y + 5, 30, 25, 0, PI, CHORD);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
最佳答案
[...] multiple smiley faces drawn on one canvas random colours (r,g,b) and placed at random on screen.
只需将调用 smileyFace
移至 draw
函数并通过以下方式创建随机颜色
c = color(random(0, 255), random(0, 255), random(0, 255));
参见示例:
function setup() {
createCanvas(400, 400);
background(220);
}
function draw() {
c = color(random(0, 255), random(0, 255), random(0, 255));
smileyFace(random(0, 400), random(0, 400), c);
}
function smileyFace(x, y, c) {
fill(c);
ellipse(x, y, 60, 60);
fill(255);
ellipse(x - 10, y - 10, 10, 10);
ellipse(x + 10, y - 10, 10, 10);
arc(x, y + 5, 30, 25, 0, PI, CHORD);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
关于javascript - 在 p5.js 中使用 random() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58236354/