class Box {
constructor(i, n) {
this.radius = 350
this.w = map(i, 0, n - 1, 80, 180)
this.startAngle = random(TAU)
this.endAngle = this.startAngle + expRand(PI / 6, PI / 2)
this.black = random() < 0.5
this.offset = random()
this.rot = random(TAU)
}
display(s, t, g) {
const tt = (t + this.offset) % 1
const endAngle =
this.startAngle +
easings.easeQuadInOut(linearstep(tt, 0, 0.25)) *
(this.endAngle - this.startAngle)
const startAngle =
this.startAngle +
easings.easeQuadInOut(linearstep(tt, 0.3, 0.55)) *
(this.endAngle - this.startAngle)
if (endAngle - startAngle > 0.001) {
const white = 250
const n = ((endAngle - startAngle) / 0.05) | 0
const angles = array(n).map((i) =>
map(i, 0, n - 1, startAngle, endAngle)
)
const coses = array(n).map((i) => s.cos(angles[i]))
const sines = array(n).map((i) => s.sin(angles[i]))
s.push()
s.noStroke()
s.fill(this.black ? 30 : white)
const nn = 8
s.beginShape()
for(let i = 0; i < nn; i ++) {
let angle = i/nn * TAU + PI/4
s.vertex(
coses[0] * (this.radius + s.sin(angle)*this.w / 2),
s.cos(angle)*this.w / 2,
sines[0] * (this.radius + s.sin(angle)*this.w / 2)
)
}
s.endShape(s.CLOSE)
s.beginShape()
for(let i = 0; i < nn; i ++) {
let angle = i/nn * TAU + PI/4
s.vertex(
coses[1] * (this.radius + s.sin(angle)*this.w / 2),
s.cos(angle)*this.w / 2,
sines[1] * (this.radius + s.sin(angle)*this.w / 2)
)
}
s.endShape(s.CLOSE)
for(let j = 0; j < nn; j ++) {
const angle = j / nn * TAU + PI/4
const angle2 = (j+1) / nn * TAU + PI/4
s.beginShape(s.TRIANGLE_STRIP)
array(n).forEach((i) => {
s.vertex(
coses[i] * (this.radius + s.sin(angle)*this.w / 2),
+ s.cos(angle)*this.w / 2,
sines[i] * (this.radius + s.sin(angle)*this.w / 2)
)
s.vertex(
coses[i] * (this.radius + s.sin(angle2)*this.w / 2),
+ s.cos(angle2)*this.w / 2,
sines[i] * (this.radius + s.sin(angle2)*this.w / 2)
)
})
s.endShape()
}
s.noFill()
s.strokeWeight(7)
s.stroke(this.black ? white : 30)
s.beginShape()
for(let i = 0; i < nn; i ++) {
let angle = i/nn * TAU + PI/4
s.vertex(
coses[0] * (this.radius + s.sin(angle)*this.w / 2),
s.cos(angle)*this.w / 2,
sines[0] * (this.radius + s.sin(angle)*this.w / 2)
)
}
s.endShape(s.CLOSE)
s.beginShape()
for(let i = 0; i < nn; i ++) {
let angle = i/nn * TAU + PI/4
s.vertex(
coses[n - 1] * (this.radius + s.sin(angle)*this.w / 2),
s.cos(angle)*this.w / 2,
sines[n - 1] * (this.radius + s.sin(angle)*this.w / 2)
)
}
s.endShape(s.CLOSE)
for(let j = 0; j < nn; j ++) {
const angle = j / nn * TAU + PI/4
s.beginShape()
array(n).forEach((i) => {
s.vertex(
coses[i] * (this.radius + s.sin(angle)*this.w / 2),
+ s.cos(angle)*this.w / 2,
sines[i] * (this.radius + s.sin(angle)*this.w / 2)
)
})
s.endShape()
}
s.pop()
}
}
}