D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
SleepyHarry
Full window
Github gist
Lissajous
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } path { stroke: black; fill: none; } </style> </head> <body> <label>A</label> <input type="text" placeholder="A" id="A"> <label>B</label> <input type="text" placeholder="B" id="B"> <label>a</label> <input type="text" placeholder="a" id="c"> <label>b</label> <input type="text" placeholder="b" id="d"> <label>∂</label> <input type="text" placeholder="δ" id="δ"> <label>R</label> <input type="text" placeholder="R" id="R"> <svg width="960" height="500"></svg> <script> var svg = d3.select('svg'), width = +svg.attr('width'), height = +svg.attr('height'); // x = Asin(at + δ), y = Bsin(bt) var inputs = { A: 1, B: 1, c: 1, d: 1, δ: Math.PI / 2, R: Math.min(width, height) / 8, }; Object.keys(inputs).forEach(function (input) { d3.select('input#' + input).node().placeholder = inputs[input]; }); var PRECISION = 360 * 2; var lissajous = function (A, B, a, b, δ) { return function (t) { return [A * Math.sin(a*t + δ), B * Math.sin(b*t)]; }; }; var path = function () { var f = lissajous(inputs.A, inputs.B, inputs.c, inputs.d, inputs.δ), g = function (t) { var [x, y] = f(t); return [inputs.R * x, inputs.R * y]; }; var pts = d3.range(0, Math.PI * 2, Math.PI * 2 / PRECISION).map(g); return 'M' + pts.join('L'); }; var tgt = svg.append('g') .attr('transform', 'translate(' + (width / 2) + ',' + (height / 2) + ')') .append('path'); d3.timer(function () { Object.keys(inputs).forEach(function (input) { var ud = d3.select('input#' + input).node().value; inputs[input] = ud !== '' ? +ud : inputs[input]; }); inputs.δ += 0.01; if (inputs.δ > Math.PI) { inputs.δ -= Math.PI * 2; } d3.select('input#δ').node().placeholder = inputs.δ; tgt.attr('d', path()); }); </script> </body>
https://d3js.org/d3.v4.min.js