Skip to content

Instantly share code, notes, and snippets.

@ptvans
Created August 4, 2012 02:49
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/3253712 to your computer and use it in GitHub Desktop.
Save ptvans/3253712 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/BXjWAar.png"}
//laser math
//http://spot.colorado.edu/~lessley/pdf%20stuff/lasermath2.pdf
//http://spot.colorado.edu/~lessley/rosecurves.html
var x0 = 450;
var y0 = 288;
var n = 55;
//main parameters, ratio matters
var a = 6.4512 * n;
var b = -2 * n;
//var b = (n-1)/(n+10);
//x tilt? (x radius)
var hx = 498;
//y tilt? (y radius)
var hy = 4;
//frequency, adjust fbase
var fbase = 58.2568;
var omega0 = 2 * Math.PI * fbase;
//slow down or speed up the tracers
var om = omega0 * 0.03755;
//how fast it fades
var erasing = 0.05;
//number of points in the paths (more = smoother)
var linen = 300;
//color of the paths
var cs = d3.interpolateHsl("#3300FF", "#093335");
var sopacity = 1;
//color of the tracers
var cf = d3.interpolateHsl("#A80014", "#000424");
var fopacity = 0.7;
//radius of the tracers
var radius = 1;
// number of tracers
var nt = 413;
//time offset for each tracer
var to = 0.4712;
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(tt);
circle(ctx, p);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment