Created
July 1, 2020 15:49
-
-
Save AEnMe/cf78a9e27e92f1a952b466ccdb370183 to your computer and use it in GitHub Desktop.
oscillate
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
/* | |
name convention: | |
[1,2,3,4] | |
["sin","cos","square","saw","tri"] | |
or | |
["sine","cosine","square",sawtooth","triangle"] | |
*/ | |
function oscillate(w,f,a,t) { | |
//Defaults | |
if(!arguments[0]) w="sin"; | |
if(!arguments[1]) f=1; | |
if(!arguments[2]) a=1; | |
if(!arguments[3]) t=time; | |
try { | |
w=w.toString().toLowerCase(); | |
x=t*f; | |
switch(w){ | |
case "1": case "sin": case "sine": | |
return Math.sin(2*Math.PI*x)*a; | |
case "2": case "cos": case "cosine": | |
return Math.cos(2*Math.PI*x)*a; | |
case "3": case "square": | |
return Math.floor(Math.sin(2*Math.PI*x))*a*2+a; | |
case "4": case "saw": case "sawtooth": | |
x=x*f+f/2;x<0?adj=a:adj=-a; | |
return ((x%f)/f)*a*2+adj; | |
case "5": case "tri": case "triangle": | |
x=x*f-f/4;x<0?adj=a:adj=-a; | |
return (Math.abs(((x%f)/f)*a*2+adj)-a/2)*2; | |
default: | |
return false; | |
} | |
} catch(e) {return false} | |
} | |
value + oscillate(w,f,a,t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment