Skip to content

Instantly share code, notes, and snippets.

@gcalmettes
Last active May 25, 2018 16:49
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 gcalmettes/b6b8cd0f3454337771a0226f75b585c4 to your computer and use it in GitHub Desktop.
Save gcalmettes/b6b8cd0f3454337771a0226f75b585c4 to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!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; }
</style>
</head>
<body>
<svg id='waves'></svg>
<script>
const seaLevel = 100 // meters
// const height = 10 // meters
const speed = 10 // kilometers per hour
// magic numbers
const barWidth = 10 // pixels
const svgWidth = 500 // pixels
const svgHeight = 500 // pixels
const xMargin = 0
const yMargin = svgHeight * 0.2
const waveBoxWidth = svgWidth - xMargin
const waveBoxHeight = svgHeight - yMargin
// create scale: meters to pixels
const scaleY = d3.scaleLinear().domain([0, waveBoxHeight - yMargin]).range([0, seaLevel * 2])
// number of wave rectangles
const numRects = (waveBoxWidth - xMargin) / barWidth
const svg = d3.select('#waves')
.attr('width', svgWidth)
.attr('height', svgHeight)
.append("g")
/////////////////////////////////
const height = scaleY(seaLevel)
const low = scaleY(seaLevel - height)
const high = scaleY(seaLevel + height)
const bar = svg.selectAll('rect')
.data([...new Array(numRects)])
.enter().append("rect")
.attr('fill', 'blue')
.attr('opacity', 0.7)
.attr('x', (d,i) => i * barWidth + xMargin)
.attr('width', barWidth)
.attr('y', waveBoxHeight)
.attr('height', 0)
.transition()
.ease(d3.easeQuad)
.duration(1000)
.delay((d,i) => i * 20)
.on("start", function repeat() {
d3.active(this)
.attr('y', waveBoxHeight - high)
.attr('height', high)
.transition()
.attr('y', waveBoxHeight - low)
.attr('height', low)
.transition()
.on("start", repeat);
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment