Skip to content

Instantly share code, notes, and snippets.

@eesur
Last active May 23, 2017 11:13
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 eesur/48438950054932863cd85d1d7fa090ca to your computer and use it in GitHub Desktop.
Save eesur/48438950054932863cd85d1d7fa090ca to your computer and use it in GitHub Desktop.
d3 | parametric equations
var s = d3.select('#vis') //selection
var t = 0 // increment
var w = 960
var h = 400
var reps = 1500 // number of repetitions
var colorRainbow = d3.scaleSequential()
.domain([0, reps])
.interpolator(d3.interpolateRainbow)
function update(_t) {
s.append('circle')
.attrs({
cx: x(_t),
cy: y(_t) + (h/2),
r: d3.randomUniform(2, 7)(),
fill: colorRainbow(_t)
})
}
// parametric equations
function x(n) {
return Math.sin(n / 10) * 100 + Math.sin(n / 15) * 100
}
function y(n) {
return Math.cos(n / 10) * 100
}
// update
d3.interval(function() {
if (t < reps) {
t++
update(t)
}
}, d3.randomUniform(8, 24)());
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<svg width="960" height="400">
<g id="vis" transform="translate(450, 10)"></g>
</svg>
<script src="//d3js.org/d3.v4.min.js" charset="utf-8"></script>
<script src="//d3js.org/d3-selection-multi.v1.min.js"></script>
<!-- d3 code -->
<script src=".script-compiled.js" charset="utf-8"></script>
</body>
</html>
const s = d3.select('#vis') //selection
let t = 0 // increment
const w = 960
const h = 400
const reps = 1500 // number of repetitions
const colorRainbow = d3.scaleSequential()
.domain([0, reps])
.interpolator(d3.interpolateRainbow)
function update(_t) {
s.append('circle')
.attrs({
cx: x(_t),
cy: y(_t) + (h/2),
r: d3.randomUniform(2, 7)(),
fill: colorRainbow(_t)
})
}
// parametric equations
function x(n) {
return Math.sin(n / 10) * 100 + Math.sin(n / 15) * 100
}
function y(n) {
return Math.cos(n / 10) * 100
}
// update
d3.interval(function() {
if (t < reps) {
t++
update(t)
}
}, d3.randomUniform(8, 24)());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment