Last active
September 22, 2017 03:22
-
-
Save GoSubRoutine/f9a0a94dcfee23a037b35f308b256d98 to your computer and use it in GitHub Desktop.
Pulsing Equations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
height: 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script async src=http://CDN.JSDelivr.net/npm/p5></script> | |
<script defer src=http://CDN.JSDelivr.net/gh/dataarts/dat.gui/build/dat.gui.min.js></script> | |
<script defer src=sketch.js></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Pulsing Equations (v3.0.2) | |
* by _vk (2014/Sep) | |
* mod GoToLoop (2017/Sep/21) | |
* | |
* Forum.Processing.org/two/discussion/7147/ | |
* some-simple-pulse-equations-using-trig-#Item_10 | |
* | |
* Forum.Processing.org/two/discussion/23468/loading-external-pde#Item_20 | |
* | |
* Bl.ocks.org/GoSubRoutine/f9a0a94dcfee23a037b35f308b256d98 | |
*/ | |
"use strict"; | |
const EQUATIONS = Object.freeze([ | |
"sin(t)", | |
"cos(t)", | |
"cos(t)*sin(t)", | |
"sin(t)*sin(t*1.5)", | |
"sin(tan(cos(t)*1.2))", | |
"sin(tan(t)*.05)", | |
"cos(sin(t*3.0))*sin(t*.2)", | |
"sin(pow(8.0, sin(t)))", | |
"sin(exp(cos(t*.8))*2.0)", | |
"sin(t - PI*tan(t)*.01)", | |
"pow(sin(t*PI), 12.0)", | |
"cos(sin(t)*tan(t*PI)*PI/8.0)", | |
"sin(tan(t)*pow(sin(t), 10.0))", | |
"cos(sin(t*3.0) + t*3.0)", | |
"pow(abs(sin(t*2.0))*.6, sin(t*2.0)) * .6" | |
]); | |
const TXT = 'Press any button for next equation', | |
FNT = 'DejaVu Sans', | |
FPS = 60, OFFSET = 20, OPACITY = 50, | |
vals = { size: 200, step: .5, spd: .15, amp: 5 }; | |
let idx = 0, equation = EQUATIONS[idx], x = 0, txtW; | |
function setup() { | |
createCanvas(500, 500).mouseClicked(keyTyped); | |
colorMode(RGB).blendMode(BLEND); | |
rectMode(CORNERS).ellipseMode(CENTER).angleMode(RADIANS); | |
strokeCap(ROUND).frameRate(FPS).background(0); | |
textFont(FNT).textAlign(LEFT).textSize(15); | |
txtW = OFFSET + textWidth(TXT); | |
createGUI(); | |
} | |
function draw() { | |
fill(0, OPACITY).noStroke(); | |
rect(0, 0, width, height - 150); | |
fill(0).rect(0, height - 50, width, height); | |
fill(220).ellipse(35, height - 37, 20); | |
fill(0).textSize(14); | |
text(nf(idx+1, 2), 26, height - 32); | |
fill(150).textSize(15); | |
text(TXT, width - txtW, height - 12); | |
fill(200).textSize(20); | |
text(equation, 54, height - 32); | |
const diam = evalEquation(equation, millis()*vals.spd, vals.size); | |
fill(0xff).ellipse(width>>1, (height>>1) - 100, diam); | |
stroke(0xd0, 0, 0).point(x, height - 100 + diam/vals.amp); | |
stroke(80, 0, 0).line(0, height - 100, width, height - 100); | |
if ((x += vals.step) > width || x < 0) { | |
x = x > width? 0 : width | |
fill(0).noStroke(); | |
rect(0, height - 150, width, height); | |
} | |
} | |
function keyTyped() { | |
fill(x = 0).noStroke(); | |
rect(0, height - 250, width, height); | |
equation = EQUATIONS[idx = (idx+1) % EQUATIONS.length]; | |
} | |
function evalEquation(expression, theta, mult) { | |
const t = theta * DEG_TO_RAD; | |
return mult * eval(expression); | |
} | |
function createGUI() { | |
const gui = new dat.GUI; | |
gui.add(vals, 'size', height>>2, height>>1, 1); // 200 | |
gui.add(vals, 'step', -3, 3, .01); // .5 | |
gui.add(vals, 'spd', -2, 2, .01); // .15 | |
gui.add(vals, 'amp', -20, 20, .1); // 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment