如何产生随机数量(有限制)的这些“星星”(椭圆)?

function setup() {

  createCanvas(5000, 2100);

  randomX = random(100, 1000)

  randomY = random(100, 1000)

  randomSpeed = random(1, 10)

  randomSize = random(10, 100)

}

 function draw() {

      background(0);

      fill(255)

      ellipse(randomX, randomY, randomSize)

      randomX = randomX + randomSpeed

      if (randomX > 5000) {

        randomX = 0

          }

}

在 draw() 函数中,我需要在画布上随机绘制一个椭圆,有限制地在画布上绘制以制作星夜效果,我该怎么做?


幕布斯7119047
浏览 123回答 1
1回答

斯蒂芬大帝

如果我没理解错的话,您想在画布上随机位置绘制一串椭圆。我已经回答假设这就是你要问的。如果这不是您想要的,我们深表歉意。这个程序所做的是创建两个列表来保存关于省略号的数据。我们选择setup()随机数量的椭圆来绘制。我们制作了那么多随机大小和位置,然后将它们放入列表中。当需要绘制椭圆时,我们循环遍历包含有关它们的信息的列表,并使用它来绘制许多椭圆。const ellipseMinSize = 1;const ellipseMaxSize = 10;const ellipseMinAmount = 10;const ellipseMaxAmount = 100;// Create some lists so we can remember where the ellipses are and how big they arevar ellipseSizes = [];var ellipsePositions = [];function setup() {&nbsp; &nbsp; createCanvas(500, 500);&nbsp; &nbsp; // Choose an amount of ellipses to make&nbsp; &nbsp; var ellipseAmount = random(ellipseMinAmount, ellipseMaxAmount);&nbsp; &nbsp; for (var i = 0; i < ellipseAmount; i ++) {&nbsp; &nbsp; &nbsp; &nbsp; // Choose a random size and position, then remember those&nbsp; &nbsp; &nbsp; &nbsp; var ellipseSize = random(ellipseMinSize, ellipseMaxSize);&nbsp; &nbsp; &nbsp; &nbsp; var ellipsePosition = createVector(random(0, width), random(0, height));&nbsp; &nbsp; &nbsp; &nbsp; ellipseSizes.push(ellipseSize);&nbsp; &nbsp; &nbsp; &nbsp; ellipsePositions.push(ellipsePosition);&nbsp; &nbsp; }}function draw() {&nbsp; &nbsp; background(0);&nbsp; &nbsp; fill(255);&nbsp; &nbsp; // Then loop through the remembered positions and sizes, and draw an ellipse with those parameters&nbsp; &nbsp; for (var i = 0; i < ellipseSizes.length; i ++) {&nbsp; &nbsp; &nbsp; &nbsp; var ellipseSize = ellipseSizes[i];&nbsp; &nbsp; &nbsp; &nbsp; var ellipsePosition = ellipsePositions[i];&nbsp; &nbsp; &nbsp; &nbsp; ellipse(ellipsePosition.x, ellipsePosition.y, ellipseSize, ellipseSize);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript