var frameWidth = WIDTH/100;
var frameHeight = HEIGHT/100;
createCanvas(WIDTH, HEIGHT);
var stg = map(sliderstrength.value(),-2,2,-2,2);
text('k= '+nfc(stg,1,1),60,460);
translate(width/2, height/2);
let newx = map(mouseX, 0, width, -3, 3);
let newy = map(mouseY, height, 0, -2.5, 2.5);
particles[currentParticle] = new Particle(newx+random(-splatter,splatter), newy+random(-splatter,splatter), t,h);
if (currentParticle >= numMax)
for (var i=particles.length-1; i>=0; i-=1) {
if ( p.x > 3.5 || p.y > 2.5 || p.x < -3.5 || p.y < -2.5 ) {
if(mouseButton == RIGHT){
if (fieldShow == false) {
var P = (t, x, y) => sliderstrength.value()*( -y );
var Q = (t, x, y) => sliderstrength.value()*( x );
function Particle(_x, _y, _t, _h) {
this.radius = random(4,6);
this.op = random(187,200);
this.r = random(100,255);
this.b = random(200,255);
this.update = function() {
this.k1 = P(this.time, this.x, this.y);
this.j1 = Q(this.time, this.x, this.y);
this.k2 = P(this.time + 1/2 * this.h, this.x + 1/2 * this.h * this.k1, this.y + 1/2 * this.h * this.j1);
this.j2 = Q(this.time + 1/2 * this.h, this.x + 1/2 * this.h * this.k1, this.y + 1/2 * this.h * this.j1);
this.k3 = P(this.time + 1/2 * this.h, this.x + 1/2 * this.h * this.k2, this.y + 1/2 * this.h * this.j2);
this.j3 = Q(this.time + 1/2 * this.h, this.x + 1/2 * this.h * this.k2, this.y + 1/2 * this.h * this.j2);
this.k4 = P(this.time + this.h, this.x + this.h * this.k3, this.y + this.h * this.j3);
this.j4 = Q(this.time + this.h, this.x + this.h * this.k3, this.y + this.h * this.j3);
this.x = this.x + this.h/6 *(this.k1 + 2 * this.k2 + 2 * this.k3 + this.k4);
this.y = this.y + this.h/6 *(this.j1 + 2 * this.j2 + 2 * this.j3 + this.j4);
this.display = function() {
fill(this.r, this.b, this.g, this.op);
this.updatex = map(this.x, -7, 7, -width, width);
this.updatey = map(-this.y, -5, 5, -height, height);
ellipse(this.updatex, this.updatey, 2*this.radius, 2*this.radius);
sliderstrength = createSlider(-2, 2, 1.2, 0.1);
sliderstrength.position(10, 470);
sliderstrength.style('width', '140px');
buttonField = createButton('Field');
buttonField.position(350, 470);
buttonField.mousePressed(fieldShow);
buttonTrace = createButton('Trace');
buttonTrace.position(300, 470);
buttonTrace.mousePressed(traceShow);
for(var k=ymin; k<=ymax; k+=ystep){
for(var j=xmin; j<=xmax; j+=xstep){
var xx = j + sc * P(this.time, j, k);
var yy = k + sc * Q(this.time, j, k);
var lj = map(j, -6, 6, -width, width);
var lk = map(-k, -4, 4, -height, height);
var lx = map(xx, -6, 6, -width, width);
var ly = map(-yy, -4, 4, -height, height);
var angle = atan2(ly-lk, lx-lj);
var dist = sqrt((lk-ly)*(lk-ly)+(lj-lx)*(lj-lx));
triangle(-15, -4, 15, 0, -15, 4);