【发布时间】:2018-12-07 15:06:58
【问题描述】:
我正在为这个特殊的任务而苦苦挣扎。我想用某种加速度制作随机移动的粒子动画(我知道它不可能那么随机)。我发现一个网站正在使用这种效果,但我自己无法复制它,我花了好几个小时研究解决方案,但没有成功。
这个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);
}
【问题讨论】:
-
我很肯定这个网站使用这个库:vincentgarreau.com/particles.js 查看他们的代码 GitHub github.com/VincentGarreau/particles.js
-
@COLBYBROOKS 我知道这个网站,但这不是我要找的。我实际上可以像网站一样做到这一点,但我需要运动和加速方面的帮助。老实说,大多数人都在运动。 particles.js 只是直线运动,这不是我想要做的,无论如何谢谢你的回答。
-
一种方法是将粒子的
speed与Math.sin(something)相乘,其中something可能是帧数的函数,例如framesNumber * Math.PI / 180。请添加一些代码。 -
@enxaneta 我添加了代码,也许你会知道怎么做或者我做错了
标签: javascript canvas random pixi.js