Skip to content

Instantly share code, notes, and snippets.

@ptvans
Created March 17, 2014 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptvans/9594481 to your computer and use it in GitHub Desktop.
Save ptvans/9594481 to your computer and use it in GitHub Desktop.
Tributary inlet
{"description":"Tributary inlet","endpoint":"","display":"canvas","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":true,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/RBqeGWe.png"}
//laser math
//http://spot.colorado.edu/~lessley/pdf%20stuff/lasermath2.pdf
//http://spot.colorado.edu/~lessley/rosecurves.html
var x0 = 344;
var y0 = 323;
var n = 21;
//main parameters, ratio matters
var a = 5.74228 * n;
var b = 1.296 * n;
//var b = (n-1)/(n+10);
//x tilt? (x radius)
var hx = -115;
//y tilt? (y radius)
var hy = 34;
//frequency, adjust fbase
var fbase = 28.2296;
var omega0 = 2 * Math.PI * fbase;
//slow down or speed up the tracers
var om = omega0 * 0.0205964;
//how fast it fades
var erasing = 0.15464;
//number of points in the paths (more = smoother)
var linen = -3;
//color of the paths
var cs = d3.interpolateHsl("#1D0808", "#350909");
var sopacity = -0.28;
//color of the tracers
var cf = d3.interpolateHsl("#FF0000", "#3300FF");
var fopacity = 0.7;
//radius of the tracers
var radius = 3.44;
// number of tracers
var nt = 78;
//time offset for each tracer
var to = 0.60314;
function x(t, o) {
//return (a - b) * Math.cos(omega0*t) + h * Math.cos(omega0*t*(a/b-1));
return (a - b) * Math.cos(o*t) + hx * Math.cos(o*t*(a-b)/b);
//return h * Math.cos(t*Math.PI);
}
function y(t, o) {
//return (a - b) * Math.sin(omega0*t) - h * Math.sin(omega0*t*(a/b-1));
return (a - b) * Math.sin(o*t) - hy * Math.sin(o*t*(a-b)/b);
//return h * Math.sin(t*Math.PI);
}
function circle(ctx, p) {
//ctx.fillStyle = 'hsla(0,0%,' + 70 +'%,' + 1 + ')';
ctx.beginPath();
ctx.arc(p[0],p[1],radius,0,2*Math.PI);
ctx.fill();
}
function line(ctx) {
ctx.beginPath();
var i;
for(i=0;i<linen;i++) {
//ctx.strokeStyle = c(i/linen);
ctx.lineTo(x0 + x(i/linen, omega0), y0 + y(i/linen, omega0));
}
ctx.stroke();
}
tributary.init = function(ctx) {
d3.select("#display").style("background-color", "#000000")
ctx.globalCompositeOperation = 'source-over';
};
tributary.run = function(ctx,t) {
//tributary.clear(); //helper function to clear the canvas
ctx.fillStyle = "rgba(9,9,9," + erasing + ")";
ctx.fillRect(0,0,tributary.sw,tributary.sh);
var tt = Math.cos(t)/2;
var p;
ctx.strokeStyle = cs(tt);
line(ctx);
p = [x0 + x(tt, om), y0 + y(tt, om)];
ctx.fillStyle = cf(tt);
circle(ctx, p);
var i = 0;
for(i = 0; i < nt; i++) {
//p = [x0 + x(tt+i*tt/2, om), y0 + y(tt+i*tt/2, om)];
p = [x0 + x(tt+i*to, om), y0 + y(tt+i*to, om)];
ctx.fillStyle = cf(i/nt);
circle(ctx, p);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment