Hello everyone! Does anyone have any research on the transition from p5js to processing?
I tried to convert the p5js code to processing successfully but it runs abnormally slowly. I am not sure what went wrong. Can anyone help me?
p5js code!
let kMax;
let circle = [];
let n = 30;
let inter = 1.5;
let step;
let maxNoise = 100;
let noiseProg = (x) => (x);
let W;
let Hx;
let Hy;
let D;
let C=0;
let P=[];
let run = false;
function setup() {
createCanvas(windowWidth, windowHeight);
W=0;
Hx=width/2;
Hy=height/2;
D=Hy/20;
background(10);
kMax = random(0.5, 1.0);
step = 0.05;
let t;
for (let i = n; i > 0; i--) {
let k;
let noisiness;
let alpha = pow(1 - noiseProg(i / n), 3);
let COLS = [];
COLS[0] = color(255, 0, 0, alpha * 255 * 0.55);
COLS[1] = color(0, 255, 0, alpha * 255 * 0.55);
COLS[2] = color(0, 0, 255, alpha * 255 * 0.55);
circle.push(
new WaveCircle(createVector(width / 2, height / 2), 50, width * 0.1 + i * inter, width * 0.3,
COLS[0], k, 0.8, noisiness, t - i * step),
new WaveCircle(createVector(width / 2, height / 2), 50, width * 0.1 + i * inter, width * 0.3,
COLS[1], k, 0.8, noisiness, t - i * step + 0.2),
new WaveCircle(createVector(width / 2, height / 2), 50, width * 0.1 + i * inter, width * 0.3,
COLS[2], k, 0.8, noisiness, t - i * step + 0.4));
}
noFill();
noStroke();
}
function draw() {
blendMode(BLEND);
//background(10);
fill(10,100);
rect(0,0,width,height)
blendMode(ADD);
PlanetGrowth();
for (const i of circle) {
i.drawWave();
i.updateWave();
}
if (mouseIsPressed) {
W+=10;
run = true;
for (const i of circle) {
let r = 50;
const d = dist(i.center.x, i.center.y, random(width), random(height));
const v = map(d, 0, i.radius * 1.5, -i.radius / 3, -1);
var xoff = 0;
var x = map(noise(xoff), 0, 1, 0, width);
xoff += 0.01;
i.addVel(createVector(width / 2 - r * sin(x), height / 2 - r * cos(x)), v * 0.4);
i.addVel(createVector(width / 2 + r * sin(x), height / 2 - r * cos(x)), v * 0.4);
i.addVel(createVector(random(width), random(height)), v * 0.05);
//i.addVel(createVector(width/2, height/2),v*0.5);
}
}
}
class WaveCircle {
constructor(centerPos, vetrNum, radius, waveHeight, col, k, atten, noisiness, t) //waveHeight 波形高度 k弹力系数 atten衰减率
{
this.center = centerPos;
this.waveH = [];
this.vel = [];
this.vNum = vetrNum;
this.k = k;
this.atten = atten;
this.radius = radius;
this.maxWaveH = waveHeight;
this.col = col;
for (let i = 0; i < n; i++) {
this.k = kMax * sqrt(i / n) * 0.6;
this.noisiness = maxNoise * noiseProg(i / n);
}
for (var i = 0; i < this.vNum; i++) {
this.waveH[i] = 0;
this.vel[i] = 0;
}
}
drawWave() {
const baseRadius = this.radius;
const vertNum = this.vNum;
const rStep = TAU / (vertNum * 0.5);
const centerPos = this.center;
noStroke();
fill(this.col);
push();
translate(centerPos);
beginShape();
for (let i = 0; i <= vertNum + 2; i++) {
let theta = i * rStep;
//r1 = cos(theta)+1;
//r2 = sin(theta)+1;
let radius = baseRadius + this.waveH[i % vertNum];
//let radius = baseRadius + noise(k * r1, k * r2, t) * noisiness;
let x = cos(theta - PI * 0.47) * radius;
let y = sin(theta - PI * 0.47) * radius;
curveVertex(x, y);
}
endShape();
pop();
}
updateWave() {
const maxHeight = this.maxWaveH;
const num = this.vNum;
const waveH = this.waveH;
const vel = this.vel;
for (let i = 0; i < num; i++) {
const prev = i == 0 ? num - 1 : i - 1;
const next = i == num - 1 ? 0 : i + 1;
var accel = waveH[prev] - waveH[i] + waveH[next] - waveH[i];
accel *= this.k;
vel[i] = (vel[i] + accel) * this.atten;
vel[i] -= waveH[i] / 65;
}
for (let i = 0; i < num; i++) {
waveH[i] += vel[i];
if (waveH[i] > maxHeight) {
waveH[i] = maxHeight;
}
}
}
addVel(pos, vel)
{
let angle = atan2(pos.y - this.center.y, pos.x - this.center.x);
angle = angle < 0 ? angle + TAU : angle;
let i = int(map(angle, 0, TAU, 0, this.vel.length));
this.vel[i] += vel;
}
}
function PlanetGrowth() {
stroke(255);
P.length<W&&P.push({x:Hx,y:Hy})
C+=.01
i=0
for(_ of P)with(_){
mag(x-Hx,y-Hy)<100&&(x=random(width),y=0)
n=noise(x/width-C,y/width-i++*.01)*PI*5
line(x,y,
x+=cos(n)*9-(x-Hx)/D,
y+=sin(n)*9-(y-Hy)/D
)
}
}
processing code!
float kMax;
int n = 30;
ArrayList<WaveCircle> circle;
float inter = 1.5;
float step;
int maxNoise = 100;
//let noiseProg = (x) => (x);
//let W;
//let Hx;
//let Hy;
//let D;
//let C=0;
//let P=[];
//let run = false;
void setup() {
size(1920, 1080,P3D);
circle = new ArrayList<WaveCircle>();
//W=0;
//Hx=width/2;
//Hy=height/2;
//D=Hy/20;
background(10);
kMax = random(0.5, 1.0);
step = 0.05;
float t=0;
for (int i = n; i > 0; i--) {
float k=0;
float noisiness=0;
float alpha = pow(1 - noise(i/n), 3);
color COLS[] = new color[3];
COLS[0] = color(255, 0, 0, alpha * 255 * 0.55);
COLS[1] = color(0, 255, 0, alpha * 255 * 0.55);
COLS[2] = color(0, 0, 255, alpha * 255 * 0.55);
circle.add(new WaveCircle(new PVector(width / 2, height / 2), 50, width * 0.1 + i * inter, width * 0.3,
COLS[0], k, 0.8, noisiness, (t - i * step)));
circle.add( new WaveCircle(new PVector(width / 2, height / 2), 50, width * 0.1 + i * inter, width * 0.3,
COLS[1], k, 0.8, noisiness, t - i * step + 0.2));
circle.add( new WaveCircle(new PVector(width / 2, height / 2), 50, width * 0.1 + i * inter, width * 0.3,
COLS[2], k, 0.8, noisiness, t - i * step + 0.4));
}
noFill();
noStroke();
frameRate(60);
}
void draw() {
println(frameRate);
blendMode(BLEND);
//background(10);
fill(10,100);
rect(0,0,width,height);
blendMode(ADD);
float t = frameCount / 100;
//PlanetGrowth();
for (int i =0; i<circle.size();i++ ) {
WaveCircle p = circle.get(i);
p.drawWave();
p.updateWave();
if (mousePressed){
//W+=10;
//run = true;
int r = 50;
float d = dist(p.center.x, p.center.y, random(width), random(height));
float v = map(d, 0, p.radius * 1.5, -p.radius / 3, -1);
float xoff = 0;
float x = map(noise(xoff), 0, 1, 0, width);
xoff += 0.01;
p.addVel(new PVector(width / 2 - r * sin(x), height / 2 - r * cos(x)), v * 0.4);
p.addVel(new PVector(width / 2 + r * sin(x), height / 2 - r * cos(x)), v * 0.4);
p.addVel(new PVector(random(width), random(height)), v * 0.05);
//i.addVel(createVector(width/2, height/2),v*0.5);
}
}
}
class WaveCircle {
PVector center ;
float waveH[] ;
float vel[] ;
int vNum ;
float k;
float radius;
float waveHeight;
color col;
float atten;
float maxWaveH;
float noisiness_;
WaveCircle(PVector centerPos, int vetrNum_, float radius_, float waveHeight_, color col_, float k_, float atten_, float noisiness_, float t_) //waveHeight 波形高度 k弹力系数 atten衰减率
{
center = centerPos;
// this.waveH = [];
vNum = vetrNum_;
waveH = new float[vNum];
vel = new float[vNum];
k = k_;
atten = atten_;
radius = radius_;
maxWaveH = waveHeight_;
col = col_;
for (int i = 0; i < n; i++) {
k = kMax * sqrt(i / n) * 0.6;
noisiness_ = maxNoise * noise(i / n);
}
for (int i = 0; i < vNum; i++) {
waveH[i] = 0;
vel[i] = 0;
}
}
void drawWave() {
float baseRadius =radius;
float vertNum = vNum;
float rStep = TAU / (vertNum * 0.5);
PVector centerPos = center;
noStroke();
fill(col);
push();
translate(centerPos.x,centerPos.y);
beginShape();
for (int i = 0; i <= vertNum + 2; i++) {
float theta = i * rStep;
//r1 = cos(theta)+1;
//r2 = sin(theta)+1;
float radius = baseRadius + this.waveH[int(i % vertNum)];
//let radius = baseRadius + noise(k * r1, k * r2, t) * noisiness;
float x = cos(theta - PI * 0.47) * radius;
float y = sin(theta - PI * 0.47) * radius;
curveVertex(x, y);
}
endShape();
pop();
}
void updateWave() {
float maxHeight = maxWaveH;
float num = vNum;
for (int i = 0; i < num; i++) {
int prev = int(i == 0 ? num - 1 : i - 1);
int next = int(i == num - 1 ? 0 : i + 1);
float accel = waveH[prev] - waveH[i] + waveH[next] - waveH[i];
accel *= this.k;
vel[i] = (vel[i] + accel) * this.atten;
vel[i] -= waveH[i] / 65;
waveH[i] += vel[i];
if (waveH[i] > maxHeight) {
waveH[i] = maxHeight;
}
}
///
//for (int i = 0; i < num; i++) {
//}
}
void addVel(PVector pos, float vel_)
{
float angle = atan2(pos.y - this.center.y, pos.x - this.center.x);
angle = angle < 0 ? angle + TAU : angle;
int i = int(map(angle, 0, TAU, 0, vel.length));
vel[i] += vel_;
}
}
//function PlanetGrowth() {
// stroke(255);
// P.length<W&&P.push({x:Hx,y:Hy})
// C+=.01
// i=0
// for(_ of P)with(_){
// mag(x-Hx,y-Hy)<100&&(x=random(width),y=0)
// n=noise(x/width-C,y/width-i++*.01)*PI*5
// line(x,y,
// x+=cos(n)*9-(x-Hx)/D,
// y+=sin(n)*9-(y-Hy)/D
// )
// }
//}
Looking forward to your reply. Thank you very much