xxxxxxxxxx
99
let a = []
let numP = 30
function setup() {
createCanvas(400, 400)
let initial = Math.random() * PI * 2
for (let i = 0; i < numP; i++) {
a[i] = Math.cos(initial + i / numP * PI * 2) * 30;
}
}
let t = 0
let maxT = 100
let branches = []
function mousePressed() {
let a = []
let initial = Math.random() * PI * 2
for (let i = 0; i < numP; i++) {
a[i] = Math.cos(initial + i / numP * PI * 2) * 30;
}
let t = 0
let branch = {
a: a,
t: t,
mouseX: mouseX,
mouseY: mouseY
}
branches.push(branch)
console.log(branch)
}
function draw() {
background(220);
let x1 = 200
let y1 = 400
if (t < maxT) {
// t += 1
} else {
// setup()
// t = 0
}
for (let b = 0; b < branches.length; b++) {
let branch = branches[b]
let x2 = branch.mouseX
let y2 = branch.mouseY
if (branch.t < 100) {
branch.t += 1
}
for (let i = 0; i < numP + 1; i++) {
let n = branch.t / maxT
// difference
let dx = (x2 - x1) * n
let dy = (y2 - y1) * n
// fractions of line
let f = i / numP
let randomX = branch.a[i]
if (i == numP) {
randomX = 0
}
// next point
let nx = x1 + (dx * f) + randomX
let ny = y1 + (dy * f)
// last point
let last = (i - 1) / numP
let lx = x1 + (dx * last) + branch.a[i - 1]
let ly = y1 + (dy * last)
let nw = (numP - i) * 2.5,
lw = (numP - i + 1) * 2.5,
nx2 = nx + nw,
lx2 = lx + lw;
noStroke()
fill(118, 177, 131, 100)
// line(lx, ly, nx, ny)
// line(lx2, ly, nx2, ny)
triangle(nx, ny, lx, ly, lx2, ly)
triangle(nx2, ny, nx, ny, lx2, ly)
}
}
fill(196, 88, 36, 255)
noStroke()
rect(80, 350, 240, 100, 20)
rect(80, 20, 240, 40, 20)
let s = 'Click to grow your plant :)';
fill(50);
textSize(18);
fill(255);
text(s, 100, 32, 300, 80);
}