xxxxxxxxxx
let particles = [];
let amount = 8000; // 粒子数量
let noiseScale = 0.01; // 噪声缩放
let img;
function setup() {
createCanvas(windowWidth, windowHeight);
background(0); // 背景改为黑色
noStroke();
fill(255);
// 创建文本图像
img = createGraphics(width, height);
img.textSize(300); // 文本大小
img.textAlign(CENTER, CENTER);
img.fill(255); // 文本颜色
img.text("张 浩", width / 2, height / 2);
// 获取文本像素数据
img.loadPixels();
for (let i = 0; i < amount; i++) {
let x, y;
// 随机选择文本像素位置
do {
x = floor(random(width));
y = floor(random(height));
} while (img.get(x, y)[0] < 128); // 确保选择的是文本像素
let size = random(1, 10); // 随机粒子大小
let pos = createVector(x, y);
particles.push({ pos, size }); // 存储粒子位置和大小
}
}
function draw() {
background(0, 10); // 背景改为黑色,并添加透明度以创建拖尾效果
for (let i = 0; i < particles.length; i++) {
let particle = particles[i];
let p = particle.pos;
let size = particle.size;
// 绘制粒子
stroke(255); // 设置线条颜色
strokeWeight(size); // 设置粒子大小
point(p.x, p.y); // 使用 point 绘制点
// 更新粒子位置
let n = noise(p.x * noiseScale, p.y * noiseScale, frameCount * noiseScale);
let a = 2 * PI * n;
p.x += 2 * sin(a * random(1, 1.2)); // 水平方向速度加倍
p.y += 2 * cos(a) * map(sin(a / 10), -1, 1, -0.5, 0.5); // 垂直方向速度加倍
// 如果粒子离开屏幕,重新初始化到文本位置
if (!onScreen(p)) {
let x, y;
do {
x = floor(random(width));
y = floor(random(height));
} while (img.get(x, y)[0] < 128);
p.x = x;
p.y = y;
}
}
}
function onScreen(p) {
return p.x < width && p.y < height && p.x > 0 && p.y > 0;
}