A Pen By
tamichan

JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.

Any URL's added here will be added as `<script>`

s in order, and run *before* the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.

If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.

You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.

+ add another resource

` ````
<div id='p5js_container'>
<div class="controlls">
</div>
</div>
```

` ````
#p5js_container {
width: 500px;
margin: auto;
position: relative;
}
#p5js_container canvas {
border: 2px solid #DEDBD7;
border-radius: 5px;
}
body {
background-color: #343230;
}
.controlls {
position: absolute;
top: 0;
left: 0;
}
button {
margin: 5px 10px;
padding: 5px 10px;
border: none;
box-sizing:border-box;
display: inline-block;
font-size: 80%;
position: relative;
cursor: pointer;
}
button::before,
button::after {
box-sizing:border-box;
}
/* buttonA */
.btn {
background-color: #2474D3;
color: black;
}
```

` ````
var canvas;
var cell_length = 100;
const cells = [];
function addButton(x, y, name, fn) {
const btn = createButton(name);
btn.position(x,y);
btn.mousePressed(fn);
btn.class('btn');
btn.parent('p5js_container');
}
function setup() {
canvas = createCanvas(500, 500);
canvas.parent('p5js_container');
addButton(0,0,'Reset',reset);
addButton(60,0,'Default',()=>changeMode('default'));
addButton(130,0,'Spots',()=>changeMode('spots'));
addButton(190,0,'Stripes',()=>changeMode('stripes'));
addButton(260,0,'Dark spots',()=>changeMode('dark spots'));
addButton(350,0,'Chaotic',()=>changeMode('chaotic'));
//addButton(420,0,'Girraff',()=>changeMode('girraff'));
for (let y=0; y<cell_length; y++) {
cells.push([]);
for (let x=0; x<cell_length; x++) {
const cell = new Cell();
cells[y].push(cell);
}
}
for (let y=0; y<cell_length; y++) {
for (let x=0; x<cell_length; x++) {
const c = cells[y][x];
c.neighbor.push(cells[max(y-1,0)][x]);
c.neighbor.push(cells[min(y+1,cell_length-1)][x]);
c.neighbor.push(cells[y][max(x-1,0)]);
c.neighbor.push(cells[y][min(x+1,cell_length-1)]);
}
}
reset();
}
function reset() {
for (let y=0; y<cell_length; y++) {
for (let x=0; x<cell_length; x++) {
const c = cells[y][x];
c.u = uMax * ((uMinRand + random(uMaxRand-uMinRand))/100.0);
c.v = vMax * ((vMinRand + random(vMaxRand-vMinRand))/100.0);
}
}
}
function draw() {
background(0);
scale(5);
for (let n=0; n<40; n++) {
for (let y=0; y<cell_length; y++) {
for (let x=0; x<cell_length; x++) {
const c = cells[y][x];
c.react();
c.spread();
}
}
for (let y=0; y<cell_length; y++) {
for (let x=0; x<cell_length; x++) {
const c = cells[y][x];
c.sum();
}
}
}
for (let y=0; y<cell_length; y++) {
for (let x=0; x<cell_length; x++) {
const c = cells[y][x];
const r = (c.u/uMax)*255;
const b = (c.v/vMax)*255;
stroke(r,r,b);
point(x, y);
}
}
}
function changeMode(mode) {
Au = .08;
Bu = -0.08;
Cu = .04;
Du = .03;
Av = .1;
Bv = 0.0;
Cv = -0.15;
Dv = .08;
Fmax = .2;
Gmax = .5;
uMax = 7.5;
vMax = 10.0;
uMinRand = 20;
uMaxRand = 80;
vMinRand = 10;
vMaxRand = 90;
dt = .1;
dx = .6;
DU = .02 / (dx*dx);
DV = .5 / (dx*dx);
switch (mode) {
case "spots":
Cu = 0.02;
Av = 0.11;
Dv = 0.06;
break;
case "stripes":
Cu = 0.1;
Av = 0.11;
Dv = 0.06;
break;
case "dark spots":
Cu = .2;
Av = 0.11;
Dv = 0.06;
break;
case "chaotic":
DU = 0.01 / (dx*dx);
DV = 0.01 / (dx*dx);
Au = 0.09;
Cu = 0;
Av = 0.06;
Dv = 0.03;
break;
case "girraff":
DV = 0.25 / (dx*dx);
Cu = 0;
Av = 0.06;
Dv = 0.03;
Gmax = 0.1;
break;
}
reset();
}
let Au = .08;
let Bu = -0.08;
let Cu = .04;
let Du = .03;
let Av = .1;
let Bv = 0.0;
let Cv = -0.15;
let Dv = .08;
let Fmax = .2;
let Gmax = .5;
let uMax = 7.5;
let vMax = 10.0;
let uMinRand = 20;
let uMaxRand = 80;
let vMinRand = 10;
let vMaxRand = 90;
let dt = .1;
let dx = .6;
let DU = .02 / (dx*dx);
let DV = .5 / (dx*dx);
class Cell {
constructor () {
this.neighbor = [];
//this.u = 0.0;
this.u = 0.0;
this.v = 0.0;
//this.v = random(3);
this.u_reacted = 0;
this.v_reacted = 0;
this.u_spreaded = 0;
this.v_spreaded = 0;
}
react() {
this.u_reacted = Au*this.u + Bu*this.v + Cu;
this.u_reacted = max(0,min(this.u_reacted, Fmax));
this.v_reacted = Bv*this.v + Av*this.u + Cv;
this.v_reacted = max(0,min(this.v_reacted, Gmax));
}
spread() {
let sumU = 0; let sumV = 0;
this.neighbor.map(nei => {
sumU += nei.u;
sumV += nei.v;
})
this.u_spreaded = DU * (sumU - this.u*this.neighbor.length);
this.v_spreaded = DV * (sumV - this.v*this.neighbor.length);
}
sum() {
this.u = this.u + ((this.u_reacted - Du*this.u) + this.u_spreaded) * dt;
this.v = this.v + ((this.v_reacted - Dv*this.v) + this.v_spreaded) * dt;
this.u_spreaded = this.v_spreaded = 0;
this.u_reacted = this.v_reacted = 0;
}
}
```

999px

Loading
..................

Alt F
Opt F
Find & Replace

Also see: Tab Triggers