Click on the screen to create bubbles. They will join to create a tapeworm. [SPACE] to toggle attraction. [c] for color scheme. [+] for faster. [-] for slower. [p] to play / pause. [r] for reset. [s] for save screwenshot
A fork of tapeworm (works on phone too) by Rstar37
xxxxxxxxxx
/////// t a p e w o r m //////////// ///////
// // //
////// ///////
//
////// (c) martin schneider 2010 ////// ///////
//
////// ////////////////// ///////// ///////
// // // // // // //
// /////////// ///////// ////// //
// //
//////// p5.js version 2016 ////////////////////
///////////////////////////////////////////////////
// PARAMETERS //
///////////////////////////////////////////////////
// color theme is dark
var dark = false;
// activate physics
var assemble = true;
// collision detection iteration cycles
var maxiter = 10;
// radius of the bubble
var bblsize = 8;
// cellwall thickness
var cellwall = 8;
// speed of bubble movement
var speed = 5;
// speed of gravity
var gravity = 5;
// number of bubbles, after which the color cycle should repeat
var cycle = 100;
// a variable that controls if the simulation is running
var paused = false;
///////////////////////////////////////////////////
// GLOBAL VARIABLES //
///////////////////////////////////////////////////
var bubbles = [];
var bblcount = 0;
// color variables
var bg0, bg1, c1,c2;
///////////////////////////////////////////////////
// MAIN CODE //
///////////////////////////////////////////////////
function setup() {
createCanvas(1112, 834);
noStroke();
// create color objects
bg0 = color(0);
bg1 = color(255, 45);
c1 = color("#ff6666");
c2 = color("#6666ff");
}
function draw() {
if(paused) {return;}
var n = bubbles.length;
// blur background
fill(dark ? bg0 : bg1);
rect(0, 0, width, height);
// move bubbles
for(var i = 0; i < n; i++) {
bubbles[i].move();
}
// resolve collisions
for(var cc = 0; cc < maxiter; cc++) {
for(i = 0; i < n; i++) {
bubbles[i].collides();
}
}
// draw bubbles
for(i = 0; i < n; i++) {
bubbles[i].draw();
}
// user interaction for adding bubbles
//interaction();
}
///////////////////////////////////////////////////
// MY FUNCTIONS //
///////////////////////////////////////////////////
// mouse interaction, called explicitly from the draw loop
function mouseReleased() {
// create a new bubble at the mouse pointer position
var b = new Bubble(mouseX, mouseY, bblsize, bblcount);
// let's only add the bubble if no collision occurs
if(!b.collides()) {
bubbles.push(b);
bblcount++;
}
}
// draw a bubble at (x, y) with radius r
function bubble(x, y, r, cellColor, wallColor) {
// draw cell wall
fill(wallColor);
ellipse(x, y, 2*r, 2*r);
// draw cell core
fill(cellColor);
ellipse(x, y, 2*r-cellwall, 2*r-cellwall);
}
///////////////////////////////////////////////////
// INTERACTION //
///////////////////////////////////////////////////
// adjust canvas to fill the entire window
function windowResized() {
resizeCanvas(width, height);
}
// keyboard interaction
function keyTyped() {
switch(key) {
// [SPACE] to toggle attraction
case ' ':
assemble = !assemble;
break;
// [p] to play / pause
case 'p':
paused = !paused;
break;
// [c] for color scheme
case 'c':
dark = !dark;
break;
// [+] for faster
case '+':
speed++;
break;
// [-] for slower
case '-':
speed--;
break;
// [r] for reset
case 'r':
bubbles = [];
bblcount = 0;
break;
// [s] for save screwenshot
case 's':
save("img_" + month() + '-' + day() + '_' + hour() + '-' + minute() + '-' + second() + ".jpg");
break;
}
// add speed limit
speed = constrain(speed, 1, 2*bblsize);
}