let w1Points, butterflies, parts;
let nRows, nColumns, rowHeight, columnWidth;
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 255, 255, 255);
backGroundColor=color(random(360), random(50), random(225, 255));
background(backGroundColor);
if (windowWidth>windowHeight)nColumns=int(1.5*nRows);
else nColumns=int(.75*nRows);
columnWidth=int(windowWidth/nColumns);
rowHeight=int(windowHeight/nRows);
nInner=int(random(2, 10));
aPoints=[[ new Point(.5, .2, 0), new Point(.48, .4, randomFactor), new Point(.5, .6, 0), true, true],
[ new Point(.5, .3, 0), new Point(.45, .6, randomFactor), new Point(.35, .7, randomFactor), new Point(.25, .5, 0), true, true],
[new Point(.5, .3, 0), new Point(.45, .16, randomFactor), new Point(.25, .11, randomFactor), new Point(.11, .14, randomFactor), new Point(.16, .3, randomFactor), new Point(.25, .5, 0), true, true],
[new Point(.5, .2, 0), new Point(.47, .01, randomFactor), new Point(.43, .01, 0), false, false]];
for (col=0; col<ncolors; col++) {
colors[col]=color(random(360), random(255), random(255), random(255));
for (let k=0; k<aPoints.length; k++) {
parts[k]= new Part(k, aPoints[k]);
backGroundColor=color(random(360), random(50), random(225, 255));
background(backGroundColor);
if (windowWidth>windowHeight)nColumns=int(1.5*nRows);
else nColumns=int(.75*nRows);
columnWidth=int(windowWidth/nColumns);
rowHeight=int(windowHeight/nRows);
for (nr=0; nr<nRows; nr++) {
for (nc=0; nc<nColumns; nc++) {
nIn=max(0,nInner+random(-2,2));
scale(columnWidth, rowHeight);
if(nc!=0||nr!=0)varycolors(randomFactor);
let but=new Butterfly(nr*nColumns+nc, parts.slice(),colors,nIn);
butterflies[nr*nColumns+nc]=but;
strokeWeight(1/columnWidth);
for (let k=0; k<parts.length; k++) {
fill(colors[(3*k)%ncolors]);
for (let k=0; k<parts.length; k++) {
let dec=parts[k].points[parts[k].points.length-1];
let clos=parts[k].points[parts[k].points.length-2];
parts[k].points=parts[k].rpoints.slice();
parts[k].points.push(clos);
parts[k].points.push(dec);
function touchStarted() {
let nx=int(mouseX/columnWidth);
let ny=int(mouseY/rowHeight);
let index=ny*nColumns+nx;
init(butterflies[index]);
function deviceTurned() {
function varycolors(raf) {
for (col=0; col<ncolors; col++) {
h=hue(colors[col])+int(random(-20*raf,20*raf))%360;
s=saturation(colors[col])+int(random(-20*raf,20*raf))%255;
b=brightness(colors[col])+int(random(-20*raf,20*raf))%255;
a=max(50, alpha(colors[col])+int(random(-20*raf,20*raf))%255);
colors[col]=color(h, s, b, a);
return new p5.Vector(a.x+c*(b.x-a.x), a.y+c*(b.y-a.y));