粒子创建了一条很好的轨迹。然而,我希望轨迹淡出。
我试着设置笔划颜色
stroke(random(255))
虽然设置顶点,但它会更改整个形状的颜色。
// draw particle and history
(约76行)
https://codepen.io/normanwink/project/editor/XJoRYa
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="framerate"></div>
<!-- scripts -->
<script src="https://github.com/processing/p5.js/releases/download/0.5.14/p5.min.js"></script>
<script>
function setup() {
frameRate(30);
createCanvas(1000, 500, 'WEBGL');
particle = new Particle();
}
function draw() {
background(0);
particle.update();
particle.edges();
particle.show();
var output = '';
output += floor(frameRate()) + 'fps';
document.getElementById('framerate').innerHTML = output;
}
function Particle(mX = random(width), mY = random(height)) {
this.pos = createVector(mX,mY);
this.vel = createVector(8,0);
this.acc = createVector(0,0);
this.maxSpeed = 8;
this.trail = 60; // how long to track history
this.history = [];
this.update = function() {
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed);
this.pos.add(this.vel);
this.acc.mult(0);
this.history.push(this.pos.copy());
if (this.history.length > this.trail) {
this.history.splice(0,1);
}
}
this.show = function() {
stroke(255);
strokeWeight(5);
// draw particle and history
beginShape();
for (var i=0; i<this.history.length; i++) {
var pos = this.history[i];
// stroke(random(255))
curveVertex(pos.x, pos.y);
}
endShape();
noStroke();
fill(255);
ellipse(this.pos.x, this.pos.y, 10);
}
// if particle hits the edge
this.edges = function() {
if (this.history[0].x > width && this.pos.x > width) {
this.pos.x = 0;
this.history = [];
return false;
}
if (this.history[0].x < 0 && this.pos.x < 0) {
this.pos.x = width;
this.history = [];
return false;
}
if (this.history[0].y > height && this.pos.y > height) {
this.pos.y = 0;
this.history = [];
return false;
}
if (this.history[0].y < 0 && this.pos.y < 0) {
this.pos.y = height;
this.history = [];
return false;
}
}
}
</script>
</body>
</html>
对于感兴趣的人,这里有一个完整的示例:
https://codepen.io/normanwink/pen/jLdpez