Built with blockbuilder.org
Last active
May 2, 2018 14:19
-
-
Save basilesimon/efa31f333313e5ba958c047fc5c420f1 to your computer and use it in GitHub Desktop.
Fun with lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit | |
height: 210 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://unpkg.com/d3"></script> | |
<script src="https://unpkg.com/d3-jetpack"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
svg { margin: 0 auto; } | |
</style> | |
</head> | |
<body> | |
<script> | |
const numberOfLines = 10; | |
let data = { v: [], h: [] }; | |
for (let i = 0; i < parseInt(numberOfLines); i++) { | |
data.v.push([{ x: Math.random(), y: 0 }, { x: Math.random(), y: 1 }]); | |
data.h.push([{ x: 0, y: Math.random() }, { x: 1, y: Math.random() }]); | |
} | |
const svg = d3 | |
.select('body') | |
.append('svg') | |
.at({ width: 200, height: 200 }); | |
const blues = d3.scaleOrdinal(d3.schemeBlues[9]); | |
const scale = d3 | |
.scaleLinear() | |
.domain([0, 1]) | |
.range([10, 200 - 10]); | |
const line = d3 | |
.line() | |
.x(d => scale(d.x)) | |
.y(d => scale(d.y)); | |
function drawLines(selection, data, className) { | |
selection.selectAll(className) | |
.data(data) | |
.enter() | |
.append('path') | |
.at({ | |
class: className + ' line', | |
fill: 'none', | |
strokeLinecap: 'round', | |
d: line, | |
}); | |
} | |
function draw() { | |
d3.selectAll('.line').each(function() { | |
d3 | |
.select(this) | |
.transition() | |
.duration(100) | |
.at({ strokeWidth: 0 }); | |
}); | |
const vlines = drawLines(svg, data.v, '.vlines') | |
const hlines = drawLines(svg, data.h, '.hlines') | |
d3.selectAll('.line').each(function(d, i) { | |
const totalLength = d3 | |
.select(this) | |
.node() | |
.getTotalLength(); | |
d3 | |
.select(this) | |
.at({ | |
strokeDasharray: totalLength + ' ' + totalLength, | |
strokeDashoffset: totalLength, | |
}) | |
.transition() | |
.duration(200) | |
.delay(100 * i) | |
.at({ strokeDashoffset: 0 }) | |
.st({ | |
strokeWidth: Math.random() * 20, | |
stroke: d3.interpolateSpectral(Math.random()), | |
}); | |
}); | |
} | |
draw(data); | |
setInterval(() => { | |
let data = { v: [], h: [] }; | |
for (let i = 0; i < parseInt(numberOfLines); i++) { | |
data.v.push([{ x: Math.random(), y: 0 }, { x: Math.random(), y: 1 }]); | |
data.h.push([{ x: 0, y: Math.random() }, { x: 1, y: Math.random() }]); | |
} | |
return draw(data); | |
}, 3000); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment