Built with blockbuilder.org
Created
September 4, 2016 04:27
-
-
Save cmpolis/6a6eac105966e34b34005d3c562e6e84 to your computer and use it in GitHub Desktop.
Café wall illusion
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 { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; font-family:Verdana, Geneva, sans-serif;} | |
button { | |
position: absolute; | |
padding: 12px; | |
top: 0; | |
left: 0; | |
} | |
svg line.grid { | |
stroke: black; | |
stroke-width: 1px; | |
} | |
svg * { | |
transition: all 600ms ease-in-out; | |
} | |
svg[reveal] line.grid { | |
stroke: red; | |
} | |
svg[reveal] g.tile-group { | |
opacity: 0.22 | |
} | |
</style> | |
</head> | |
<body> | |
<button>Are these lines crooked?</button> | |
<script> | |
// Feel free to change or delete any of the code you see in this editor! | |
var width = 960, | |
height = 500, | |
svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height), | |
tileWidth = width / 12, | |
tileHeight = height / 12, | |
yTicks = d3.range(-0.5, height-1, tileHeight), | |
tileOffsets = d3.range(0,12).map(function(d) { | |
return Math.sin(Math.PI * (d / 4)) * tileWidth; | |
}); | |
svg.selectAll('.grid').data(yTicks).enter() | |
.append('line').attr('class', 'grid') | |
.attr('x1', 0).attr('x2', width) | |
.attr('y1', function(d) { return d; }) | |
.attr('y2', function(d) { return d; }); | |
svg.selectAll('.tile-group').data(yTicks).enter() | |
.append('g') | |
.attr('class', 'tile-group') | |
.attr('transform', function(d, ndx) { | |
return 'translate('+(tileOffsets[ndx] - 10)+','+d+')' }); | |
svg.selectAll('.tile-group').selectAll('rect') | |
.data(function(d) { return d3.range(-1,7); }) | |
.enter().append('rect') | |
.attr('x', function(d) { return d * tileWidth * 2; }) | |
.attr('width', tileWidth) | |
.attr('height', tileHeight); | |
d3.select('button').on('click', function() { | |
svg.attr('reveal', svg.attr('reveal') === '' ? null : ''); | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment