xxxxxxxxxx
let num = 200;
let url = "https://coolors.co/app/1b998b-2d3047-fffd82-ff9b71-e84855";
let cols;
let gra;
let pos = [];
let p = [];
let isAuto = true;
let strs = ['你','好','啊','A','B','C','a','b','c'];
//////
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(60);
gra = createGraphics(50,30);
gra.textSize(gra.height*0.85);
gra.textAlign(CENTER,CENTER);
gra.noStroke();
gra.fill(0);
cols = createCols(url);
for(let i = 0; i < num; i++)p[i] = new Particle(cols[Math.floor(random(cols.length))]);
setStr(strs[0]);
}
function draw() {
background('#F0E0DA');
if(isAuto)if(frameCount % 120 == 0)setStr(strs[Math.floor((frameCount/120))%strs.length]);
for(let i = 0; i < p.length; i++){p[i].update();}
}
//////
function keyPressed(){
if(keyCode == 32)isAuto =true;
else{
isAuto = false;
setStr(key);
}
}
///////
function setStr(_str){
gra.background(255);
gra.text(_str,gra.width/2,gra.height/2);
pos = getPosList();
setTargetPos();
}
function setTargetPos(){
let ratio = min(width,height)/min(gra.width,gra.height) * 0.8;
for(let i = 0; i < p.length; i++){
let x = (pos[i%pos.length].x-gra.width/2) * ratio + width/2;
let y = (pos[i%pos.length].y-gra.height/2) * ratio + height/2;
p[i].setTarget(x,y);
}
}
function getPosList(){
let posList = [];
for(let x =0; x < gra.width; x++){
for(let y =0; y < gra.height; y++){
let c = gra.get(x,y);
if(brightness(c) == 0)posList.push(createVector(x,y));
}
}
if(posList.length == 0)posList.push(createVector(gra.width/2,gra.height/2));
return posList;
}
/////
function createCols(_url) {
let slash_index = _url.lastIndexOf('/');
let pallate_str = _url.slice(slash_index + 1);
let arr = pallate_str.split('-');
for (let i = 0; i < arr.length; i++) {
arr[i] = '#' + arr[i];
}
return arr;
}