Skip to content

Instantly share code, notes, and snippets.

@EmilienDupont
Last active June 6, 2016 15:48
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 EmilienDupont/1447fd2b25651002961b40d7e1747ea4 to your computer and use it in GitHub Desktop.
Save EmilienDupont/1447fd2b25651002961b40d7e1747ea4 to your computer and use it in GitHub Desktop.
Infinite Relaxing Spinny Lines
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #222
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500,
then = Date.now();
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var lineFunction = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("linear");
var sphere_radius = .4 * height,
sphere_center = {"x": width/2, "y": height/2};
function x_proj(theta, phi) {
return sphere_center.x + sphere_radius * Math.sin(theta) * Math.cos(phi);
}
function y_proj(theta) {
return sphere_center.y + sphere_radius * Math.cos(theta);
}
function depth_proj(theta, phi) {
return 1 + 0.5 * Math.sin(theta) * Math.sin(phi);
}
function create_path(timesteps) {
var path = [];
/* Get initial parameters */
var theta_init = Math.PI * Math.random(),
phi_init = 2 * Math.PI * Math.random(),
v_theta = 0.002 * Math.random() + 0.002,
v_phi = 0.05 * Math.random() + 0.05,
theta = 0,
phi = 0,
tilt = Math.PI / 8 + Math.PI/ 5 * Math.random();
/* Calculate path */
for (t = 0; t < timesteps; t++) {
phi = v_phi * t;
theta = tilt * Math.sin(phi - phi_init) + v_theta * t;
path.push({"x" : x_proj(theta_init + theta, phi_init + phi),
"y" : y_proj(theta_init + theta)});
}
return path;
}
function draw_path(path_data, color) {
var circular_path = svg.append("path")
.attr("d", lineFunction(path_data) )
.attr("stroke", color)
.attr("stroke-width", 4)
.attr("stroke-opacity", 0.5)
.attr("fill", "none");
var path_length = circular_path.node().getTotalLength();
circular_path.attr("stroke-dasharray", path_length + " " + path_length)
.attr("stroke-dashoffset", path_length)
.transition()
.duration(10000)
.ease("circleout")
.attr("stroke-dashoffset", 0)
.attr("stroke-opacity", 0)
.remove();
}
function draw_many_paths(num_paths) {
for (i = 0; i < num_paths; i++) {
var colour = d3.hsl(Math.floor(360 * Math.random()), 1, .5);
draw_path(create_path(700), colour);
}
}
draw_many_paths(10);
d3.timer(function() {
var time = Date.now() - then;
if (time > 1000) {
draw_many_paths(3);
then = Date.now();
}
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment