Built with blockbuilder.org
Last active
March 26, 2017 23:14
-
-
Save pstuffa/a6133b49851dd03f2f6783f02e05c17b to your computer and use it in GitHub Desktop.
Oscilloscope II
This file contains 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 |
This file contains 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://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { | |
background: #2f3337 !important; | |
padding: 0px; | |
margin: 0px; | |
vertical-align: center; | |
} | |
svg { | |
padding: 0px; | |
margin: 0px; | |
} | |
#chart { | |
text-align: center; | |
margin-top: 10%; | |
} | |
.dots { | |
fill: #53535f; | |
} | |
.line { | |
stroke: #22ffc5; | |
stroke-width: 2; | |
stroke-opacity: .75; | |
fill: none; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="chart"> </div> | |
<script> | |
var margin = {top: 10, right: 10, bottom: 10, left: 10}, | |
width = 300 - margin.left - margin.right, | |
height = 300 - margin.top - margin.bottom, | |
numTicks = 10, | |
numDashes = 5, | |
numDots = 10; | |
tickSpace = 8; | |
var y = d3.scaleLinear() | |
.range([height, 0]) | |
.domain([-1,1]) | |
var x = d3.scaleLinear() | |
.range([0, width]) | |
.domain([-1,1]) | |
var svg = d3.select("#chart").selectAll("svg") | |
.data(d3.range(3)) | |
.enter().append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.style("filter","url(#I)") | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var defs = svg.append("defs"); | |
var filter = defs.append("filter").attr("id","spect"); | |
var filterGoo = defs.append("filter").attr("id","gooeyCodeFilter"); | |
filterGoo.append("feGaussianBlur") | |
.attr("in","SourceGraphic") | |
.attr("stdDeviation","5") | |
.attr("color-interpolation-filters","sRGB") | |
.attr("result","blur"); | |
filterGoo.append("feColorMatrix") | |
.attr("in","blur") | |
.attr("mode","matrix") | |
.attr("values","1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 18 -7") | |
.attr("result","gooey"); | |
var turbulence = filter.append("feTurbulence") | |
.attr("baseFrequency",".4") | |
.attr("numOctaves","2") | |
.attr("seed", 201); | |
var backgroundnoise = svg.append("rect") | |
.attr("filter","url(#spect)") | |
.style("opacity", .2) | |
.attr("width", width) | |
.attr("height", height); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(d3.axisBottom(x).tickSize(-height).ticks(numTicks)) | |
.selectAll("line") | |
.style("stroke-dasharray", function(d,i) { | |
var dashArray = [] | |
for (var k = 0; k < numTicks; k += 1) { | |
dashArray.push((height/numTicks - (tickSpace/2) - (k > 0 ? (tickSpace/2) : 0))); | |
dashArray.push(tickSpace) | |
} | |
return dashArray.join(",") }) | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(d3.axisLeft(y).tickSize(-width).ticks(numTicks)) | |
.selectAll("line") | |
.style("stroke-dasharray", function(d,i) { | |
var dashArray = [] | |
for (var k = 0; k < numTicks; k += 1) { | |
dashArray.push((width/numTicks - (tickSpace/2) - (k > 0 ? (tickSpace/2) : 0))); | |
dashArray.push(tickSpace) | |
} | |
return dashArray.join(",") }) | |
svg.selectAll(".verticalDashes") | |
.data(d3.range(numTicks*numDashes).map(Object)) | |
.enter().append("line") | |
.attr("class", "verticalDashes") | |
.attr("y1", function(d, i) { return i*(height/numTicks/numDashes); } ) | |
.attr("y2", function(d, i) { return i*(height/numTicks/numDashes); } ) | |
.attr("x1", x(-.025)) | |
.attr("x2", x(.025)) | |
.style("stroke", "#000") | |
.style("stroke-dasharray", function() { | |
var unit = x(.025) - x(0); | |
return (unit - (tickSpace/4)) + "," + (tickSpace/2) + "," + (unit - (tickSpace/4)) | |
}) | |
svg.selectAll(".horizontalDashes") | |
.data(d3.range(numTicks*numDashes).map(Object)) | |
.enter().append("line") | |
.attr("class", "horizontalDashes") | |
.attr("x1", function(d, i) { return i*(width/numTicks/numDashes); } ) | |
.attr("x2", function(d, i) { return i*(width/numTicks/numDashes); } ) | |
.attr("y1", y(-.025)) | |
.attr("y2", y(.025)) | |
.style("stroke", "#000") | |
.style("stroke-dasharray", function() { | |
var unit = x(.025) - x(0); | |
return (unit - (tickSpace/4)) + "," + (tickSpace/2) + "," + (unit - (tickSpace/4)) | |
}) | |
svg.selectAll(".rowOne dots") | |
.data(d3.range(numTicks*numDots).map(Object)) | |
.enter().append("circle") | |
.attr("class", "rowOne dots") | |
.attr("cx", function(d, i) { return i*(width/numTicks/numDots); } ) | |
.attr("cy", function(d, i) { return i % 2 == 0 ? y(.28) : y(-.28) }) | |
.attr("r", 1); | |
svg.selectAll(".rowTwo") | |
.data(d3.range(numTicks*numDots).map(Object)) | |
.enter().append("circle") | |
.attr("class", "rowTwo dots") | |
.attr("cx", function(d, i) { return i*(width/numTicks/numDots); } ) | |
.attr("cy", function(d, i) { return i % 2 == 0 ? y(.44) : y(-.44) }) | |
.attr("r", 1); | |
svg.append("clipPath") | |
.attr("id", "clip") | |
.append("rect") | |
.attr("width", width) | |
.attr("height", height); | |
var line = d3.line() | |
.curve(d3.curveCardinal) | |
.x(function(d) { return x(d.x); }) | |
.y(function(d) { return y(Math.cos(y(d.y))/2); }); | |
var data = [] | |
svg.append("path") | |
.datum(data) | |
.attr("class", "line") | |
.attr("clip-path", "url(#clip)") | |
.attr("d", line) | |
.style("filter", "url(#gooeyCodeFilter)"); | |
var dividor = 2; | |
var strokeWidth = 10; | |
var t = d3.interval(function(elapsed) { | |
data.push({x: data.length/dividor*4-1, y: Math.sin(data.length)}); | |
data.forEach(function(d, i) { | |
d.x = Math.sin(i/dividor) | |
d.y = Math.sin(i/dividor) | |
}); | |
dividor += .1; | |
svg.select(".line") | |
.datum(data) | |
.transition() | |
.attr("d", line) | |
.style("stroke-width", strokeWidth) | |
}, 100); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment