Skip to content

Instantly share code, notes, and snippets.

@TheNeuralBit
Last active February 12, 2016 15:26
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 TheNeuralBit/383a2c182fdb64a4c775 to your computer and use it in GitHub Desktop.
Save TheNeuralBit/383a2c182fdb64a4c775 to your computer and use it in GitHub Desktop.
Animate a random Lissajous Curve
<html>
<body>
<svg></svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.14/d3.min.js"></script>
<script>
var lineData = d3.range(1000);
function get_rand() {
return Math.round(Math.random()*9 + 0.5);
}
var a = get_rand();
var b = get_rand();
//This is the accessor function we talked about above
function lineFunction(delta_x, delta_y) {
return d3.svg.line()
.x(function(d) {
return 100*Math.cos(a*2*Math.PI*d/180 + delta_x) + 125;
})
.y(function(d) {
return 100*Math.sin(b*2*Math.PI*d/180 + delta_y) + 125;
})
.interpolate("basis");
}
function randomLineFunction(lineData) {
return lineFunction(randomOdd(), randomOdd())(lineData);
}
//The SVG Container
var svgContainer = d3.select("svg")
.attr("width", 300)
.attr("height", 300);
//The line SVG Path we draw8
var curr_offset = 0.1;
var lineGraph = svgContainer.append("path")
.attr("d", lineFunction(curr_offset, 0)(lineData))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");
function updateCurve() {
curr_offset += Math.PI/8;
lineGraph
.transition()
.ease(d3.ease('linear'))
.duration(500)
.attr("d", lineFunction(curr_offset, 0)(lineData));
setTimeout(updateCurve, 500);
}
updateCurve();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment