Skip to content

Instantly share code, notes, and snippets.

@tomshanley
Created October 4, 2019 01:46
Show Gist options
  • Save tomshanley/bebcdb1e7c96263e8234cd00242ea47c to your computer and use it in GitHub Desktop.
Save tomshanley/bebcdb1e7c96263e8234cd00242ea47c 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>
<script>
console.clear()
let height = 800
let width = height
let margin = 50
let noOfCircles = 75
let sineData = d3.range(0, 101).map(function(k) {
let freq = 0.05
var value = [freq * k * Math.PI, Math.sin(freq * k * Math.PI)];
return value;
});
let sineX = d3.scaleLinear()
.domain([-1, 1])
.range([0, width])
let sineY = d3.scaleLinear()
.domain(d3.extent(sineData, function(d) {
return d[0]
}))
.range([0, height])
let line = d3.line()
.x(function(d) {
return sineX(d[1]);
})
.y(function(d) {
return sineY(d[0]);
})
.curve(d3.curveLinear)
var svg = d3.select("body").append("svg")
.attr("width", width + margin + margin)
.attr("height", height + margin + margin)
var g = svg.append('g')
.attr('transform', 'translate(' + margin + ',' + margin + ')')
var sinePath = g.append('path')
.datum(sineData)
.attr('d', line)
.style('stroke', 'black')
.style('stroke-width', 1)
.style('fill', 'none')
let node = sinePath.node()
let pathLength = Math.floor(node.getTotalLength())
var circleX = function(i){
return node.getPointAtLength(i).x
}
var circleY = function(i){
return node.getPointAtLength(i).y
}
let direction = "TBC"
let magnitude = 20
createCircles(10)
function createCircles(radius){
g.selectAll('circle').remove()
let circleID = 0
let prevCircleX = circleX(0)
let prevCircleY = circleY(0)
appendCircle(prevCircleX, prevCircleY, radius, circleID)
for (var l = 1; l < pathLength; l++) {
let thisCircleX = circleX(l)
let thisCircleY = circleY(l)
let sideX = Math.abs(thisCircleX - prevCircleX)
let sideY = Math.abs(thisCircleY - prevCircleY)
let hyp = Math.sqrt((sideX * sideX) + (sideY * sideY))
if (hyp > (radius * 2)) {
circleID = circleID + 1
prevCircleX = thisCircleX
prevCircleY = thisCircleY
appendCircle(prevCircleX, prevCircleY, radius, circleID)
}
}
console.log(circleID)
if (circleID != 75) {
let newDirection = circleID < 75 ? "down" : "up"
magnitude = direction == newDirection ? magnitude : magnitude/10
direction = newDirection
radius = circleID < 75 ? radius - magnitude : radius + magnitude
circleID = 0
createCircles(radius)
}
}
function appendCircle(x, y, r, id) {
g.append('circle')
.attr('id', 'circle-' + id)
.attr('r', r)
.attr("cx", x )
.attr("cy", y )
.style('fill', 'grey')
.style('opacity', 0.5)
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment