Canvas JS-如何通过加速度进行随机运动

时间:2018-12-07 15:06:58

标签: javascript canvas random pixijs

我正在为这项特殊任务而苦苦挣扎。我想制作带有某种加速度的随机运动粒子的动画(我知道它不会那么随机)。我发现一个网站正在使用这种效果,但我自己无法复制它,我花了很多时间研究解决方案,但没有成功。

site具有预期的效果。我不知道如何反复进行运动和加速。

有一会儿我以为我知道它是用pixijs制造的,但是我也没有设法在pixi中做到这一点。我所能做的只是随机产生的圆周运动,但效果并不理想。

如果有人可以向正确的方向推动我,我真的很感激。

编辑: Here就是我到目前为止所说的,它仅使用圆周运动。

这只是我的JS文件:

function getRandomInt(min, max) {

min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getLineWidth(max, min, distance) {
  if(distance < min) {return 1;}
  if(distance >= min && distance <= max) {
    return -(distance - max) / min;
  } else {
    return 0;
  }
}

function particles(numberOfParticles) {
  var canvas = document.getElementById('canvas'),
      ctx = canvas.getContext('2d');

  var W = canvas.width;
  var H = canvas.height;

  var x = 0, y = 0;

  //random properties of particle
  var particleProperties = [];
  for(i = 0; i < numberOfParticles; i++) {
    var speed = Math.random() * (0.35 - 0.1) + 0.1; //0.1 - 0.35
    var radius = getRandomInt(100, 500); // 100 - 500
    var angle = 0;
    var direction = Math.random() < 0.5 ? -1 : 1;
    var circleCenterX = getRandomInt(250, 550); //250 - 550
    var circleCenterY = getRandomInt(150, 550); //150 - 550
    var sizeOfParticle = getRandomInt(1, 3);

    particleProperties[i] = [speed, radius, angle, direction, circleCenterX, circleCenterY, sizeOfParticle];
  }

  function draw() {
      ctx.clearRect(0, 0, W, H);

      var coordinatesOfParticles = [];
      //object R
      for(i = 0; i < numberOfParticles; i++) {
          var newX = particleProperties[i][1] * Math.cos(particleProperties[i][2] * (Math.PI/180) * particleProperties[i][3]);
          var newY = particleProperties[i][1] * Math.sin(particleProperties[i][2] * (Math.PI/180) * particleProperties[i][3]);
          x = newX + particleProperties[i][4];
          y = newY + particleProperties[i][5];
          ctx.beginPath();
          ctx.arc(x, y, particleProperties[i][6], 0, 2*Math.PI);
          ctx.fillStyle = 'black';
          ctx.fill();
          ctx.stroke();
          particleProperties[i][2] += particleProperties[i][0];
          coordinatesOfParticles[i] = [x, y];
      }

      for(i = 0; i < coordinatesOfParticles.length - 1; i++) {
          for(j = i + 1; j < coordinatesOfParticles.length; j++) {
              if(math.distance(coordinatesOfParticles[i], coordinatesOfParticles[j]) < 220) {
                  ctx.beginPath();
                  ctx.lineWidth = getLineWidth(220, 90, math.distance(coordinatesOfParticles[i], coordinatesOfParticles[j]));
                  ctx.moveTo(coordinatesOfParticles[i][0], coordinatesOfParticles[i][1]);
                  ctx.lineTo(coordinatesOfParticles[j][0], coordinatesOfParticles[j][1]);
                  ctx.stroke();
              }
          }
      }
  }

  setInterval(draw, 1000/60);
}

1 个答案:

答案 0 :(得分:0)

我想我找到了解决我的问题的方法,即使它看起来不像我提到的网站那样好,也可以达到目的。这是我编辑过的particles.js文件。也许会对别人有帮助。

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getLineWidth(max, min, distance) {
  if(distance < min) return 1;
  if(distance >= min && distance <= max) {
    return -(distance - max) / min;
  } else {
    return 0;
  }
}

function particles(numberOfParticles) {
  var canvas = document.getElementById('canvas'),
      ctx = canvas.getContext('2d');

  var W = canvas.width;
  var H = canvas.height;

  var x = 0, y = 0;

  //random object
  var particleProperties = [];
  for(i = 0; i < numberOfParticles; i++) {
    var speed = Math.random() * (0.30 - 0.18) + 0.18; //0.1 - 0.35
    var radius = getRandomInt(100, 200); // 100 - 500
    var angle = 0;
    var direction = Math.random() < 0.5 ? -1 : 1;
    var circleCenterX = 400; //250 - 550
    var circleCenterY = getRandomInt(100, 600); //150 - 550
    var sizeOfParticle = getRandomInt(1, 3);
    var X = getRandomInt(30, 230);
    var Y = getRandomInt(290, 360);

    particleProperties[i] = [speed, radius, angle, direction, circleCenterX, circleCenterY, sizeOfParticle, X, Y];
  }

  function draw() {
      //clear rect
      ctx.clearRect(0, 0, W, H);

      var coordinatesOfParticles = [];
      //objekt R
      for(i = 0; i < numberOfParticles; i++) {
          var newX = particleProperties[i][1] * Math.cos(particleProperties[i][2] * (Math.PI/particleProperties[i][7]) * particleProperties[i][3]);
          var newY = particleProperties[i][1] * Math.tan(particleProperties[i][2] * (Math.PI/particleProperties[i][8]) * particleProperties[i][3]);
          x = newX + particleProperties[i][4];
          y = newY + particleProperties[i][5];
          ctx.beginPath();
          ctx.arc(x, y, particleProperties[i][6], 0, 2*Math.PI);
          ctx.fillStyle = 'grey';
          ctx.fill();
          ctx.stroke();
          particleProperties[i][2] += particleProperties[i][0];
          coordinatesOfParticles[i] = [x, y];
      }

      for(i = 0; i < coordinatesOfParticles.length - 1; i++) {
          for(j = i + 1; j < coordinatesOfParticles.length; j++) {
              if(math.distance(coordinatesOfParticles[i], coordinatesOfParticles[j]) < 190) {
                  ctx.beginPath();
                  ctx.lineWidth = getLineWidth(190, 90, math.distance(coordinatesOfParticles[i], coordinatesOfParticles[j]));
                  ctx.moveTo(coordinatesOfParticles[i][0], coordinatesOfParticles[i][1]);
                  ctx.lineTo(coordinatesOfParticles[j][0], coordinatesOfParticles[j][1]);
                  ctx.stroke();
              }
          }
      }
  }

  setInterval(draw, 1000/60);
}